if (Input.GetMouseButtonDown(1))
{
//EventSystem.current就是场景中的EventSystem
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
if (results.Count > 0)
{
results[0].gameObject.GetComponent<Button>().onClick.Invoke();
}
}
if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
#if IPHONE || ANDROID
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
if (EventSystem.current.IsPointerOverGameObject())
#endif
Debug.Log("当前触摸在UI上" + EventSystem.current.currentSelectedGameObject.name);
else
Debug.Log("当前没有触摸在UI上");
}
Unity用坐标来判断是否点击在UI上,并调用
最新推荐文章于 2024-07-20 11:57:18 发布