//PC端判断
if (EventSystem.current.IsPointerOverGameObject())
{
//点击UI上面
}
else
{
//点击不在UI上面
}
//移动端判断 screenPosition=Input.mousePosition
public bool IsPointerOverGameObject(Vector2 screenPosition)
{
if (Input.GetMouseButtonDown(0))
{
//实例化点击事件
PointerEventData eventDataCurrentPosition = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
//将点击位置的屏幕坐标赋值给点击事件
eventDataCurrentPosition.position = new Vector2(screenPosition.x, screenPosition.y);
List<RaycastResult> results = new List<RaycastResult>();
//向点击处发射射线
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
return false;
}
unityPC和移动端判断是否点击UI
最新推荐文章于 2023-05-04 15:42:41 发布