diff --git a/XRTK.WindowsMixedReality/Assets/XRTK.WindowsMixedReality/Controllers/WindowsMixedRealityDataProvider.cs b/XRTK.WindowsMixedReality/Assets/XRTK.WindowsMixedReality/Controllers/WindowsMixedRealityDataProvider.cs index f5f677b..15fb41f 100644 --- a/XRTK.WindowsMixedReality/Assets/XRTK.WindowsMixedReality/Controllers/WindowsMixedRealityDataProvider.cs +++ b/XRTK.WindowsMixedReality/Assets/XRTK.WindowsMixedReality/Controllers/WindowsMixedRealityDataProvider.cs @@ -54,6 +54,12 @@ public WindowsMixedRealityDataProvider(string name, uint priority, WindowsMixedR private readonly WindowsMixedRealityControllerDataProviderProfile profile; + /// + /// The max expected sources is two - two controllers and/or two hands. + /// We'll set it to 20 just to be certain we can't run out of sources. + /// + public const int MaxInteractionSourceStates = 20; + /// /// Dictionary to capture all active controllers detected /// @@ -62,7 +68,12 @@ public WindowsMixedRealityDataProvider(string name, uint priority, WindowsMixedR /// /// Cache of the states captured from the Unity InteractionManager for UWP /// - private InteractionSourceState[] interactionManagerStates; + InteractionSourceState[] interactionManagerStates = new InteractionSourceState[MaxInteractionSourceStates]; + + /// + /// The number of states captured most recently + /// + private int numInteractionManagerStates; /// /// The current source state reading for the Unity InteractionManager for UWP @@ -279,10 +290,10 @@ public override void Enable() InteractionManager.InteractionSourceDetected += InteractionManager_InteractionSourceDetected; InteractionManager.InteractionSourceLost += InteractionManager_InteractionSourceLost; - interactionManagerStates = InteractionManager.GetCurrentReading(); + numInteractionManagerStates = InteractionManager.GetCurrentReading(interactionManagerStates); // NOTE: We update the source state data, in case an app wants to query it on source detected. - for (var i = 0; i < interactionManagerStates?.Length; i++) + for (var i = 0; i < numInteractionManagerStates; i++) { var controller = GetController(interactionManagerStates[i].source); @@ -306,9 +317,9 @@ public override void Update() { base.Update(); - interactionManagerStates = InteractionManager.GetCurrentReading(); + numInteractionManagerStates = InteractionManager.GetCurrentReading(interactionManagerStates); - for (var i = 0; i < interactionManagerStates?.Length; i++) + for (var i = 0; i < numInteractionManagerStates; i++) { GetController(interactionManagerStates[i].source, false)?.UpdateController(interactionManagerStates[i]); }