39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
|
||
public class UIBlocker : MonoBehaviour
|
||
{
|
||
public GameObject uiBlocker; // 拦截点击的UI画面
|
||
|
||
void Update()
|
||
{
|
||
if (Input.GetMouseButtonDown(0))
|
||
{
|
||
// 使用Raycast检查UI元素
|
||
PointerEventData pointerData = new PointerEventData(EventSystem.current)
|
||
{
|
||
position = Input.mousePosition
|
||
};
|
||
|
||
var results = new List<RaycastResult>();
|
||
EventSystem.current.RaycastAll(pointerData, results);
|
||
|
||
// 如果结果中有UI元素,则不处理3D物体点击
|
||
if (uiBlocker.activeInHierarchy && results.Count > 0)
|
||
{
|
||
Debug.Log("Clicked on UI.");
|
||
return; // 返回,避免处理3D物体
|
||
}
|
||
|
||
// 进行3D物体点击检测
|
||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
if (Physics.Raycast(ray, out RaycastHit hit))
|
||
{
|
||
Debug.Log("Clicked on: " + hit.collider.name);
|
||
}
|
||
}
|
||
}
|
||
}
|