Unity’s new Input System is cool but the documentation needs a lot of work. Most tutorials on the new Input System are super basic and rely on the PlayerInput and PlayerInputManager components to do the magic of syncing users and control schemes which won’t work if you’re doing more custom gameplay or multiplayer setups.
So digging deeper you may have found the
InputUser.PerformPairingWithDevice(...) method which will allow you to manually create a InputUser paired with a device like a gamepad. Which works great, however what is not obvious is how you pair 2 devices with a UserInput, like with the Keyboard AND the Mouse!
Well, after days of googling and banging my head against the wall I finally found the answer in a semi-unrelated forum thread:
var controls = new MyControls(); var user = InputUser.PerformPairingWithDevice(Keyboard.current); InputUser.PerformPairingWithDevice(Mouse.current, user: user); // this is the key, non-intuitive part user.AssociateActionsWithUser(controls); user.ActivateControlScheme("KeyboardMouse"); controls.Enable();
So you still do the initial instantiation/pairing with one of the devices (the Keyboard in this case) and then follow it up with another call to
PerformPairingWithDevice() this time passing the Mouse and the
user that was just created.
This feels unfinished to me. It seems an odd process to pair two devices like this. Instead I wish you could pass in an array of devices when first instantiating the InputUser.
Hope others that struggle with this are able to find the answer faster now! Here’s the thread I found the solution: https://forum.unity.com/threads/solved-can-the-new-input-system-be-used-without-the-player-input-component.856108/#post-5669128
2 thoughts on “Unity Input System: How to Pair Keyboard AND Mouse to the same InputUser”
THANK YOU, HOLY CRAP!
I am not sure if this was the case when you originally made the tutorial, but now you can instantiate an object with an array of devices with the following function:
PlayerInput Instantiate(GameObject prefab, int playerIndex = -1, string controlScheme = null, int splitScreenIndex = -1, params InputDevice pairWithDevices);