在我们开发app的过程中,有时会需要到一种情况:点击设备屏幕中的空白地方,隐藏UI。我的第一想法就是通过判断点击是否在UI上,得到true/false,进行下一步的操作。下面就是判断代码
public Text text;
void Update () {
if (Input.GetMouseButton(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)){
#if UNITY_ANDROID
if(UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
if(EventSystem.current.IsPointerOverGameObject())
#endif
{
Debug.Log("触摸在UI上");
text.text = "触摸在UI上";
}
else
{
Debug.Log("没有触摸在UI上");
text.text = "没有触摸在UI上";
}
}
}
Android设备测试正确。