Hello together, i have a question is there a way to find out which control scheme is currently being used or which input device, i want to switch ui icons. I know IT works with PlayerInput but i use the Generated c# Class Option.
Thanks
Hello together, i have a question is there a way to find out which control scheme is currently being used or which input device, i want to switch ui icons. I know IT works with PlayerInput but i use the Generated c# Class Option.
Thanks
You can get it by referencing the InputUser like this:
InputUser user = InputUser.PerformPairingWithDevice(device);
user.ActivateControlScheme("Gamepad");
// This will print 'Gamepad(<Gamepad>)'
print(user.controlScheme);
// This will print 'Gamepad'
print(user.controlScheme.Value.name);
What I like to do is create a ScriptableObject so that I can easily customize the ui that is shown like this:
[CreateAssetMenu(menuName = "Control Scheme", fileName = "New Control Scheme")]
public class ControlScheme : ScriptableObject
{
[SerializeField] string displayName;
[SerializeField] Sprite icon;
public string Name => name;
public string DisplayName => displayName;
public Sprite Icon => icon;
}
Hey Thanks for the reply well i think the InputUser is only working with a PlayerInput in the Scene ?
I don’t completely understand but I am assuming you are referring to the PlayerInput component. The InputUser API, as far as I know is not related to it. You can do it completely through code as is explored in this thread: Manual Local Multiplayer Using InputDevices
You can get the current control scheme in the PlayerInput component using: PlayerInput.currentControlScheme
idk is this correct or not but it works for now maybe someone has a better solution for me but for now i can detect if a deveice has changed and i can send a event to change my ui icons
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Users;
public class Test : MonoBehaviour {
[Header("References")]
[SerializeField] private InputActionAsset inputActions;
[Header("Broadcasting Events")]
[SerializeField] private ControlSchemeEventChannelSO controlSchemeEvent;
private ControlScheme currentControlScheme;
private InputUser inputUser;
private void Awake() {
// On Awake we set Keybaord & Mouse as Default Devices
inputUser = InputUser.PerformPairingWithDevice(Keyboard.current);
InputUser.PerformPairingWithDevice(Mouse.current, user: inputUser);
inputUser.AssociateActionsWithUser(inputActions);
inputUser.ActivateControlScheme("Keybaord & Mouse");
currentControlScheme = ControlScheme.MouseKeyboard;
++InputUser.listenForUnpairedDeviceActivity;
InputUser.onUnpairedDeviceUsed += OnUnpairedDeviceUsed;
}
private void OnUnpairedDeviceUsed(InputControl inputControl, InputEventPtr inputEventPtr) {
// Chache CurrentControlScheme
ControlScheme cacheControlScheme = currentControlScheme;
// Get the Unpaired Device
InputDevice inputDevice = inputControl.device;
// Check if the Unpaired Device is Gamepad
if (inputDevice is Gamepad) {
// Unpaire all Devices
inputUser.UnpairDevices();
// Pair Current Gamepad
InputUser.PerformPairingWithDevice(Gamepad.current, user: inputUser);
// Set ControlScheme to Gamepad
inputUser.ActivateControlScheme("Gamepad");
currentControlScheme = ControlScheme.Gamepad;
}
// Check if the Unpaired Device is Mouse or Keybaord
if (inputDevice is Mouse || inputDevice is Keyboard) {
// Unpaire all Devices
inputUser.UnpairDevices();
// Pair Current Mouse & Keyboard
InputUser.PerformPairingWithDevice(Keyboard.current, user: inputUser);
InputUser.PerformPairingWithDevice(Mouse.current, user: inputUser);
// Set ControlScheme to Keybaord & Mouse
inputUser.ActivateControlScheme("Keybaord & Mouse");
currentControlScheme = ControlScheme.MouseKeyboard;
}
// Send Event to UI Elements to Change Icons if the ControlScheme has changed
if (cacheControlScheme != currentControlScheme) {
controlSchemeEvent.RaiseEvent(currentControlScheme);
}
}
}
public enum ControlScheme { MouseKeyboard, Gamepad }