如何在新的Unity输入系统中映射到KeyCode.JoystickButton0?

时间:2020-05-13 18:46:25

标签: unity3d android-tv amazon-fire-tv

我正在研究Android TV和Fire TV的Unity项目,该项目利用了v2019.3。中发布的新输入系统。

Fire TV's button mappings在旧系统中如下:

Back                    KeyCode.Escape
Select (D-Pad Center)   KeyCode.JoystickButton0
Left (D-Pad)            KeyCode.LeftArrow
Right (D-Pad)           KeyCode.RightArrow
Up (D-Pad)              KeyCode.UpArrow
Down (D-Pad)            KeyCode.DownArrow

在新系统中,我成功地将Select / D-Pad Center之外的所有内容与以下绑定路径进行了映射:

Escape [Keyboard]
Right Arrow [Keyboard]
Left Arrow [Keyboard]
Up Arrow [Keyboard]
Down Arrow [Keyboard]

如果我映射到Any Key [Keyboard]并实现以下代码作为回调,它将显示为key: JoystickButton0,与Amazon的文档相匹配,但是在键盘下的新系统中该选项不存在绑定路径:

public void DebugKey(InputAction.CallbackContext context)
{
    foreach(KeyCode vKey in System.Enum.GetValues(typeof(KeyCode))){
         if(Input.GetKey(vKey)){
             Debug.Log ("key: " + vKey);
         }
    }
}

我尝试了Gamepad,Android Gamepad,Joystick和Android Joystick下的各种按钮,但均未成功。另一个奇怪的事情是,在没有任何绑定的情况下,Center / D-Pad在UI按钮上可以正常工作。

使用新的输入系统映射到旧版KeyCode.JoystickButtonX命名约定的正确方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

在新系统中,设备被读取为 HID ,因此KeyCode无效,因此,如果要读取操纵杆按钮的状态,可以执行以下操作:

using UnityEngine.InputSystem;


public Joystick joy;

void Start()
{
     joy = Joystick.current;
}

void FixedUpdate()
{
    var button2 = joy.allControls[2].IsPressed();
    if (button2)
    {
        Debug.Log("Button2 of current joystick is pushed");
    }
}

如果要映射button0(在旧的输入系统中),则现在映射为button1或触发器

var button1 = joy.allControls[1].IsPressed();

var button1 = joy.trigger.IsPressed();

您还可以测试更多摇杆的按钮。

void Start()
{
    var ListOfJoys = Joystick.all;
     joy = Joystick.current;//here current joy is ListOfJoys[1]
     otherjoy = ListOfJoys[0];
}


    var Buttons = joy.allControls;


    if ((Buttons[2] as ButtonControl).wasPressedThisFrame)
    {
        Debug.Log("b2 pressed during this frame");
    }
    if ((Buttons[2] as ButtonControl).wasReleasedThisFrame)
    {
        Debug.Log("b2 released during this frame");
    }
    if (joy.trigger.wasPressedThisFrame)
    {
        Debug.Log("trig or button1 pressed during this frame");
    }
    if (joy.trigger.wasReleasedThisFrame)
    {
        Debug.Log("trig or button1 released during this fram");
    }
    if (otherjoy.trigger.wasPressedThisFrame)
    {
        Debug.Log("otherjoy trigger");
    }

另一种方法是使用新系统的映射,我已将动作press Only Test1映射到操纵杆的button3

enter image description here

简化了代码,您可以将事件和直接测试混在一起:

//Newinputsystem is a name of class generated 
private Newinputsystem playerAction;

void Awake()
{
    playerAction = new Newinputsystem();
    playerAction.Player.Test1.performed += ctx => FireTest();
}

void FixedUpdate()
{
    if (playerAction.Player.Test1.triggered)
    {
        Debug.Log("fire!!!");
    }
}

public void FireTest()
{
    Debug.Log("i am in firetest");
}

private void OnEnable()
{
    playerAction.Enable();
}
private void OnDisable()
{
    playerAction.Disable();
}

因此您可以添加新的动作test2,该动作将由button3的动作release only触发...

要显示HID设备,请进入菜单窗口/分析/输入调试器

您必须查看自己的设备(这里是hotas)

enter image description here

然后单击它,并在标签项“ HID描述符”中

enter image description here

所以我想您已经在项目设置中选择了设备:

enter image description here