解决方案1:2D转3D
UI底背景铺一个热区 当点击到该热区时说明不被其他UI遮挡 这时再用该点发射3D射线 进行3D的碰撞检测
解决方案2:3D阻挡
用EventSystem.current.IsPointerOverGameObject()方法判断鼠标是否在UI上
public class Test : MonoBehavIoUr {
private void Update() { CheckMouseInput(); } // 检测鼠标输入 private void CheckMouseInput() { // 左键点击 if (Input.GetMouseButton(0) && !EventSystem.current.IsPointerOverGameObject()) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray,out hit)) { // OK } } }
}