天天看点

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;
    }