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

Unity Input System UI模块回调无法触发,咨询模块使用方法

Unity UI输入系统回调触发问题解决方法

Unity的UI输入模块(Input System UI Module)和普通Input Action的触发逻辑存在差异,直接按普通InputAction的方式绑定OnCancel回调无法触发,需按以下步骤正确配置:

  • 确认UI输入模块与EventSystem配置

    1. 打开Project Settings → Input System Package,确保已勾选"UI Module"。
    2. 场景中必须存在EventSystem对象,且该对象挂载的是Input System UI Input Module组件(替换旧版的Standalone Input Module)。
  • 正确绑定Cancel事件的两种方式
    方式一:通过UI组件直接绑定
    在Inspector面板中选中目标UI元素(如InputField、Button),找到对应的"On Cancel"事件栏,拖拽挂载了OnCancel方法的脚本对象,选择该方法完成绑定。

    方式二:通过代码全局监听
    获取场景中的InputSystemUIInputModule实例,订阅其Cancel动作的performed事件:

    using UnityEngine;
    using UnityEngine.InputSystem.UI;
    
    public class UIController : MonoBehaviour
    {
        private InputSystemUIInputModule _uiInputModule;
    
        void Awake()
        {
            _uiInputModule = FindObjectOfType<InputSystemUIInputModule>();
            _uiInputModule?.cancel.action.performed += OnCancel;
        }
    
        void OnDestroy()
        {
            _uiInputModule?.cancel.action.performed -= OnCancel;
        }
    
        void OnCancel(InputAction.CallbackContext context)
        {
            // 执行你的操作逻辑
            Debug.Log("Cancel事件已触发");
        }
    }
    
  • 检查输入动作绑定
    确认Cancel对应的输入动作已正确绑定触发键(如Escape键):

    • 若使用默认UI输入动作,可在InputSystemUIInputModule的Inspector面板中查看"Cancel"动作的绑定。
    • 若使用自定义Input Action Asset,需将其关联到InputSystemUIInputModule的"Actions"字段,并确保Cancel动作的输入绑定正确。
  • 关键注意点
    UI输入事件需要有激活的UI对象(如InputField获得焦点、Button被选中)才会触发,确保当前场景中有UI元素处于可交互的激活状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 22:21:35