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

Unity新输入系统UI响应问题:点击UI时角色误触发跳跃

解决Unity新输入系统手机端点击UI误触角色跳跃的问题

问题根源

你在InputAction回调里调用EventSystem.current.IsPointerOverGameObject()触发警告的核心原因是:InputAction的回调时机早于EventSystem处理当前帧UI事件的时机,此时调用该方法会读取上一帧的UI状态,Unity因此抛出提示。

两种可行解决方案

方案1:将输入响应逻辑移到Update中处理

把输入触发状态存在变量里,在Update阶段(此时EventSystem已处理完当前帧UI事件)再判断是否执行跳跃,避免读取旧状态:

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerJump : MonoBehaviour
{
    private InputAction _tapAction;
    private bool _isTapTriggered;

    void Awake()
    {
        // 初始化屏幕点击输入动作
        _tapAction = new InputAction(type: InputActionType.Button, binding: "<Touchscreen>/primaryTap");
        // 标记点击触发状态
        _tapAction.performed += ctx => _isTapTriggered = true;
        _tapAction.canceled += ctx => _isTapTriggered = false;
    }

    void OnEnable() => _tapAction.Enable();
    void OnDisable() => _tapAction.Disable();

    void Update()
    {
        if (_isTapTriggered)
        {
            // 此时判断的是当前帧最新UI状态,无警告
            if (!EventSystem.current.IsPointerOverGameObject())
            {
                Jump();
            }
            _isTapTriggered = false; // 重置标记防止重复触发
        }
    }

    void Jump()
    {
        // 替换为你的角色跳跃逻辑
        Debug.Log("角色执行跳跃");
    }
}

方案2:利用新输入系统的UI拦截交互

直接在InputAction绑定中配置UI拦截,无需手动写判断逻辑:

  • 打开你的Input Actions配置文件(.inputactions)
  • 找到屏幕点击对应的绑定项,在右侧Interactions列表点击+,选择UI Blocking
  • 保存配置后,点击UI元素时该输入动作会被自动拦截,不会触发跳跃回调,同时不影响UI自身的暂停逻辑

额外说明

方案2更贴合新输入系统的设计逻辑,无需额外代码;方案1适合需要自定义交互规则的场景。两种方法都能解决点击UI时角色误跳的问题,同时消除警告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 03:10:03