首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在新的统一输入系统中映射到KeyCode.JoystickButton0?

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

Stack Overflow用户
提问于 2020-05-13 18:46:25
回答 2查看 5K关注 0票数 2

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

旧系统中的Fire的按钮映射如下:

代码语言:javascript
复制
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在新系统中使用了以下绑定路径:

代码语言:javascript
复制
Escape [Keyboard]
Right Arrow [Keyboard]
Left Arrow [Keyboard]
Up Arrow [Keyboard]
Down Arrow [Keyboard]

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

代码语言:javascript
复制
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下的各种按钮,但都没有成功。另一件奇怪的事情是,Center/D可以很好地工作在UI按钮上,不需要任何绑定。

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

谢谢!

EN

Stack Overflow用户

发布于 2021-10-16 19:35:14

为了将Amazon电视控制器与新的输入系统进行完全映射,您需要一个自定义设备。

有了这个脚本,您可以让所有的按钮工作。这也是有可能使音量上升,音量降低和穆特。但我不认为这是个好主意。

代码语言:javascript
复制
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Utilities;
using UnityEngine.Scripting;
 
#if UNITY_EDITOR
using UnityEditor;
#endif
 
[Preserve]
public struct AftvRemoteDeviceState : IInputStateTypeInfo
{
    public FourCC format => new FourCC('A', 'G', 'C');
 
    //Mapped Here
    [InputControl(name = "dPadUpButton", layout = "Button", bit = 19, displayName = "Dpad Up")]
    [InputControl(name = "dPadRightButton", layout = "Button", bit = 22, displayName = "Dpad Right")]
    [InputControl(name = "dPadDownButton", layout = "Button", bit = 20, displayName = "Dpad Down")]
    [InputControl(name = "dPadLeftButton", layout = "Button", bit = 21, displayName = "Dpad Left")]
    //[InputControl(name = "backButton", layout = "Button", bit = 4, displayName = "Back Button")]
    [InputControl(name = "menuButton", layout = "Button", bit = 82, displayName = "Menu Button")]
    //Non Maped
    [InputControl(name = "selectButton", layout = "Button", bit = 23, displayName = "Select Button")]
    [InputControl(name = "rewindButton", layout = "Button", bit = 89, displayName = "Rewind Button")]
    [InputControl(name = "playPauseButton", layout = "Button", bit = 85, displayName = "Play Pause Button")]
    [InputControl(name = "fastForwardButton", layout = "Button", bit = 90, displayName = "Fast Forward Button")]
    public uint buttons;
}
 
#if UNITY_EDITOR
[InitializeOnLoad] // Call static class constructor in editor.
#endif
[Preserve]
[InputControlLayout(displayName = "Aftv Remote", stateType = typeof(AftvRemoteDeviceState))]
public class AftvRemoteDevice : InputDevice
{
#if UNITY_EDITOR
    static AftvRemoteDevice()
    {
        Initialize();
    }
#endif
 
    [RuntimeInitializeOnLoadMethod]
    private static void Initialize()
    {
        InputSystem.RegisterLayout<AftvRemoteDevice>
            (
                matches: new InputDeviceMatcher()
                .WithInterface("Android")
                .WithDeviceClass("AndroidGameController")
                .WithProduct("Amazon Fire TV Remote")
            );
    }
 
    public ButtonControl dPadUpButton { get; private set; }
    public ButtonControl dPadRightButton { get; private set; }
    public ButtonControl dPadDownButton { get; private set; }
    public ButtonControl dPadLeftButton { get; private set; }
    public ButtonControl selectButton { get; private set; }
    public ButtonControl rewindButton { get; private set; }
    public ButtonControl playPauseButton { get; private set; }
    public ButtonControl fastForwardButton { get; private set; }
    //public ButtonControl backButton { get; private set; }
    public ButtonControl menuButton { get; private set; }
    public bool SelectButtonDown { get; set; }
    public bool RewindButtonDown { get; set; }
    public bool PlayPauseButtonDown { get; set; }
    public bool FastForwardButtonDown { get; set; }
 
    protected override void FinishSetup()
    {
        base.FinishSetup();
        dPadUpButton = GetChildControl<ButtonControl>("dPadUpButton");
        dPadRightButton = GetChildControl<ButtonControl>("dPadRightButton");
        dPadDownButton = GetChildControl<ButtonControl>("dPadDownButton");
        dPadLeftButton = GetChildControl<ButtonControl>("dPadLeftButton");
 
        rewindButton = GetChildControl<ButtonControl>("rewindButton");
        playPauseButton = GetChildControl<ButtonControl>("playPauseButton");
        fastForwardButton = GetChildControl<ButtonControl>("fastForwardButton");
 
        //backButton = GetChildControl<ButtonControl>("backButton");
        menuButton = GetChildControl<ButtonControl>("menuButton");
        selectButton = GetChildControl<ButtonControl>("selectButton");    
    }
 
    public static AftvRemoteDevice current { get; private set; }
 
    public override void MakeCurrent()
    {
        base.MakeCurrent();
        current = this;
    }
 
    protected override void OnRemoved()
    {
        base.OnRemoved();
        if (current == this)
            current = null;
    }
 
    #region Editor
#if UNITY_EDITOR
    [MenuItem("Tools/AftvRemote/Create Device")]
    private static void CreateDevice()
    {
        InputSystem.AddDevice<AftvRemoteDevice>();
    }
    [MenuItem("Tools/AftvRemote/Remove Device")]
    private static void RemoveDevice()
    {
        var customDevice = InputSystem.devices.FirstOrDefault(x => x is AftvRemoteDevice);
        if (customDevice != null)
            InputSystem.RemoveDevice(customDevice);
    }
#endif
    #endregion
}

位值是来自Android 的相同的常量值。您可以检查列表这里,您只需要将此脚本包含在项目中,设置您的输入操作和输入操作映射。

由于某些原因,播放机输入组件有问题。但是对于UI导航,这也是很好的工作,它与输入调试器工作良好。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61782490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档