I’m currently trying to build in local multiplayer support for my game. Once the game is running, things work great: I just instantiate my Player prefab as many times as I need and the Input System takes care of things, smartly mapping available devices to player prefab instances (each player prefab instance has it’s own Player Input and Player Controller component).
However I need to subscribe to the onDeviceChange event at the main menu to detect how many players (well, technically how many input devices) are present before the game starts. Here is what I have:
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Users;
public class InputManager : MonoBehaviour
{
public static InputManager instance;
public InputUser[] Players { get; private set; }
private void Awake()
{
if (instance == null) instance = this;
else if (instance != this) Destroy(this);
DontDestroyOnLoad(this);
}
private void Start()
{
// add our listener to the onDeviceChange event
InputSystem.onDeviceChange += onInputDeviceChange;
InputSystem.onEvent += onEvent;
}
public void onInputDeviceChange(InputDevice device, InputDeviceChange change)
{
Debug.Log("onInputDeviceChange");
switch (change)
{
case InputDeviceChange.Added:
Debug.Log("Device added: " + device);
break;
case InputDeviceChange.Removed:
Debug.Log("Device removed: " + device);
break;
case InputDeviceChange.ConfigurationChanged:
Debug.Log("Device configuration changed: " + device);
break;
}
}
public void onEvent(InputEventPtr inputEvent, InputDevice device)
{
Debug.Log("onEvent");
}
}
The problem is that the onDeviceChange event never fires - even when I turn on my Xbox controller after the game has started, or disconnect and reconnect the device. The onEvent fires constantly, so that at least works, but I only put that there to test that subscribing to InputSystem events this way does actually work.
I don’t get why the onDeviceChange event doesn’t fire - can anyone see what I’m doing wrong here?