Find the device a player is using
Find out which input device a player used most recently, for example, to show button prompts that match it.
Players often change devices, such as from a keyboard and mouse to a gamepad. To show the right button prompts or icons, your game needs to know which device the player used last. How you find out depends on your workflow.
Find the device with the Player Input component
If you use the Player Input component:
- To get the player's current control scheme, such as
GamepadorKeyboard&Mouse, refer toPlayerInput.currentControlSchemefor detailed information. - To get the devices paired with the player, refer to
PlayerInput.devices. - To find out when the control scheme changes, respond to the
OnControlsChangedmessage or theonControlsChangedevent.
For more information, refer to Select a notification behavior.
Find the device that performed an action
If you don't use the Player Input component, track the device that performs each action.
To track the device that the player most recently used to perform an action:
Create a C# script with the following code, and add it to a GameObject in your scene:
using UnityEngine; using UnityEngine.InputSystem; /// <summary> /// Example script that tracks the device that the player most recently used to perform an action. /// </summary> public class FindActiveDeviceExample : MonoBehaviour { InputDevice m_LastDevice; void OnEnable() { InputSystem.onActionChange += OnActionChange; } void OnDisable() { InputSystem.onActionChange -= OnActionChange; } void OnActionChange(object obj, InputActionChange change) { if (change != InputActionChange.ActionPerformed) return; // For action changes, obj is the InputAction that changed. var action = (InputAction)obj; var control = action.activeControl; if (control == null) return; // Pass-through actions perform on every value change, including when a // control returns to its default value. For example, a pass-through action // bound to <Pointer>/delta performs when the delta resets to zero, even if // the user didn't move the mouse. Ignore those changes. if (action.type == InputActionType.PassThrough && !control.IsActuated()) return; var device = control.device; if (device == m_LastDevice) return; m_LastDevice = device; Debug.Log("The player is now using " + device.displayName); } }The script subscribes to
InputSystem.onActionChange, which reports changes to all enabled actions. When the player performs an action, the script reads the device from the action'sactiveControl.For pass-through actions, the script ignores controls that aren't actuated, so that a control returning to its default value, such as a mouse delta resetting to zero, doesn't change the device.
Replace the
Debug.Logcall with code that updates your button prompts.
To show the right icon for each binding on the new device, refer to Display bindings.
Find the last device of a type
To get the device of a particular type that the player used most recently, use its current property, such as Gamepad.current or Keyboard.current. These properties tell you which gamepad or keyboard the player used last, but not whether the player used a gamepad or a keyboard more recently.