Skip to content

Commit a11a3ee

Browse files
added handling for cached ghost controllers that unity's interactionManager sends our way on consecutive editor sessions with different controller setup
1 parent 38fed3e commit a11a3ee

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/WindowsMixedRealityDeviceManager.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public WindowsMixedRealityDeviceManager(
6565
/// <summary>
6666
/// Cache of the states captured from the Unity InteractionManager for UWP
6767
/// </summary>
68-
InteractionSourceState[] interactionmanagerStates = new InteractionSourceState[MaxInteractionSourceStates];
68+
InteractionSourceState[] interactionManagerStates = new InteractionSourceState[MaxInteractionSourceStates];
6969

7070
/// <summary>
7171
/// The number of states captured most recently
@@ -299,20 +299,17 @@ public override void Enable()
299299

300300
UpdateInteractionManagerReading();
301301

302-
// Avoids a Unity Editor bug detecting a controller from the previous run during the first frame
303-
#if !UNITY_EDITOR
304-
// NOTE: We update the source state data, in case an app wants to query it on source detected.
302+
//NOTE: We update the source state data, in case an app wants to query it on source detected.
305303
for (var i = 0; i < numInteractionManagerStates; i++)
306304
{
307-
var controller = GetController(interactionmanagerStates[i].source);
305+
var controller = GetController(interactionManagerStates[i].source);
308306

309307
if (controller != null)
310308
{
311-
controller.UpdateController(interactionmanagerStates[i]);
309+
controller.UpdateController(interactionManagerStates[i]);
312310
inputSystem?.RaiseSourceDetected(controller.InputSource, controller);
313311
}
314312
}
315-
#endif
316313

317314
if ((inputSystem != null) &&
318315
InputSystemProfile.GesturesProfile != null &&
@@ -333,15 +330,15 @@ public override void Update()
333330
{
334331
// SourceDetected gets raised when a new controller is detected and, if previously present,
335332
// when OnEnable is called. Do not create a new controller here.
336-
var controller = GetController(interactionmanagerStates[i].source, false);
333+
var controller = GetController(interactionManagerStates[i].source, false);
337334

338335
if (controller != null)
339336
{
340-
controller.UpdateController(interactionmanagerStates[i]);
337+
controller.UpdateController(interactionManagerStates[i]);
341338
}
342339
}
343340

344-
LastInteractionManagerStateReading = interactionmanagerStates;
341+
LastInteractionManagerStateReading = interactionManagerStates;
345342
}
346343

347344
private void RegisterGestureEvents()
@@ -571,15 +568,6 @@ private void RemoveController(InteractionSource interactionSource)
571568
/// <param name="args">SDK source detected event arguments</param>
572569
private void InteractionManager_InteractionSourceDetected(InteractionSourceDetectedEventArgs args)
573570
{
574-
575-
// Avoids a Unity Editor bug detecting a controller from the previous run during the first frame
576-
#if UNITY_EDITOR
577-
if (Time.frameCount <= 1)
578-
{
579-
return;
580-
}
581-
#endif
582-
583571
bool raiseSourceDetected = !activeControllers.ContainsKey(args.state.source.id);
584572

585573
var controller = GetController(args.state.source);
@@ -769,20 +757,29 @@ private void UpdateInteractionManagerReading()
769757
int newSourceStateCount = InteractionManager.numSourceStates;
770758
// If there isn't enough space in the cache to hold the results, we should grow it so that it can, but also
771759
// grow it in a way that is unlikely to require re-allocations each time.
772-
if (newSourceStateCount > interactionmanagerStates.Length)
760+
if (newSourceStateCount > interactionManagerStates.Length)
773761
{
774-
interactionmanagerStates = new InteractionSourceState[newSourceStateCount * InteractionManagerStatesGrowthFactor];
762+
interactionManagerStates = new InteractionSourceState[newSourceStateCount * InteractionManagerStatesGrowthFactor];
775763
}
776764

777765
// Note that InteractionManager.GetCurrentReading throws when invoked when the number of
778766
// source states is zero. In that case, we want to just update the number of read states to be zero.
779767
if (newSourceStateCount == 0)
780768
{
781-
numInteractionManagerStates = 0;
769+
// clean up existing controllers that didn't trigger the InteractionSourceLost event.
770+
// this can happen eg. when unity is registering cached controllers from a previous play session in the editor.
771+
// those actually don't exist in the current session and therefor won't receive the InteractionSourceLost once
772+
// Unity's InteractionManager catches up
773+
for (int i = 0; i < numInteractionManagerStates; ++i)
774+
{
775+
RemoveController(interactionManagerStates[i].source);
776+
}
777+
778+
numInteractionManagerStates = newSourceStateCount;
782779
}
783780
else
784781
{
785-
numInteractionManagerStates = InteractionManager.GetCurrentReading(interactionmanagerStates);
782+
numInteractionManagerStates = InteractionManager.GetCurrentReading(interactionManagerStates);
786783
}
787784
}
788785

@@ -791,4 +788,4 @@ private void UpdateInteractionManagerReading()
791788
#endif // UNITY_WSA
792789

793790
}
794-
}
791+
}

0 commit comments

Comments
 (0)