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

问题描述 投票:0回答:1

我正在为Android TV和Fire TV做一个Unity项目,利用v2019.3中发布的新输入系统。

Fire TV的按钮映射 旧系统中的绑定路径如下。

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

我已经成功地映射了除了SelectD -Pad中心以外的所有东西 在新系统中,我的绑定路径如下。

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

如果我映射到 Any Key [Keyboard] 并实现以下代码作为回调,它显示为 key: JoystickButton0,这与亚马逊的文档相吻合,但在新系统中,键盘绑定路径下并没有作为一个选项存在。

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下的各种按钮,都没有成功。另外一个奇怪的事情是,CenterD-Pad在UI Buttons上可以正常使用,没有任何绑定。

请问用新的输入系统映射到传统的KeyCode.JoystickButtonX命名规范的正确方法是什么?

谢谢!我正在做一个Unity的项目,我想请教一下,如何将传统的KeyCode.JoystickButtonX与新的输入系统进行映射?

unity3d android-tv amazon-fire-tv
1个回答
0
投票

在新系统中,设备读作 HID如果你想读取操纵杆按钮的状态,你可以这样做。

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");
    }
}

如果你想映射按钮0(在旧的输入系统中),现在是按钮1或触发器。

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 测试1到棒子的按钮3

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 它将会被动作所触发 release only 对于按钮3...

© www.soinside.com 2019 - 2024. All rights reserved.