@@ -3,6 +3,7 @@ namespace VRTK
3
3
{
4
4
using UnityEngine ;
5
5
using System . Collections ;
6
+ using System . Collections . Generic ;
6
7
7
8
/// <summary>
8
9
/// Event Payload
@@ -146,15 +147,10 @@ public enum ValidInteractingObject
146
147
/// </summary>
147
148
public event InteractObjectAppearanceEventHandler RenderersDisabled ;
148
149
149
- protected GameObject touchAffectedObject ;
150
- protected GameObject grabAffectedObject ;
151
- protected GameObject useAffectedObject ;
152
- protected Coroutine touchRoutine ;
153
- protected Coroutine grabRoutine ;
154
- protected Coroutine useRoutine ;
155
- protected bool currentRenderState ;
156
- protected bool currentGameObjectState ;
157
- protected bool currentStatesSet ;
150
+ protected Dictionary < GameObject , bool > currentRenderStates = new Dictionary < GameObject , bool > ( ) ;
151
+ protected Dictionary < GameObject , bool > currentGameObjectStates = new Dictionary < GameObject , bool > ( ) ;
152
+ protected Dictionary < GameObject , Coroutine > affectingRoutines = new Dictionary < GameObject , Coroutine > ( ) ;
153
+ protected List < GameObject > touchingObjects = new List < GameObject > ( ) ;
158
154
159
155
public virtual void OnGameObjectEnabled ( InteractObjectAppearanceEventArgs e )
160
156
{
@@ -190,7 +186,10 @@ public virtual void OnRenderersDisabled(InteractObjectAppearanceEventArgs e)
190
186
191
187
protected virtual void OnEnable ( )
192
188
{
193
- currentStatesSet = false ;
189
+ currentRenderStates . Clear ( ) ;
190
+ currentGameObjectStates . Clear ( ) ;
191
+ affectingRoutines . Clear ( ) ;
192
+ touchingObjects . Clear ( ) ;
194
193
objectToMonitor = ( objectToMonitor == null ? GetComponentInParent < VRTK_InteractableObject > ( ) : objectToMonitor ) ;
195
194
196
195
if ( objectToMonitor != null )
@@ -227,7 +226,7 @@ protected virtual void OnDisable()
227
226
objectToMonitor . InteractableObjectUsed -= InteractableObjectUsed ;
228
227
objectToMonitor . InteractableObjectUnused -= InteractableObjectUnused ;
229
228
}
230
- CancelAllRoutines ( ) ;
229
+ CancelRoutines ( ) ;
231
230
}
232
231
233
232
protected virtual InteractObjectAppearanceEventArgs SetPayload ( GameObject affectingObject , InteractionType interactionType )
@@ -244,7 +243,10 @@ protected virtual void RestoreDefaults()
244
243
{
245
244
if ( objectToMonitor != null && objectToMonitor . IsTouched ( ) )
246
245
{
247
- ToggleState ( touchAffectedObject , gameObjectActiveByDefault , rendererVisibleByDefault , InteractionType . None ) ;
246
+ for ( int i = 0 ; i < touchingObjects . Count ; i ++ )
247
+ {
248
+ ToggleState ( touchingObjects [ i ] , gameObjectActiveByDefault , rendererVisibleByDefault , InteractionType . None ) ;
249
+ }
248
250
}
249
251
}
250
252
@@ -281,21 +283,20 @@ protected virtual void ToggleState(GameObject objectToToggle, bool gameObjectSho
281
283
{
282
284
if ( objectToToggle != null )
283
285
{
284
- if ( ! currentStatesSet || currentRenderState != rendererShow )
286
+ if ( ! currentRenderStates . ContainsKey ( objectToToggle ) || currentRenderStates [ objectToToggle ] != rendererShow )
285
287
{
286
288
VRTK_ObjectAppearance . ToggleRenderer ( rendererShow , objectToToggle , ObjectToIgnore ( ) ) ;
287
289
EmitRenderEvent ( objectToToggle , rendererShow , interactionType ) ;
288
290
}
289
291
290
- if ( ! currentStatesSet || currentGameObjectState != gameObjectShow )
292
+ if ( ! currentGameObjectStates . ContainsKey ( objectToToggle ) || currentGameObjectStates [ objectToToggle ] != gameObjectShow )
291
293
{
292
294
objectToToggle . SetActive ( gameObjectShow ) ;
293
295
EmitGameObjectEvent ( objectToToggle , gameObjectShow , interactionType ) ;
294
296
}
295
297
296
- currentRenderState = rendererShow ;
297
- currentGameObjectState = gameObjectShow ;
298
- currentStatesSet = true ;
298
+ currentRenderStates [ objectToToggle ] = rendererShow ;
299
+ currentGameObjectStates [ objectToToggle ] = gameObjectShow ;
299
300
}
300
301
}
301
302
@@ -305,37 +306,24 @@ protected virtual IEnumerator ToggleStateAfterTime(GameObject objectToToggle, bo
305
306
ToggleState ( objectToToggle , gameObjectShow , rendererShow , interactionType ) ;
306
307
}
307
308
308
- protected virtual void CancelAllRoutines ( )
309
- {
310
- CancelTouchRoutine ( ) ;
311
- CancelGrabRoutine ( ) ;
312
- CancelUseRoutine ( ) ;
313
- }
314
-
315
- protected virtual void CancelTouchRoutine ( )
309
+ protected virtual void CancelRoutines ( GameObject currentAffectingObject = null )
316
310
{
317
- if ( touchRoutine != null )
311
+ if ( currentAffectingObject != null )
318
312
{
319
- StopCoroutine ( touchRoutine ) ;
320
- touchRoutine = null ;
321
- }
322
- }
323
-
324
- protected virtual void CancelGrabRoutine ( )
325
- {
326
- if ( grabRoutine != null )
327
- {
328
- StopCoroutine ( grabRoutine ) ;
329
- grabRoutine = null ;
313
+ if ( affectingRoutines . ContainsKey ( currentAffectingObject ) && affectingRoutines [ currentAffectingObject ] != null )
314
+ {
315
+ StopCoroutine ( affectingRoutines [ currentAffectingObject ] ) ;
316
+ }
330
317
}
331
- }
332
-
333
- protected virtual void CancelUseRoutine ( )
334
- {
335
- if ( useRoutine != null )
318
+ else
336
319
{
337
- StopCoroutine ( useRoutine ) ;
338
- useRoutine = null ;
320
+ foreach ( KeyValuePair < GameObject , Coroutine > affectingRouting in affectingRoutines )
321
+ {
322
+ if ( currentAffectingObject == affectingRouting . Key && affectingRouting . Value != null )
323
+ {
324
+ StopCoroutine ( affectingRouting . Value ) ;
325
+ }
326
+ }
339
327
}
340
328
}
341
329
@@ -386,81 +374,89 @@ protected virtual void InteractableObjectTouched(object sender, InteractableObje
386
374
{
387
375
if ( IsValidInteractingObject ( e . interactingObject , validTouchInteractingObject ) )
388
376
{
389
- CancelAllRoutines ( ) ;
390
- touchAffectedObject = ( objectToAffect == null ? GetActualController ( e . interactingObject ) : objectToAffect ) ;
391
- touchRoutine = StartCoroutine ( ToggleStateAfterTime ( touchAffectedObject , gameObjectActiveOnTouch , rendererVisibleOnTouch , touchAppearanceDelay , InteractionType . Touch ) ) ;
377
+ GameObject touchAffectedObject = ( objectToAffect == null ? GetActualController ( e . interactingObject ) : objectToAffect ) ;
378
+ CancelRoutines ( touchAffectedObject ) ;
379
+ if ( ! touchingObjects . Contains ( touchAffectedObject ) )
380
+ {
381
+ touchingObjects . Add ( touchAffectedObject ) ;
382
+ }
383
+ affectingRoutines [ touchAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( touchAffectedObject , gameObjectActiveOnTouch , rendererVisibleOnTouch , touchAppearanceDelay , InteractionType . Touch ) ) ;
392
384
}
393
385
}
394
386
395
387
protected virtual void InteractableObjectUntouched ( object sender , InteractableObjectEventArgs e )
396
388
{
397
389
if ( IsValidInteractingObject ( e . interactingObject , validTouchInteractingObject ) )
398
390
{
399
- CancelAllRoutines ( ) ;
400
- touchRoutine = StartCoroutine ( ToggleStateAfterTime ( touchAffectedObject , gameObjectActiveByDefault , rendererVisibleByDefault , untouchAppearanceDelay , InteractionType . Untouch ) ) ;
401
- touchAffectedObject = null ;
391
+ GameObject touchAffectedObject = ( objectToAffect == null ? GetActualController ( e . interactingObject ) : objectToAffect ) ;
392
+ CancelRoutines ( touchAffectedObject ) ;
393
+ if ( touchingObjects . Contains ( touchAffectedObject ) )
394
+ {
395
+ touchingObjects . Remove ( touchAffectedObject ) ;
396
+ }
397
+ affectingRoutines [ touchAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( touchAffectedObject , gameObjectActiveByDefault , rendererVisibleByDefault , untouchAppearanceDelay , InteractionType . Untouch ) ) ;
402
398
}
403
399
}
404
400
405
401
protected virtual void InteractableObjectGrabbed ( object sender , InteractableObjectEventArgs e )
406
402
{
407
403
if ( IsValidInteractingObject ( e . interactingObject , validGrabInteractingObject ) )
408
404
{
409
- CancelAllRoutines ( ) ;
410
- grabAffectedObject = ( objectToAffect == null ? GetActualController ( e . interactingObject ) : objectToAffect ) ;
411
- grabRoutine = StartCoroutine ( ToggleStateAfterTime ( grabAffectedObject , gameObjectActiveOnGrab , rendererVisibleOnGrab , grabAppearanceDelay , InteractionType . Grab ) ) ;
405
+ GameObject grabAffectedObject = ( objectToAffect == null ? GetActualController ( e . interactingObject ) : objectToAffect ) ;
406
+ CancelRoutines ( grabAffectedObject ) ;
407
+ affectingRoutines [ grabAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( grabAffectedObject , gameObjectActiveOnGrab , rendererVisibleOnGrab , grabAppearanceDelay , InteractionType . Grab ) ) ;
412
408
}
413
409
}
414
410
415
411
protected virtual void InteractableObjectUngrabbed ( object sender , InteractableObjectEventArgs e )
416
412
{
417
413
if ( IsValidInteractingObject ( e . interactingObject , validGrabInteractingObject ) )
418
414
{
419
- CancelAllRoutines ( ) ;
415
+ GameObject grabAffectedObject = ( objectToAffect == null ? GetActualController ( e . interactingObject ) : objectToAffect ) ;
416
+ CancelRoutines ( grabAffectedObject ) ;
420
417
if ( objectToMonitor . IsUsing ( ) )
421
418
{
422
- grabRoutine = StartCoroutine ( ToggleStateAfterTime ( grabAffectedObject , gameObjectActiveOnUse , rendererVisibleOnUse , ungrabAppearanceDelay , InteractionType . Ungrab ) ) ;
419
+ affectingRoutines [ grabAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( grabAffectedObject , gameObjectActiveOnUse , rendererVisibleOnUse , ungrabAppearanceDelay , InteractionType . Ungrab ) ) ;
423
420
}
424
421
else if ( objectToMonitor . IsTouched ( ) )
425
422
{
426
- grabRoutine = StartCoroutine ( ToggleStateAfterTime ( grabAffectedObject , gameObjectActiveOnTouch , rendererVisibleOnTouch , ungrabAppearanceDelay , InteractionType . Ungrab ) ) ;
423
+ affectingRoutines [ grabAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( grabAffectedObject , gameObjectActiveOnTouch , rendererVisibleOnTouch , ungrabAppearanceDelay , InteractionType . Ungrab ) ) ;
427
424
}
428
425
else
429
426
{
430
- grabRoutine = StartCoroutine ( ToggleStateAfterTime ( grabAffectedObject , gameObjectActiveByDefault , rendererVisibleByDefault , ungrabAppearanceDelay , InteractionType . Ungrab ) ) ;
427
+ affectingRoutines [ grabAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( grabAffectedObject , gameObjectActiveByDefault , rendererVisibleByDefault , ungrabAppearanceDelay , InteractionType . Ungrab ) ) ;
431
428
}
432
- grabAffectedObject = null ;
433
429
}
434
430
}
435
431
436
432
protected virtual void InteractableObjectUsed ( object sender , InteractableObjectEventArgs e )
437
433
{
438
434
if ( IsValidInteractingObject ( e . interactingObject , validUseInteractingObject ) )
439
435
{
440
- CancelAllRoutines ( ) ;
441
- useAffectedObject = ( objectToAffect == null ? GetActualController ( e . interactingObject ) : objectToAffect ) ;
442
- useRoutine = StartCoroutine ( ToggleStateAfterTime ( useAffectedObject , gameObjectActiveOnUse , rendererVisibleOnUse , useAppearanceDelay , InteractionType . Use ) ) ;
436
+ GameObject useAffectedObject = ( objectToAffect == null ? GetActualController ( e . interactingObject ) : objectToAffect ) ;
437
+ CancelRoutines ( useAffectedObject ) ;
438
+ affectingRoutines [ useAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( useAffectedObject , gameObjectActiveOnUse , rendererVisibleOnUse , useAppearanceDelay , InteractionType . Use ) ) ;
443
439
}
444
440
}
445
441
446
442
protected virtual void InteractableObjectUnused ( object sender , InteractableObjectEventArgs e )
447
443
{
448
444
if ( IsValidInteractingObject ( e . interactingObject , validUseInteractingObject ) )
449
445
{
450
- CancelAllRoutines ( ) ;
446
+ GameObject useAffectedObject = ( objectToAffect == null ? GetActualController ( e . interactingObject ) : objectToAffect ) ;
447
+ CancelRoutines ( useAffectedObject ) ;
451
448
if ( objectToMonitor . IsGrabbed ( ) )
452
449
{
453
- useRoutine = StartCoroutine ( ToggleStateAfterTime ( useAffectedObject , gameObjectActiveOnGrab , rendererVisibleOnGrab , unuseAppearanceDelay , InteractionType . Unuse ) ) ;
450
+ affectingRoutines [ useAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( useAffectedObject , gameObjectActiveOnGrab , rendererVisibleOnGrab , unuseAppearanceDelay , InteractionType . Unuse ) ) ;
454
451
}
455
452
else if ( objectToMonitor . IsTouched ( ) )
456
453
{
457
- useRoutine = StartCoroutine ( ToggleStateAfterTime ( useAffectedObject , gameObjectActiveOnTouch , rendererVisibleOnTouch , unuseAppearanceDelay , InteractionType . Unuse ) ) ;
454
+ affectingRoutines [ useAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( useAffectedObject , gameObjectActiveOnTouch , rendererVisibleOnTouch , unuseAppearanceDelay , InteractionType . Unuse ) ) ;
458
455
}
459
456
else
460
457
{
461
- useRoutine = StartCoroutine ( ToggleStateAfterTime ( useAffectedObject , gameObjectActiveByDefault , rendererVisibleByDefault , unuseAppearanceDelay , InteractionType . Unuse ) ) ;
458
+ affectingRoutines [ useAffectedObject ] = StartCoroutine ( ToggleStateAfterTime ( useAffectedObject , gameObjectActiveByDefault , rendererVisibleByDefault , unuseAppearanceDelay , InteractionType . Unuse ) ) ;
462
459
}
463
- useAffectedObject = null ;
464
460
}
465
461
}
466
462
}
0 commit comments