Unity Input System: How to Pair Keyboard AND Mouse to the same InputUser

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

Convert Unity UI Screen Space position to World position

All credit goes to tosiabunio over on this forum post for this great solution.

Using Unity’s Canvas UI is excellent when you want your UI elements to scale and position correctly across different devices and aspect ratios. Learning to work with the Canvas UI properly is a bit of a nightmare but that’s a different story.

My issue was that while I do want UI elements to reposition across different aspect ratios I also want some GameObjects to have animations that seemingly interact with the UI elements. For instance, I wanted the Deck and Discard piles (UI Elements in Screen Space) to have Cards (Game Objects in World Space) come in/out of them.

GameObject Cards in World Space coming out of UI element Images in Screen Space. Works even as the UI elements move due to aspect ratio changes.

The problem is that you cannot simply have your World Space objects get the transform position of the Screen Space UI objects, the positions will not translate properly and you’ll end up with your World Space objects flying off the screen. This is where Tosiabunio’s solution comes into play when you want to ” Translate anchored overlay UI element to world position. “

var screenToWorldPosition = Camera.main.ScreenToWorldPoint(rectTransform.transform.position);

Basically, all you have to do is use the ScreenToWorldPoint method and pass it the desired UI object’s transform.position. It’s a simple one liner but this solution had evaded me for months where I was doing all kinds of crazy hacks trying to get these two positioning systems to play nice together.

Tosiabunio’s post also mentions a way to convert in the opposite direction, World Space into Screen Space:

Translate game object position from the world to overlay position in order to place UI element near that object.

To which he suggests:

RectTransformUtility.WorldToScreenPoint(Camera.main, obj.transform.position)

I haven’t tried this yet but it might be handy for others. Also, user karma0413 points out that WorldToScreenPoint can be slightly off in some situations and offers a solution which involves performing position calculations off the scale factor. Again, I haven’t tried it yet as the original one liner works perfectly for my purposes.

Let me know if this helps you and if you have any other Unity Canvas issues that you need help resolving. I’ve been working with it a lot lately and perhaps I can help or write more about common issues people face.