判断鼠标是否位于UI上,最常见的方法是IsPointerOverGameObject。但是这个函数无法判断鼠标在哪个UI上,例始场景中既有WorldSpace的UI,也有普通UI,怎么区分呢。
1.首先选中场景中的EventSystem,移动鼠标,查看参数变化。2d UI或3d UI(WorldSpace)主要区别在distance。
2.获取Distance分别UI。
(1)方法一
graphicRaycast在Canvas上。
public bool IsOver2dUI()
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List results = new List();
graphicRaycast.Raycast(pointerEventData, results);
if (results.Count != 0 && results[0].distance < 0.0001f)
{
Debug.Log("2d ");
return true;
}
return false;
}
(1)方法二
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List results = new List();
EventSystem.current.RaycastAll(pointerEventData, results);
if (results.Count != 0 && results[0].distance < 0.0001f)
{
return true;
}
return false;