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

Unity新输入系统Send Messages带参数方法报错及移动值获取问题

Unity新输入系统Send Messages传参问题解决办法

方案1:改用Invoke Unity Event替代Send Messages

这是最直接的解决方式,因为Invoke Unity Event原生支持带参数的方法绑定:

  • 在PlayerInput组件的Action Map设置中,将Move行为的响应类型从Send Messages改为Invoke Unity Event
  • 在玩家控制脚本中编写带InputAction.CallbackContext参数的方法:
public void OnMove(InputAction.CallbackContext context)
{
    float moveValue = context.ReadValue<float>();
    // 这里使用moveValue(±1或0)处理水平移动逻辑
}
  • 将该方法拖拽到Invoke Unity Event的绑定列表中,即可正常接收输入数值。

方案2:保留Send Messages,通过变量缓存输入值

如果必须使用Send Messages,可以提前缓存移动输入值,再用无参数方法读取:

  • 在控制脚本中缓存输入值并监听Action状态:
private float _currentMoveInput;
private PlayerInput _playerInput;

void Awake()
{
    _playerInput = GetComponent<PlayerInput>();
    var moveAction = _playerInput.actions["Move"];
    // 输入触发时更新缓存值
    moveAction.performed += ctx => _currentMoveInput = ctx.ReadValue<float>();
    // 输入取消时重置为0
    moveAction.canceled += ctx => _currentMoveInput = 0f;
}
  • 编写无参数的OnMove方法,直接使用缓存的输入值:
public void OnMove()
{
    // 使用_currentMoveInput处理移动逻辑
}

方案3:全局监听PlayerInput的onActionTriggered事件

通过统一监听所有输入事件,绕过Send Messages的参数限制:

private PlayerInput _playerInput;

void Awake()
{
    _playerInput = GetComponent<PlayerInput>();
    _playerInput.onActionTriggered += HandleAllInputActions;
}

private void HandleAllInputActions(InputAction.CallbackContext context)
{
    switch(context.action.name)
    {
        case "Move":
            float moveValue = context.ReadValue<float>();
            // 处理水平移动
            break;
        case "Jump":
            // 处理跳跃逻辑
            break;
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 20:10:08