您的位置:首页 > 移动开发 > Unity3D

Unity用坐标来判断是否点击在UI上,并调用

2018-01-16 11:43 1186 查看
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上");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: