版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
想象这么一个场景,我的鼠标发出射线每帧检测,碰到物体就触发函数,但是如果射线同时碰到UI和物体(即射线穿过UI)该怎么办呢?
我的解决办法是这样的:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class MouseExample : MonoBehaviour
{
void Update()
{
// Check if the left mouse button was clicked
if (Input.GetMouseButtonDown(0))
{
// Check if the mouse was clicked over a UI element
if (!EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("射线不会透过UI执行其他的操作");
}
}
}
}
大家可以尝试一下。