Search Unity

Buttons in custom controller trigger events but do not set the value

Discussion in 'Input System' started by jamesvhyde, Jul 15, 2020.

  1. jamesvhyde

    jamesvhyde

    Joined:
    May 31, 2017
    Posts:
    6
    I have one of those cheap one-hand controllers that I would like to get to work in Unity. I have been looking at the InputSystem examples and Reference, and I almost have it working. The controller generates events when the user hits the buttons on it, but I can't get the values from the buttons so that I can tell which button was pressed.

    Here is the code I have so far:
    Code (CSharp):
    1. public struct OneHandDeviceState : IInputStateTypeInfo
    2. {
    3.      public FourCC format => new FourCC('O', 'H', 'C', 'F');
    4.      public ushort unk1;
    5.      public ushort unk2;
    6.      [InputControl(name = "okButton", layout = "Button", bit = 12, displayName = "OK Button")]
    7.      public ushort buttons;
    8. }
    9. #if UNITY_EDITOR
    10. [InitializeOnLoad] // Call static class constructor in editor.
    11. #endif
    12. [InputControlLayout(stateType = typeof(OneHandDeviceState))]
    13. public class OneHandController : InputDevice
    14. {
    15. #if UNITY_EDITOR
    16.      static OneHandController()
    17.      {
    18.          // Trigger our RegisterLayout code in the editor.
    19.          Initialize();
    20.      }
    21. #endif
    22.      [RuntimeInitializeOnLoadMethod]
    23.      private static void Initialize()
    24.      {
    25.          InputSystem.RegisterLayout<OneHandController>("Fortune Tech",
    26.              matches: new InputDeviceMatcher()
    27.                  .WithInterface("HID")
    28.                  .WithProduct("Fortune Tech Wireless"));
    29.          Debug.Log("Registered Fortune Tech.");
    30.      }
    31.      public ButtonControl okButton { get; private set; }
    32.      protected override void FinishSetup()
    33.      {
    34.          base.FinishSetup();
    35.          okButton = GetChildControl<ButtonControl>("okButton");
    36.      }
    37. }
    I can tell this is working. The Input Debugger shows events coming in, and if I examine the events, I can see the okButton value is read correctly from the data. See in this screenshot how the value is different:


    However, it does not appear in the live part of the Input Debugger:


    This is different from the keyboard, for example, which correctly shows the value changing.

    The same problem shows up in-game, if I use code like this:
    Code (CSharp):
    1. public class Movement : MonoBehaviour
    2. {
    3.      void Update()
    4.      {
    5.          var controller = OneHandController.current;
    6.          var keyboard = Keyboard.current;
    7.          ButtonControl keyButton = keyboard["#(a)"] as ButtonControl;
    8.          ButtonControl conButton = controller.okButton;
    9.          if (keyButton.ReadValue() != 0)
    10.              Debug.Log("key a pressed");
    11.          if (conButton.ReadValue() != 0)
    12.              Debug.Log("OK button pressed");
    13.      }
    14. }
    The keyboard button has a value when the key is pressed, but for the controller, ReadValue always comes out 0. Similarly, if I use code like this, ReadValueFromEvent returns 0, even though the event is being triggered.

    It seems like I am missing one thing to make the Device correctly map the values from the Device State into the Input Controls. What is it?