NG_/Scripts/其他/UIBlocker.cs
2024-12-13 18:49:44 +08:00

39 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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