天天看點

Unity 判斷是否點到UI的代碼

public bool IsPointUI()
    {
        bool isPointUI = false;

        if (Input.GetMouseButtonDown(0)
            || Input.GetMouseButton(0)
            || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {

#if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
            if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
            if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
#endif
                //Debug.Log("目前觸摸在UI上");
                isPointUI = true;
            //else
            //    Debug.Log("目前沒有觸摸在UI上");
        }

        return isPointUI;
    }