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

如何使用Unity New Input System调试按键名称并监听所有输入?

Unity新输入系统运行时监听所有输入并打印按键名称

方法一:全局输入回调(推荐)

直接通过Input System的全局事件监听所有输入动作,无需提前绑定具体控件:

using UnityEngine;
using UnityEngine.InputSystem;

public class InputDebugger : MonoBehaviour
{
    private void OnEnable()
    {
        // 监听所有按键按下事件
        InputSystem.onAnyButtonPress += OnAnyButtonPressed;
        // 监听所有值变化事件(适配摇杆、滑动条等持续输入)
        InputSystem.onAnyValueChange += OnAnyValueChanged;
    }

    private void OnDisable()
    {
        // 取消监听,避免内存泄漏
        InputSystem.onAnyButtonPress -= OnAnyButtonPressed;
        InputSystem.onAnyValueChange -= OnAnyValueChanged;
    }

    private void OnAnyButtonPressed(InputControl control)
    {
        // 打印控件多维度信息,优先看displayName和path(适配Android等平台的名称差异)
        Debug.Log($"按键按下:控件名={control.name},显示名={control.displayName},路径={control.path},设备={control.device.name}");
    }

    private void OnAnyValueChanged(InputControl control)
    {
        if (control is AxisControl axis)
        {
            Debug.Log($"轴输入变化:控件名={control.name},当前值={axis.ReadValue()},路径={control.path}");
        }
    }
}

方法二:遍历设备监听单个控件

如果需要更精细化的控制,可以遍历所有已连接设备,给每个可交互控件单独绑定监听:

using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections.Generic;

public class DeviceInputDebugger : MonoBehaviour
{
    private List<InputAction> registeredActions = new List<InputAction>();

    private void OnEnable()
    {
        // 监听设备连接/移除事件
        InputSystem.onDeviceChange += OnDeviceChange;
        // 初始化已连接设备的监听
        RegisterAllDevices();
    }

    private void OnDisable()
    {
        InputSystem.onDeviceChange -= OnDeviceChange;
        UnregisterAllActions();
    }

    private void RegisterAllDevices()
    {
        foreach (var device in InputSystem.devices)
        {
            RegisterDeviceControls(device);
        }
    }

    private void RegisterDeviceControls(InputDevice device)
    {
        // 筛选出按键、轴类可交互控件
        foreach (var control in device.allControls)
        {
            if (control is ButtonControl || control is AxisControl)
            {
                var action = InputAction.Create(control);
                action.performed += ctx =>
                {
                    Debug.Log($"输入触发:控件={control.displayName},值={ctx.ReadValue<float>()},设备={device.name}");
                };
                action.Enable();
                registeredActions.Add(action);
            }
        }
    }

    private void UnregisterAllActions()
    {
        foreach (var action in registeredActions)
        {
            action.Disable();
            action.Dispose();
        }
        registeredActions.Clear();
    }

    private void OnDeviceChange(InputDevice device, InputDeviceChange change)
    {
        switch (change)
        {
            case InputDeviceChange.Added:
                RegisterDeviceControls(device);
                break;
            case InputDeviceChange.Removed:
                UnregisterAllActions();
                RegisterAllDevices();
                break;
        }
    }
}

关键注意事项

  • Android等平台的控件实际名称通常存于displayName或path字段,调试时优先查看这两个值,而非name
  • 必须在目标平台真机测试,编辑器内的控件名称可能和真机存在差异
  • 务必在OnDisable中清理所有监听事件,避免内存泄漏

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 16:10:12