很多的时候我们要判断时候都点中UGUI 此时要加命名空间 using UnityEngine.EventSystem; 有的时候新建脚本并不能直接用 UGUI 此时也要加命名空间 using UnityEngine.UI; 接下来就是判断是否点击在UGUI上了 ,上代码
//Windows 上 if (Input.GetMouseButtonDown(0))
{
Debug.Log(EventSystem.current.gameObject.name);
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("当前触摸在UI上");
}
else
{
Debug.Log("当前没有触摸在UI上");
}
}
//安卓上 if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
if (IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
Debug.Log("Hit UI, Ignore Touch");
}
else
{
Debug.Log("Handle Touch");
}
}
bool IsPointerOverGameObject(int fingerId) { EventSystem eventSystem = EventSystem.current; return (eventSystem.IsPointerOverGameObject(fingerId) && eventSystem.currentSelectedGameObject != null); }
|