Unity3D_UGUI判斷鼠標或者手指是否點擊在UI上


 比如戰斗場景,UI和3D場景同時都需要響應觸摸事件,如果同時響應可能就會出現觸摸UI的時候影響到了3D部分。為了解決這個問題在判斷3D響應之前要先判斷手指是否點擊在UI上。 以前NGUI的時候都是自己來發送射線判斷,現在UGUI好了系統提供了更為簡便的方法。

#if UNITY_ANDROID && !UNITY_EDITOR
#define ANDROID
#endif

#if UNITY_IPHONE && !UNITY_EDITOR
#define IPHONE
#endif

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
public class NewBehaviourScript : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
            #if IPHONE || ANDROID
                if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            #else
                if (EventSystem.current.IsPointerOverGameObject())
            #endif
                    Debug.Log("當前觸摸在UI上");
                else
                    Debug.Log("當前沒有觸摸在UI上");
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM