I have an action Fire, which is Left Mouse Button if player is using Mouse or the X button on XBox Controller.
I need to show in the UI a Hint to press this button. How to get the current “button description” that should be pressed according to the input method the player is using now?
// Call GetBindingDisplayString() such that it also returns information about the
// name of the device layout and path of the control on the device. This information
// is useful for reliably associating imagery with individual controls.
var bindingString = action.GetBindingDisplayString(out deviceLayout, out controlPath);
// If it's a gamepad, look up an icon for the control.
Sprite icon = null;
if (!string.IsNullOrEmpty(deviceLayout)
&& !string.IsNullOrEmpty(controlPath)
&& InputSystem.IsFirstLayoutBasedOnSecond(deviceLayout, "Gamepad"))
{
switch (controlPath)
{
case "buttonSouth": icon = aButtonIcon; break;
case "dpad/up": icon = dpadUpIcon; break;
//...
}
}
// If you have an icon, display it instead of the text.
var text = m_RebindButton.GetComponentInChildren<Text>();
var image = m_RebindButton.GetComponentInChildren<Image>();
if (icon != null)
{
// Display icon.
text.gameObject.SetActive(false);
image.gameObject.SetActive(true);
image.sprite = icon;
}
else
{
// Display text.
text.gameObject.SetActive(true);
image.gameObject.SetActive(false);
text.text = bindingString;
}
I’m probably being dumb here, but I can’t get this to work. It seems I’m missing the matching overload method for GetBindingDisplayString(out deviceLayout, out controlPath);.
The closest I can see is this:
public static string GetBindingDisplayString(this InputAction action, int bindingIndex, out string deviceLayoutName, out string controlPath, InputBinding.DisplayStringOptions options = 0);
As this is an example from the documentation I assume there must be a way to make it work.
The project is in Unity 2019.4.28 and the Input System is version 1.0.2.
Tested in a new empty project in case it was something wrong with the imported Input System package but had the same problem.
It shows this as part of an example for displaying bindings as images:
// Call GetBindingDisplayString() such that it also returns information about the
// name of the device layout and path of the control on the device. This information
// is useful for reliably associating imagery with individual controls.
var bindingString = action.GetBindingDisplayString(out deviceLayout, out controlPath);
but as far as I can tell, this extension method doesn’t exist. Maybe this got removed at some point but this part of the documentation wasn’t updated?
var deviceLayout = default(string);
var controlPath = default(string);
InputAction action = m_Action.action;
var currentControlScheme = InputUser.all[0].controlScheme;
var bindingIndex = action.GetBindingIndex(currentControlScheme.Value.bindingGroup);
var bindingString = action.GetBindingDisplayString(bindingIndex, out deviceLayout, out controlPath);
I get the current control scheme from the first user, and use that to get the binding index. I’m not sure how safe it is to assume that the first user will always be the correct one though. I’m making a single player game, so maybe it’s okay?
Anyway, testing in the editor and on a quick dev build to my Android device it seems to be working.
Same. The documentation on the InputSystem is a mess. Its so hard to follow. Everytime you want to do something slightly more advanced, you find nothing. Why is it so hard to have a InputAction.currentActiveBinding ???
Hi, for anyone who wants to add a similar feature. I created an asset that allows you to display input bindings in TextMeshPro texts side by side with other text. It makes use of the style tag of TMPro to display sprites/text for the current input bindings. There is a manager that subscribes to the “Player Input” component’s ControlsChangedEvent and automatically updates the TMPro style sheet and the TMPro texts to display sprites if the player switches to a different device (Keyboard/Mouse or Gamepads).
All you need to do is type <style=NameOfActionMap/NameOfAction> into a TMPro text field and it will display the current binding(s) for that action.
The asset also contains prefabs which you can use to rebind the actions of the InputActionAssets.