Gamepads in Unity: glyphs and detecting the input device

Article languages: English and Russian
Contents

A diagram connecting an input device, action bindings and a C# script.

At the end of the year, I feel like switching into little-hermit mode and building a snowman, not dealing with all this.
But Ivan asked how to display glyphs and detect the input device, so here comes part three about gamepads.

Rewired

Rewired has glyphs. You need to add them separately through the menu.
Atlases for all types of gamepads are already included. If you do not like them, you can add your own.
You can also roll your own solution. I did that to render them using a special font.
Connection and disconnection events can be handled like this.
And the last-used controller can be detected like this. You can additionally filter by “playstation” or “XBox” appearing in the device name. It is better to test on real devices.

Complications:\

  1. Rewired distinguishes between buttons and axes. With the wrong type, it may not return an LT/RT glyph for a trigger.\
  2. To get everything working properly on Windows, enable Use XInput + Use Windows Gaming Input in Rewired’s settings. Otherwise, action names will be reported incorrectly.

Input System

InputSystemActionPrompts for displaying prompts.
https://youtu.be/5tOOstXaIKE—this video explains input types.
https://github.com/UnityTechnologies/InputSystem_Warriors—a code example.

In a very simplified form, it could look something like this:

C#
using System;

using UnityEngine.InputSystem;

public class DeviceManager
{
  public event Action Changed;
  public event Action Connected;

  private InputDevice _lastInputDevice;
  public DeviceKind LastDeviceKind { get; private set; }

  public void Init()
  {
    InputSystem.onActionChange += OnAnyActionChange;
    InputSystem.onDeviceChange += OnDeviceChange;
  }

  public void Destroy()
  {
    InputSystem.onActionChange -= OnAnyActionChange;
    InputSystem.onDeviceChange -= OnDeviceChange;
  }

  private void OnDeviceChange(InputDevice device, InputDeviceChange change)
  {
    if (change == InputDeviceChange.Reconnected || change == InputDeviceChange.Added)
      Connected?.Invoke();
  }

  private void OnAnyActionChange(object obj, InputActionChange change)
  {
    if (change != InputActionChange.ActionPerformed)
      return;
    var inputAction = (InputAction)obj;
    var lastControl = inputAction.activeControl;
    var device = lastControl.device;
    if (device == _lastInputDevice)
      return;
    _lastInputDevice = device;
    var deviceName = device.name;
    DeviceKind kind =
        deviceName == "Mouse" || deviceName == "Keyboard" ? DeviceKind.KeyboardMouse :
        deviceName.ToLower().IndexOf("playstation") != -1 ? DeviceKind.Sony :
        DeviceKind.Xbox;
    if (LastDeviceKind != kind)
    {
      LastDeviceKind = kind;
      Changed?.Invoke();
    }
  }
}

Complications:
Some steering wheels and joysticks send “right stick tilted left” when connected and go haywire. We had a really rough time with that on the Davydov project. Those devices are also quite difficult to filter out in InputSystem.

Image viewer