UI和OnPointerClick 的重叠问题
如果写了 onPointer或者 用了Input.GetMouseDown()等方法,就很容易造成和UI的事件冲突问题,那么有没有简单的代码解决呢,
有的
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
void Update()
{
// UI遮罩判断* 兼容旧输入系统 (Input.GetMouseButtonDown) 和 新输入系统
bool isClicked = false;
#if ENABLE_INPUT_SYSTEM
if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
isClicked = true;
#else
if (Input.GetMouseButtonDown(0))
isClicked = true;
#endif
if (isClicked)
{
// 【关键代码】如果指针指向了 UI 对象,则直接返回,不执行后续的 3D 点击逻辑
if (EventSystem.current.IsPointerOverGameObject())
{
订阅专栏 解锁全文
356




被折叠的 条评论
为什么被折叠?



