You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity移动端游戏:解决Input.GetMouseDown()与UI按钮交互冲突问题

适配移动端的解决方案

你的问题根源在于IPointerEnterHandler和IPointerExitHandler是基于鼠标指针移动的事件,而移动端没有持续的鼠标指针——只有触摸点,所以这些事件在移动端几乎不会被触发(除非你在UI上滑动触摸),导致你的UI交互状态逻辑完全失效,最终按钮点击被PlayerInput的隐藏逻辑干扰。

下面给你两个实用的适配方案:

方案一:用触摸/点击事件标记UI交互状态

这个方案通过追踪UI上的触摸按下/抬起事件,来标记当前是否在和UI交互,让PlayerInput只在非UI区域点击时执行隐藏逻辑。

首先修改你的UiPointerHandler组件,换成触摸友好的事件接口:

using UnityEngine;
using UnityEngine.EventSystems;

public class UiPointerHandler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
    // 静态变量让PlayerInput可以直接读取状态
    public static bool IsInteractingWithUi { get; private set; }

    public void OnPointerDown(PointerEventData eventData)
    {
        // 触摸按下UI时标记状态
        IsInteractingWithUi = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        // 触摸抬起时重置状态
        IsInteractingWithUi = false;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        // 防止触摸滑出UI后状态没重置
        IsInteractingWithUi = false;
    }
}

然后更新PlayerInput的Update方法,加入状态判断:

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        // 只有当没有和UI交互时,才执行隐藏UI的逻辑
        if (!UiPointerHandler.IsInteractingWithUi)
        {
            // hide UI elements
        }
    }
}

方案二:直接用EventSystem判断点击是否在UI上(更简洁)

不需要额外组件,直接利用Unity的EventSystem来判断当前点击/触摸是否落在UI元素上,省去状态维护的麻烦:

using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerInput : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            bool isTouchingUi = false;
            
            // 区分移动端触摸和PC端鼠标
            if (Input.touchCount > 0)
            {
                // 移动端需要传入触摸的fingerId,否则无法正确检测
                isTouchingUi = EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId);
            }
            else
            {
                // PC端直接检测鼠标指针
                isTouchingUi = EventSystem.current.IsPointerOverGameObject();
            }

            // 非UI区域点击才执行隐藏逻辑
            if (!isTouchingUi)
            {
                // hide UI elements
            }
        }
    }
}

关键注意事项

  • 确保EventSystem配置正确:移动端需要同时有StandaloneInputModule和TouchInputModule(Unity新建UI时默认会添加,如果没有,手动在EventSystem GameObject上添加这两个组件),否则触摸事件无法被UI系统处理。
  • UI元素的Raycast Target要勾选:比如你的按钮Image组件,Raycast Target选项要保持勾选(默认是勾选的),否则EventSystem无法检测到触摸。
  • 按钮的Interactable状态:确保你的按钮Interactable属性为true,否则无法响应点击事件。

内容的提问来源于stack exchange,提问作者Don Andre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:33:36