NG_/Assets/Scripts/其他/UIBlocker.cs

39 lines
1.1 KiB
C#
Raw Normal View History

2024-12-13 19:40:05 +08:00
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);
}
}
}
}