Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Filtering input actions by control type

Discussion in 'Input System' started by JamhammerDev, Oct 30, 2019.

  1. JamhammerDev


    Jul 11, 2015
    I have two implementations for showing the correct ui elements based on the currently used controller. The first works as intended, but the second method is incorrect. I'm wondering if there is a more efficient way of doing what i'd like, would appreciate some input!

    Working method
    Below, Type is a reference to the inputactionreference that's attached to the label, example ( the join action ). I have two control types, keyboard and gamepad. I loop through the control schemes to find the gamepad and keyboards, and then use that i variable for to show the correct binding and path.

    Code (CSharp):
    2. public InputActionReference[] inputActionReference;
    3.  for (int i = 0; i < type.asset.controlSchemes.Count; i++)
    4.             {
    5.                 if(type.asset.controlSchemes[i].bindingGroup.Contains("Gamepad"))
    6.                     label.lblGamepadTxt.text = "<sprite name=" + InputControlPath.ToHumanReadableString(type.action.bindings[i].path, InputControlPath.HumanReadableStringOptions.OmitDevice) + ">";
    7.                 else if(type.asset.controlSchemes[i].bindingGroup.Contains("Keyboard"))
    8.                     label.lblKeyboardTxt.text = InputControlPath.ToHumanReadableString(type.action.bindings[i].path, InputControlPath.HumanReadableStringOptions.OmitDevice).ToUpper();
    9.             }
    Non-Working method ( shows gamepad instead of keyboard )
    Below, I get the current control scheme index and pass that into the bindings array, but i find it gives me the wrong scheme. Thoughts?

    Code (CSharp):
    2. public InputActionReference inputActionReference;
    3.  if (myPlayer.GetComponentInParent<PlayerInput>().currentControlScheme.Contains("Gamepad"))
    4.         {
    5.             joinObj.GetComponentInChildren<TextMeshPro>().text =
    6.                 "<sprite name=" + InputControlPath.ToHumanReadableString(inputActionReference.action.bindings[getCurrentControlSchemeIndex()].path,
    7.                 InputControlPath.HumanReadableStringOptions.OmitDevice) + ">";
    8.         }
    9.         else if (myPlayer.GetComponentInParent<PlayerInput>().currentControlScheme.Contains("Keyboard"))
    10.         {
    11.             joinObj.GetComponentInChildren<TextMeshPro>().text =
    12.                 InputControlPath.ToHumanReadableString(inputActionReference.action.bindings[getCurrentControlSchemeIndex()].path,
    13.                 InputControlPath.HumanReadableStringOptions.OmitDevice);
    14.         }
    16.     int getCurrentControlSchemeIndex()
    17.     {
    18.         int index = inputActionReference.asset.FindControlSchemeIndex(myPlayer.GetComponentInParent<PlayerInput>().currentControlScheme);
    19.         return index;
    20.     }
  2. Rene-Damm


    Sep 15, 2012
    If you're looking at an already joined player complete with a control scheme and device(s), the most straightforward way is probably to simply grab the display name from the actual control.

    Code (CSharp):
    1. inputActionReference.action.controls[0].displayName
    If the control scheme switches, you're still displaying the right thing. Also, it'll allow automatically distinguishing between Xbox and PS4 controllers.

    Will need some fallback and also can't deal with composites and the like where there's more than one control feeding into the action. Post 1.0, all this should become much easier.
  3. JamhammerRob


    Jan 15, 2016
    Thanks Rene!

    Ended up using that code elsewhere during play, but used the code above to produce this piece. Really helps with this new system, thanks for all the hard work.

  4. Rene-Damm


    Sep 15, 2012
    Looks great!