如何在Unity新输入系统中使用SendMessage行为并传递InputAction参数?

huangapple go评论55阅读模式
英文:

How can I use the SendMessage behaviour in Unity new input system and pass the InputAction parameter?

问题

我正在开发一个Unity游戏(使用Unity 2022.2.20)。
我想要在Send Messages行为中使用新的输入系统。当我执行OnJump方法时,它运行得很好,但是当我尝试使用需要InputAction参数的OnMove方法时,Unity会抛出MissingMethodException

我认为这是因为在使用Send Messages行为时,Unity期望方法不带参数声明。

玩家只能在水平方向上移动,所以我在新输入系统上配置了一个1D轴。
如何在Unity新输入系统中使用SendMessage行为并传递InputAction参数?

这是我的代码:

public class ControllManager : MonoBehaviour
{

    // Working as expected
    public void OnJump()
    {
        Debug.Log("Jump");
    }

    // Not working (MissingMethodException)
    public void OnMove(InputAction cc)
    {
        Debug.Log(cc.ReadValue<float>());
    }
}

我想要移除InputAction参数,但我需要知道玩家是否按下了左键或右键(作为int/float)。

非常感谢!

我尝试使用InputAction.CallbackContext作为参数类型,但它一直失败。
我期望能够知道事件是否以正数(1)或负数(-1)触发。因为如果我删除参数,触发事件是可能的,但我将无法知道用户是否按下A键以左移还是按下D键以右移。

英文:

I am developing a Unity Game (using Unity 2022.2.20).
I want to use the new Input System with the Send Messages behaviour. It works fine when I do the OnJump method, but when I try to use the OnMove method (that requires a InputAction parameter) Unity throws a MissingMethodException.

I think this is because when using the Send Messages behaviour, Unity expects the method to be declared without parameters.

The player is only allowed to move Horizontally, so I configured a 1D Axis on the new input system.
如何在Unity新输入系统中使用SendMessage行为并传递InputAction参数?

This is my code:

public class ControllManager : MonoBehaviour
{

    // Working as expected
    public void OnJump()
    {
        Debug.Log(&quot;Jump&quot;);
    }

    // Not working (MissingMethodException)
    public void OnMove(InputAction cc)
    {
        Debug.Log(cc.ReadValue&lt;float&gt;());
    }
}

I would remove the InputAction parameter, but I need to know if the player pressed left or right (as a int/float).

Thank you a lot!

I tried to use InputAction.CallbackContext as the parameter type, but it keep failing.
I expect to be able to know if the event is triggered as positive (1) or negative(-1). Because triggering the event is possible if I remove the parameter, but I won't be able to know if the user pressed the A for left or D for right.

答案1

得分: 1

你需要为参数使用 InputValue 类型。

    public void OnMove(InputValue cc)
    {
        Debug.Log(cc.Get<float>());
    }
英文:

Well, I just found the solution:
You need to use the InputValue type for the parameter.

    public void OnMove(InputValue cc)
    {
        Debug.Log(cc.Get&lt;float&gt;());
    }

huangapple
  • 本文由 发表于 2023年5月25日 03:38:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定