Unity new InputSystem Gamepad events?

Hi there,

I’m using the New Input System (com.unity.inputsystem) and have hit a pretty weird roadbloack seemingly caused by the API being pretty secretive with me :smile: Need some help :sos:

Problem: I would like to be able to adapt to gamepads being added and removed at any point by the user.

  • I would like to not have to constantly scan for changes (I could just schedule a task to keep scanning Gamepad.all and fire an event when a change is detected, but I really want to avoid this)

Looking at the documentation for Gamepad:
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.Gamepad.html#UnityEngine_InputSystem_Gamepad_dpad

I see there are OnAdded() and OnRemoved() methods that handle exactly this scenario internally for Unity (since this ofc needs to be done somewhere). However, there seems to be no way to get notified or hook into this logic from game code.

Looking at the decompiled code:

/// <summary>
        /// Called when the gamepad is added to the system.
        /// </summary>
        protected override void OnAdded()
        {
            ArrayHelpers.AppendWithCapacity(ref s_Gamepads, ref s_GamepadCount, this);
        }

Feels to me like this method could easily include an event like onDeviceListChanged?.Invoke(...) so that game code can easily adapt to changes.

How could I hook into these 2 methods, or at least get notified when a controller is added / removed (again without having to monitor the Gamepad.all array)? Hoping there’s something I’ve missed.

Thanks!

You want to subscribe to InputSystem.onDeviceChange. There’s only this one event that gets called for all devices, so you’ll need to ignore the calls where device is not a gamepad.

This is also explained in the documentation on Devices.

1 Like

Thanks a lot! Naturally I missed this :man_facepalming: