Getting a human-readably entry (like a key) from your Input Map in Unity3D

I am using the „new“ Input System of Unity, which I really like.

For my Interaction Project I needed the mapping of an action in a human readable form.

First add

using UnityEngine.InputSystem;

Then, in your class where you need it, for example a config dialog, or in my case, to show the correct key for an action on an Actionable Object when the player looks on it.

	public PlayerInput playerInput;

	public InputActionMap actionMap;

You can either drop the PlayerInput component in your Game Gameobject into it in the Inspector or just let it be initialized in the Start method.

Also in the Start() method :

		if (playerInput == null)
			playerInput  = GetComponent<PlayerInput>();

		actionMap = playerInput.currentActionMap;

        

        ReadOnlyArray<InputBinding> actions = playerInput.actions["Interaction1"].bindings;

		foreach(InputBinding binding in actions)
        {
			Debug.Log(binding .ToDisplayString());
        }

Interaction1 is the action I am looking for. I then iterate through all the bindings and display them as a readable string. (In this case I only have one binding though)

The action is mapped to the keyboard key E, so I get a plain and nice E as result, which is exactly what I needed.

References :

Unity3D Input System Reference

Unity3D Input System Reference – ToDisplayString

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.