UGUI 系统提供了简便的方法
EventSystem.current.IsPointerOverGameObject() 如果当前鼠标在 ui 上返回true 否则返回false
实例如下:
void Click ()
{
//从主这相机到鼠标点发射一条射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//接受射线返回的碰撞信息
RaycastHit hitInfo;
//如果射线碰撞到东西
//EventSystem.current.IsPointerOverGameObject() 如果当前鼠标在 ui 上返回true 否则返回false
if (Physics.Raycast(ray, out hitInfo) && !EventSystem.current.IsPointerOverGameObject())
{
if (hitInfo.collider.tag == "plane")
{
Debug.Log("点击了Plane...");
}
}
}