可用于UI遮挡点击3d物体。
对于EventSystem.current.IsPointerOverGameObject这个函数,因为EventSystem是UI的事件系统,所以IsPointerOverGameObject中的GameObject是针对UI的,而不是普遍意义上的GameObject
EventSystem:添加Canvas或者UI的时候自动添加
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class TestTouch : MonoBehaviour {
public Text text;
void Update()
{
if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IPHONE)
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
if (EventSystem.current.IsPointerOverGameObject())
#endif
{
Debug.Log("当前触摸在UI上");
return;
}
else
{
Debug.Log("当前没有触摸在UI上");
}
}
}
}