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.

Unity Remote 5 not working: Fix

If you are attempting to use Unity Remote 5 on an Android device attached to a PC (and perhaps Mac/Linux) but upon clicking Play in Unity you game goes not show up in Unity Remote then these tips might help:

First, disconnect your Android device from your PC and shut down Unity to ensure you’re starting from a clean slate. This walkthrough also assumes you’ve completed the steps found in Unity’s documentation: https://docs.unity3d.com/Manual/UnityRemote5.html

Enable Developer Mode and USB debugging (or re-enable it)

On your Android Device, enable Developer Mode

  1. Open Settings
  2. Scroll down to and click About <Device> (Mine says About Tablet)
  3. Scroll down to the Build Number
  4. click Build Number many times (after a few times, a message will pop up telling you how many more times to click)

Now that Developer Mode is active

  1. Go to Settings>Developer Options
  2. Ensure Developer Options is toggled On (at the top of the list)
  3. Optional: Enable Stay awake 
  4. Scroll down, under Debugging enable USB Debugging

Now you device should be ready.

  1. Plug in your Android device to your PC.
  2. Open Unity Remote 5 on your Android Device
  3. Open you Unity project
  4. Click Play

You should see you Unity project on your Android device. If not, try shutting down Unity and Unity Remote and restarting each.

Change Unity’s C# Template Code Style

The first thing I used to do every time I’d edit a new C# script in Unity is delete the comments and unused usings which are all included as part of the template. Going through that activity for every new script was bad enough, but on top of that the code style used in the template is pretty far off from what is considered standard for C#. Eventually, I decided it was time to stop my pre-scripting ritual and just make the default template into what I want. So to begin:

Navigate to the ScriptTemplates folder in your Unity install folder.

Route with a typical Windows install: C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates

Open 81-C# Script-NewBehaviourScript.cs.txt in your text editor of choice.

From here you can edit the file’s layout however you choose. Mine looks like this:

using UnityEngine;

public class #SCRIPTNAME# : MonoBehaviour 
{
    private void Start() 
    {
        #NOTRIM#
    }
    
    private void Update()
    {
        #NOTRIM#
    }
}

Note: You may be unable to save/overwrite the template file if you did not open your text editor As Administrator. Don’t worry, you can just Save As to your Desktop and drag/drop the new version from the Desktop to the ScriptTemplates directory.

Event Delegation in Unity

What is Event Delegation?

“…a helper object, known as a delegate, is given the responsibility to execute a task for the delegator
– Delegation Pattern Wikipedia

Why use Event Delegation?

A good practice when working with Unity (or any framework) is to decouple your UI logic from your game logic. It’s fine to have the UI code reach down to the game logic layer and call methods directly on it but having the game logic layer directly call methods on the UI logic’s layer is, in general, a bad practice because doing so “marries” you to whatever UI you are currently using. So to get around this we can use C#’s Delegates and Events to fire off methods throughout our UI and even on the game logic layer.

How to use Event Delegation with Unity

All we have to do is:

  • create a static Event Manager class
    • Add delegates and events to this Event Manager
  • Listeners will add functions to the events
  • Delegators will fire off the events

The Manager: Defining events

public class EventManager {
    public delegate void EnemyHovered(int laneIndex, int targetRange);
    public static event EnemyHovered EnemyTargetedRequest;
    public static void EnemyTargeted(int laneIndex, int targetRange) {
        if(EnemyTargetedRequest != null) EnemyTargetedRequest(laneIndex, targetRange);
    }
}

In this example we create a delegate with the type EnemyHovered which takes the arguments laneIndex and targetRange. Then we make an event EnemyTargetRequest which is the event that the listeners will subscribe functions to. Finally, there is a static method that can be called by delegators when they want to fire off this event, triggering all the functions that are subscribed to it.

The Listener: Subscribing to the event

In another class we can add the following:

void Awake() {
    EventManager.EnemyTargetedRequest += DoSomething;
}

public void DoSomething(int targetedIndex, int targetRange){
    // do something
}

In this class we define some function called DoSomething that takes the same arguments as our delegate. In the Awake() method we add the function to the static class’s EnemyTargetedRequest event.

The Delegator: Firing off events

In yet another class we can do the following:

public void OnPointerEnter(PointerEventData eventData) {      
    EventManager.EnemyTargeted(_index, _targetCount);
}

Here we’re just using Unity’s OnPointerEnter event as the UI event that will trigger our EnemyTargeted event. Because our Listener class subscribed it’s DoSomething function to the EnemyTargetRequest event when SignalManager.EnemyTargeted is called it will fire off the DoSomething function!

So with this, you can keep all your events off in the static Event Manager, subscribe to them when you need an object to react to an event and fire off the events whenever you need to which allows you to keep UI and game logic separated. You could even have GameObjects subscribe to events and unsubscribe from them as they are created and destroyed which is very useful when these objects are created and destroyed dynamically.

 

Naming Things is Hard: Brainstorming Names

I’ve been working on my game for about 3 months now and I have yet to come up with a suitable name for it. I’ve been referring to it mainly as “My Game” since it’s really my first game project to make it this far. But I have given it a code name which I will use to refer to it when posting here, and that code name is Nano. This comes from the setting of the game which I’ll discuss more in a future post but suffice to say, nano technology plays an important roll in the setting.

How to Brainstorm Names

I came up with Robot Monkey Brain during a few short brainstorming sessions with a pal of mine. It’s real important to have someone to bounce ideas off of, and when brainstorming it’s best if there aren’t any negative judgments about ideas. Just get a couple of people to start spouting out names and phrases and follow the good ideas, building on them but feel free to jump tracks if one path is getting stale. We were hovering around the words Robot and Monkey since I’m a big fan of robotics and my online handle has included a misspelling of the word monkey since I was a freshman in high school. Once you find a good one make sure it isn’t taken:

  1. Search the name, see if the name already has some pop culture weight around it.
  2. See if the domain is taken. You want the .com extension unless you’re outside the US and don’t plan on making your game available internationally.
  3. Check if there is already a Facebook page and twitter account using that name.
  4. If all those things are clear, snatch up the domain, Facebook page and twitter name.

I kinda screwed up the twitter part in that RobotMonkeyBrain is 1 letter too long to be a twitter handle, so I had to go with @RoboMonkeyBrain. It’s not the end of the world but the OCD part of me would prefer all the names match and I do have the Robot Monkey Brain Facebook Page all set.

I’ll go through the same process when I’m ready to give an official name to Nano, but I still need to develop more of the core systems and setting details before I set that in stone.