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(); 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); } } } }