@@ -62,6 +62,9 @@ public class MotionControllerVisualizer : Singleton<MotionControllerVisualizer>
6262
6363 public event Action < MotionControllerInfo > OnControllerModelLoaded ;
6464 public event Action < MotionControllerInfo > OnControllerModelUnloaded ;
65+
66+ private bool leftModelIsAlternate = false ;
67+ private bool rightModelIsAlternate = false ;
6568#endif
6669
6770#if UNITY_EDITOR_WIN
@@ -74,14 +77,6 @@ protected override void Awake()
7477 base . Awake ( ) ;
7578
7679#if UNITY_WSA && UNITY_2017_2_OR_NEWER
77- foreach ( var sourceState in InteractionManager . GetCurrentReading ( ) )
78- {
79- if ( sourceState . source . kind == InteractionSourceKind . Controller )
80- {
81- StartTrackingController ( sourceState . source ) ;
82- }
83- }
84-
8580 Application . onBeforeRender += Application_onBeforeRender ;
8681
8782 if ( GLTFMaterial == null )
@@ -116,23 +111,41 @@ protected override void OnDestroy()
116111 InteractionManager . InteractionSourceDetected -= InteractionManager_InteractionSourceDetected ;
117112 InteractionManager . InteractionSourceLost -= InteractionManager_InteractionSourceLost ;
118113 Application . onBeforeRender -= Application_onBeforeRender ;
114+
115+ foreach ( MotionControllerInfo controllerInfo in controllerDictionary . Values )
116+ {
117+ Destroy ( controllerInfo . ControllerParent ) ;
118+ }
119119#endif
120120 }
121121
122122 private void Application_onBeforeRender ( )
123123 {
124124 // NOTE: This work is being done here to present the most correct rendered location of the controller each frame.
125125 // Any app logic depending on the controller state should happen in Update() or using InteractionManager's events.
126- UpdateControllerState ( ) ;
126+ // We don't want to potentially start loading a new controller model in this call, since onBeforeRender shouldn't
127+ // do much work for performance reasons.
128+ UpdateControllerState ( false ) ;
127129 }
128130
129- private void UpdateControllerState ( )
131+ private void UpdateControllerState ( bool createNewControllerIfNeeded = true )
130132 {
131133#if UNITY_WSA && UNITY_2017_2_OR_NEWER
132134 foreach ( var sourceState in InteractionManager . GetCurrentReading ( ) )
133135 {
136+ if ( sourceState . source . kind != InteractionSourceKind . Controller )
137+ {
138+ continue ;
139+ }
140+
141+ string key = GenerateKey ( sourceState . source ) ;
142+ if ( createNewControllerIfNeeded && ! controllerDictionary . ContainsKey ( key ) && ! loadingControllers . Contains ( key ) )
143+ {
144+ StartTrackingController ( sourceState . source ) ;
145+ }
146+
134147 MotionControllerInfo currentController ;
135- if ( sourceState . source . kind == InteractionSourceKind . Controller && controllerDictionary . TryGetValue ( GenerateKey ( sourceState . source ) , out currentController ) )
148+ if ( controllerDictionary . TryGetValue ( key , out currentController ) )
136149 {
137150 if ( AnimateControllerModel )
138151 {
@@ -236,7 +249,8 @@ private void InteractionManager_InteractionSourceLost(InteractionSourceLostEvent
236249 if ( source . kind == InteractionSourceKind . Controller )
237250 {
238251 MotionControllerInfo controllerInfo ;
239- if ( controllerDictionary != null && controllerDictionary . TryGetValue ( GenerateKey ( source ) , out controllerInfo ) )
252+ string key = GenerateKey ( source ) ;
253+ if ( controllerDictionary != null && controllerDictionary . TryGetValue ( key , out controllerInfo ) )
240254 {
241255 if ( OnControllerModelUnloaded != null )
242256 {
@@ -246,10 +260,22 @@ private void InteractionManager_InteractionSourceLost(InteractionSourceLostEvent
246260 if ( controllerInfo . Handedness == InteractionSourceHandedness . Left )
247261 {
248262 leftControllerModel = null ;
263+
264+ if ( ! AlwaysUseAlternateLeftModel && leftModelIsAlternate )
265+ {
266+ controllerDictionary . Remove ( key ) ;
267+ Destroy ( controllerInfo . ControllerParent ) ;
268+ }
249269 }
250270 else if ( controllerInfo . Handedness == InteractionSourceHandedness . Right )
251271 {
252272 rightControllerModel = null ;
273+
274+ if ( ! AlwaysUseAlternateRightModel && rightModelIsAlternate )
275+ {
276+ controllerDictionary . Remove ( key ) ;
277+ Destroy ( controllerInfo . ControllerParent ) ;
278+ }
253279 }
254280
255281 controllerInfo . ControllerParent . SetActive ( false ) ;
@@ -380,6 +406,15 @@ private IEnumerator LoadSourceControllerModel(InteractionSource source)
380406
381407 yield return sceneImporter . Load ( ) ;
382408
409+ if ( source . handedness == InteractionSourceHandedness . Left )
410+ {
411+ leftModelIsAlternate = false ;
412+ }
413+ else if ( source . handedness == InteractionSourceHandedness . Right )
414+ {
415+ rightModelIsAlternate = false ;
416+ }
417+
383418 FinishControllerSetup ( controllerModelGameObject , source . handedness , GenerateKey ( source ) ) ;
384419 }
385420
@@ -389,10 +424,12 @@ private void LoadAlternateControllerModel(InteractionSource source)
389424 if ( source . handedness == InteractionSourceHandedness . Left && AlternateLeftController != null )
390425 {
391426 controllerModelGameObject = Instantiate ( AlternateLeftController ) ;
427+ leftModelIsAlternate = true ;
392428 }
393429 else if ( source . handedness == InteractionSourceHandedness . Right && AlternateRightController != null )
394430 {
395431 controllerModelGameObject = Instantiate ( AlternateRightController ) ;
432+ rightModelIsAlternate = true ;
396433 }
397434 else
398435 {
@@ -412,7 +449,7 @@ private void FinishControllerSetup(GameObject controllerModelGameObject, Interac
412449 {
413450 var parentGameObject = new GameObject
414451 {
415- name = handedness + "Controller"
452+ name = dictionaryKey + "Controller"
416453 } ;
417454
418455 parentGameObject . transform . parent = transform ;
0 commit comments