InputSystem.devices.count returns 0 in MonoBehavior.Start method
When application starts, InputSystem.devices will contain zero devices, if you would query it from Start method. The devices are populated later...
Can this be fixed somehow? Like device creation moved before any script's Start or even Awake method?
Hmm, bit of a tricky one. Problem is that devices are sent as part of updates -- which happen in the player loop. So with the first update happening after Start(), managed won't see any data coming in before.
What we probably want is to at least do one initial push of device discoveries before Start() and then let the system run from them on the way it does now.
I should add this is inconsistent with Editor behavior, in Editor if you query InputSystem.devices in Start you get a list of devices, while in player you get zero devices.
In Start() in an ExecuteInEditMode MonoBehaviour?
No, in simple Start method without any attributes, when you click Play in Editor.
Ah right. That makes sense. In the editor, the input system would get initialized on startup by the first edit mode update and by the time you go into play mode, it's already working off a fully initialized state that is simply persisted between domain reloads.
So I assume you agree this should be fixed then ? :)
Yeah I do. Doug is looking into it.
status update for everyone. I was having difficulty finding a way to push devices after RuntimeInitializeOnLoadMethod but before start(). None of the global callbacks worked. I asked around, and there is a PR in flight that will give me the exact callback we need. https://ono.unity3d.com/unity/unity/changeset/bf2841236b9a303f26697bf8ed37b6ebcdcab871] Eneko says this will trigger after RuntimeInitializeOnLoadMethod and before start(). I'm going to test out the branch and see if it gives us what we want.
Before Start and Awake?
I'll test. I think so, but will test to make sure.
@piconid Will we get that branch for .2, though?
Are you sure that none of the global callbacks are suitable? Looking around, one thing that seems like it should work is using sceneLoadedAfterAwake. ATM InputSystem initialization in the player will run in sceneLoadedBeforeAwake. Pushing device discoveries in sceneLoadedAfterAwake should make it so devices are pushed out after Awake() is called on all objects. That way the devices should be available in Start(). It'd get called on every first-scene load, but IMO that's just fine. If there's no new devices to push out, it'll do nothing.
Fix in flight, PR under review to trunk. Will also backport to 2018.2
Fixed on managed side. Took me a while to realize that we have everything we need on the managed side and don't need any supporting native changes. Was a one line fix in the end.