How To Find Which Game Controller Is Connected – Rewired, Unity

Rewired is a great Unity asset that can be bought and downloaded from the Unity Asset Store here. It allows for Universal game pad support with ease, great for console ports. There is a lot of support and documentation, but also that sheer amount of information can be overwhelming and hard to find what you want. I’ve found it very helpful porting my game Puzzledorf to console and for PC controller support.

Puzzledorf

I am going to show how you can identify which controller the player has plugged in and is using, so that from there, you can choose to run different scripts based on that information. Final code is summarised at the end. This is useful when you want to do something very specific for certain game pads, like showing different controls when doing a console port, ie, the Switch Joy Con’s vs the Switch Pro Controller.

That being said, this uses Hardware GUID and therefore only covers game controllers you can plug into your PC and test with Unity to get the Hardware GUID, unless you can get the Hardware GUID of the controller some other way. You can’t do this for every single game pad on the market, so I’ve got further info about supporting generic game pads at the end as well.

Tutorial

First, make sure to use the Rewired namespace at the top of your script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Rewired;

Then create variables to store information about your player and their controller.

Player rewiredPlayer;
Joystick joystick;
public string controllerGUID; 

Creating a Player variable, which comes from the Rewired coding libraries, simply gives you access to things like inputs and the last controller used. Joystick stores information about our specific joystick / gamepad we want to access.

In Start, we’ll define the rewiredPlayer variable.

void Start()
{
    rewiredPlayer = ReInput.players.GetPlayer(0);
}

All we’re doing is getting the first player so we can access their inputs. If the game was multiplayer, we would do this separately for each player. Setting it to player 0 means “get Player 1” because we start counting from 0.

Each game controller receives a unique Hardware GUID that can be accessed from a script within Unity. By comparing GUID’s, we can work out which controller is being accessed. Here is a simple way to get the GUID of each controller.

void SetContollerGUID()
{
if (rewiredPlayer.controllers.Joysticks.Count > 0)
{
Joystick joystick = rewiredPlayer.controllers.Joysticks[0];
controllerGUID = joystick.hardwareTypeGuid.ToString();
}
}




We’re now setting the variable controllerGUID we created at the start. We will compare this variable to the stored Hardware GUID’s of each controller to do the comparison.

First we look inside the rewiredPlayer variable in the list of controllers. In that list, there is an array of Joysticks. We check to see if the count is greater than 0 to make sure a Joystick, or Gamepad, is plugged in. If it is, look at the first joystick in the array of joysticks by using [0].

Then, we get the Hardware GUID from the joystick, and display that inside the controllerGUID variable by converting it to a string. We have to use ToString() to convert the GUID variable because otherwise we could not display the GUID as a string. We don’t have to do this, but it makes it a bit easier to display the GUID in the inspector, and copy and paste it for use later.

But, how do you know what the GUID is for each controller we want to use? This is why I made the example a public string. Just run your project and plug in a game controller, and the Hardware GUID will show in the Inspector.

That’s the Hardware GUID of the specific game pad I had plugged in at the time. Just highlight that text, and copy and paste it somewhere for use later. Make sure to write the name of the game pad next to it.

Now, if you want to do a comparison, it’s very simple. You just need a list of all of the different Hardware GUID’s. You might want to do it as a dictionary or look up table, if you’re checking against lots of GUID’s, but here’s a much simpler example for the sake of brevity.

Create 2 new variables at the top of your script. Make sure you use the Hardware GUID’s relative to your controllers, don’t just copy mine.

string joystick_GUID1 = "e63076ff-ceb9-4e3d-b8d4-320306a42707";
string joystick_GUID2 = "28f0147d-cdb1-4d41-9778-60f7774d000c";

Then write in your Update function…

    private void Update()
    {
        SetContollerGUID();

        if(controllerGUID == joystick_GUID1)
        {
            print("Using controller 1.");
        }
        else if (controllerGUID == joystick_GUID2)
        {
            print("Using controller 2");
        }
    }

And now you will see a different message being printed depending on which controller is plugged in.

Taking it Further

The Rewired Documentation here goes into much greater detail if you want to dive into things like, how to display different icons for each different button on your controller, etc. And it talks about using a universal GUID of 00000000-0000-0000-0000-000000000000 for when you don’t know what controller it is.

So far I have only used this method with controllers that can be plugged in via USB in Unity for porting to a very specific console, and I will be displaying different controls specifically for that console. On PC, at the moment my game just displays one generic set of controls using A and B, however, any game pad will work with the game. I don’t have the time to update the UI for every single controller type, though at some point I intend to create several versions for the most supported PC controllers, ie, X Box, Playstation, and maybe Steam or Logitech.

If you liked this article, please check out Puzzledorf to show your support. If you enjoy playing, leaving a review helps the games visibility on Steam.

Final Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Rewired;

Player rewiredPlayer; 
Joystick joystick; 
public string controllerGUID;

string joystick_GUID1 = "e63076ff-ceb9-4e3d-b8d4-320306a42707";
string joystick_GUID2 = "28f0147d-cdb1-4d41-9778-60f7774d000c";

void Start()
{
    rewiredPlayer = ReInput.players.GetPlayer(0);
}

private void Update()
{
    SetContollerGUID();

    if(controllerGUID == joystick_GUID1)
    {
        print("Using controller 1.");
    }
    else if (controllerGUID == joystick_GUID2)
    {
        print("Using controller 2");
    }
}

void SetContollerGUID()
{
    if (rewiredPlayer.controllers.Joysticks.Count > 0)
    {
        joystick = rewiredPlayer.controllers.Joysticks[0];
        controllerGUID = joystick.hardwareTypeGuid.ToString();
    }
}

Leave a comment

Blog at WordPress.com.

Up ↑