@@ -51,19 +51,7 @@ protected override void Awake()
5151
5252 private void Start ( )
5353 {
54- if ( gazeManagerPointingData == null )
55- {
56- if ( GazeManager . IsInitialized )
57- {
58- gazeManagerPointingData = new PointerData ( GazeManager . Instance ) ;
59- }
60- }
61- else
62- {
63- Debug . Assert ( ReferenceEquals ( gazeManagerPointingData . PointingSource , GazeManager . Instance ) ) ;
64- }
65-
66- if ( ( pointers . Count == 0 ) && autoRegisterGazePointerIfNoPointersRegistered && GazeManager . IsInitialized )
54+ if ( pointers . Count == 0 && autoRegisterGazePointerIfNoPointersRegistered && GazeManager . IsInitialized )
6755 {
6856 RegisterPointer ( GazeManager . Instance ) ;
6957 }
@@ -75,18 +63,6 @@ private void Update()
7563 UpdateFocusedObjects ( ) ;
7664 }
7765
78- /// <summary>
79- /// It's assumed that if you're using the MRTK's input system you'll
80- /// need to update your canvases to use the proper raycast camera for input.
81- /// </summary>
82- private void OnValidate ( )
83- {
84- if ( UIRaycastCamera != null )
85- {
86- UpdateCanvasEventSystems ( ) ;
87- }
88- }
89-
9066 #endregion
9167
9268 #region Settings
@@ -273,27 +249,33 @@ public Camera UIRaycastCamera
273249
274250 public void RegisterPointer ( IPointingSource pointingSource )
275251 {
276- Debug . Assert ( pointingSource != null ) ;
252+ Debug . Assert ( pointingSource != null , "Can't register a pointer if you give us one." ) ;
253+
254+ int pointerIndex ;
255+ PointerData pointer ;
277256
278- if ( TryGetPointerIndex ( pointingSource ) != null )
257+ if ( TryGetPointerIndex ( pointingSource , out pointerIndex ) )
279258 {
280259 // This pointing source is already registered and active.
281260 return ;
282261 }
283262
284- PointerData pointer ;
285-
286263 if ( pointingSource is GazeManager )
287264 {
288265 if ( gazeManagerPointingData == null )
289266 {
290- gazeManagerPointingData = new PointerData ( pointingSource ) ;
267+ if ( GazeManager . IsInitialized )
268+ {
269+ gazeManagerPointingData = new PointerData ( GazeManager . Instance ) ;
270+ }
291271 }
292272 else
293273 {
274+ Debug . Assert ( ReferenceEquals ( gazeManagerPointingData . PointingSource , GazeManager . Instance ) ) ;
294275 gazeManagerPointingData . ResetFocusedObjects ( ) ;
295276 }
296277
278+ Debug . Assert ( gazeManagerPointingData != null ) ;
297279 pointer = gazeManagerPointingData ;
298280 }
299281 else
@@ -306,14 +288,19 @@ public void RegisterPointer(IPointingSource pointingSource)
306288
307289 public void UnregisterPointer ( IPointingSource pointingSource )
308290 {
309- Debug . Assert ( pointingSource != null ) ;
291+ Debug . Assert ( pointingSource != null , "Can't unregister a pointer if you give us one." ) ;
310292
311- int ? iPointer = TryGetPointerIndex ( pointingSource ) ;
312- Debug . Assert ( iPointer != null ) ;
293+ int pointerIndex ;
294+ TryGetPointerIndex ( pointingSource , out pointerIndex ) ;
295+ Debug . Assert ( pointerIndex > 0 , "Invalid pointer index!" ) ;
313296
314- PointerData pointer = pointers [ iPointer . Value ] ;
297+ PointerData pointer ;
298+ GetPointerData ( pointingSource , out pointer ) ;
299+ Debug . Assert ( pointer != null , "Attempting to unregister a pointer that was never registered!" ) ;
315300
316- pointers . RemoveAt ( iPointer . Value ) ;
301+ // Should we be protecting against unregistering the GazeManager?
302+
303+ pointers . RemoveAt ( pointerIndex ) ;
317304
318305 // Raise focus events if needed:
319306
@@ -343,13 +330,11 @@ public void UnregisterPointer(IPointingSource pointingSource)
343330
344331 public FocusDetails ? TryGetFocusDetails ( BaseEventData eventData )
345332 {
346- for ( int iPointer = 0 ; iPointer < pointers . Count ; iPointer ++ )
333+ for ( int i = 0 ; i < pointers . Count ; i ++ )
347334 {
348- PointerData pointer = pointers [ iPointer ] ;
349-
350- if ( pointer . PointingSource . OwnsInput ( eventData ) )
335+ if ( pointers [ i ] . PointingSource . OwnsInput ( eventData ) )
351336 {
352- return pointer . End ;
337+ return pointers [ i ] . End ;
353338 }
354339 }
355340
@@ -368,20 +353,20 @@ public GameObject TryGetFocusedObject(BaseEventData eventData)
368353 IPointingSource pointingSource ;
369354 TryGetPointingSource ( eventData , out pointingSource ) ;
370355 PointerInputEventData pointerInputEventData = GetSpecificPointerEventData ( pointingSource ) ;
356+
357+ Debug . Assert ( pointerInputEventData != null ) ;
371358 pointerInputEventData . selectedObject = details . Value . Object ;
372359
373360 return details . Value . Object ;
374361 }
375362
376363 public bool TryGetPointingSource ( BaseEventData eventData , out IPointingSource pointingSource )
377364 {
378- for ( int iPointer = 0 ; iPointer < pointers . Count ; iPointer ++ )
365+ for ( int i = 0 ; i < pointers . Count ; i ++ )
379366 {
380- PointerData pointer = pointers [ iPointer ] ;
381-
382- if ( pointer . PointingSource . OwnsInput ( eventData ) )
367+ if ( pointers [ i ] . PointingSource . OwnsInput ( eventData ) )
383368 {
384- pointingSource = pointer . PointingSource ;
369+ pointingSource = pointers [ i ] . PointingSource ;
385370 return true ;
386371 }
387372 }
@@ -392,12 +377,28 @@ public bool TryGetPointingSource(BaseEventData eventData, out IPointingSource po
392377
393378 public FocusDetails GetFocusDetails ( IPointingSource pointingSource )
394379 {
395- return GetPointer ( pointingSource ) . End ;
380+ PointerData pointerData ;
381+ FocusDetails details = default ( FocusDetails ) ;
382+
383+ if ( GetPointerData ( pointingSource , out pointerData ) )
384+ {
385+ details = pointerData . End ;
386+ }
387+
388+ return details ;
396389 }
397390
398391 public GameObject GetFocusedObject ( IPointingSource pointingSource )
399392 {
400- return GetPointer ( pointingSource ) . End . Object ;
393+ PointerData pointerData ;
394+ GameObject focusedObject = null ;
395+
396+ if ( GetPointerData ( pointingSource , out pointerData ) )
397+ {
398+ focusedObject = pointerData . End . Object ;
399+ }
400+
401+ return focusedObject ;
401402 }
402403
403404 /// <summary>
@@ -438,12 +439,13 @@ public PointerInputEventData GetGazePointerEventData()
438439
439440 public PointerInputEventData GetSpecificPointerEventData ( IPointingSource pointer )
440441 {
441- return GetPointer ( pointer ) . UnityUIPointerData ;
442+ PointerData pointerEventData ;
443+ return GetPointerData ( pointer , out pointerEventData ) ? pointerEventData . UnityUIPointerData : null ;
442444 }
443445
444446 public float GetPointingExtent ( IPointingSource pointingSource )
445447 {
446- return GetPointingExtent ( GetPointer ( pointingSource ) ) ;
448+ return pointingSource . ExtentOverride ?? pointingExtent ;
447449 }
448450
449451 #endregion
@@ -545,10 +547,13 @@ private void UpdatePointer(PointerData pointer)
545547 /// </summary>
546548 private void RaycastPhysics ( PointerData pointer , LayerMask [ ] prioritizedLayerMasks )
547549 {
548- RaycastHit physicsHit = default ( RaycastHit ) ;
549- RayStep rayStep = default ( RayStep ) ;
550550 bool isHit = false ;
551551 int rayStepIndex = 0 ;
552+ RayStep rayStep = default ( RayStep ) ;
553+ RaycastHit physicsHit = default ( RaycastHit ) ;
554+
555+ Debug . Assert ( pointer . PointingSource . Rays != null , "No valid rays for " + pointer . GetType ( ) ) ;
556+ Debug . Assert ( pointer . PointingSource . Rays . Length > 0 , "No valid rays for " + pointer . GetType ( ) ) ;
552557
553558 // Check raycast for each step in the pointing source
554559 for ( int i = 0 ; i < pointer . PointingSource . Rays . Length ; i ++ )
@@ -570,7 +575,7 @@ private void RaycastPhysics(PointerData pointer, LayerMask[] prioritizedLayerMas
570575 }
571576 else
572577 {
573- pointer . UpdateHit ( GetPointingExtent ( pointer ) ) ;
578+ pointer . UpdateHit ( GetPointingExtent ( pointer . PointingSource ) ) ;
574579 }
575580 }
576581
@@ -609,8 +614,8 @@ private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMas
609614 RayStep rayStep = default ( RayStep ) ;
610615 int rayStepIndex = 0 ;
611616
612- Debug . Assert ( pointer . PointingSource . Rays != null ) ;
613- Debug . Assert ( pointer . PointingSource . Rays . Length > 0 ) ;
617+ Debug . Assert ( pointer . PointingSource . Rays != null , "No valid rays for " + pointer . GetType ( ) ) ;
618+ Debug . Assert ( pointer . PointingSource . Rays . Length > 0 , "No valid rays for " + pointer . GetType ( ) ) ;
614619
615620 // Cast rays for every step until we score a hit
616621 for ( int i = 0 ; i < pointer . PointingSource . Rays . Length ; i ++ )
@@ -624,7 +629,7 @@ private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMas
624629 }
625630
626631 // Check if we need to overwrite the physics raycast info
627- if ( ( pointer . End . Object == null || overridePhysicsRaycast ) && ( uiRaycastResult . module != null && uiRaycastResult . module . eventCamera == UIRaycastCamera ) )
632+ if ( ( pointer . End . Object == null || overridePhysicsRaycast ) && uiRaycastResult . isValid && uiRaycastResult . module . eventCamera == UIRaycastCamera )
628633 {
629634 newUiRaycastPosition . x = uiRaycastResult . screenPosition . x ;
630635 newUiRaycastPosition . y = uiRaycastResult . screenPosition . y ;
@@ -794,32 +799,33 @@ private void RaisePointerSpecificFocusChangedEvents(IPointingSource pointer, Gam
794799 }
795800 }
796801
797- private PointerData GetPointer ( IPointingSource pointingSource )
802+ private bool GetPointerData ( IPointingSource pointingSource , out PointerData pointerData )
798803 {
799- int ? iPointer = TryGetPointerIndex ( pointingSource ) ;
800- Debug . Assert ( iPointer != null ) ;
801- return pointers [ iPointer . Value ] ;
804+ int pointerIndex ;
805+
806+ if ( TryGetPointerIndex ( pointingSource , out pointerIndex ) )
807+ {
808+ pointerData = pointers [ pointerIndex ] ;
809+ return true ;
810+ }
811+
812+ pointerData = null ;
813+ return false ;
802814 }
803815
804- private int ? TryGetPointerIndex ( IPointingSource pointingSource )
816+ private bool TryGetPointerIndex ( IPointingSource pointingSource , out int pointerIndex )
805817 {
806- int ? found = null ;
807-
808818 for ( int i = 0 ; i < pointers . Count ; i ++ )
809819 {
810- if ( pointers [ i ] . PointingSource == pointingSource )
820+ if ( pointingSource == pointers [ i ] . PointingSource )
811821 {
812- found = i ;
813- break ;
822+ pointerIndex = i ;
823+ return true ;
814824 }
815825 }
816826
817- return found ;
818- }
819-
820- private float GetPointingExtent ( PointerData pointer )
821- {
822- return ( pointer . PointingSource . ExtentOverride ?? pointingExtent ) ;
827+ pointerIndex = - 1 ;
828+ return false ;
823829 }
824830
825831 private RaycastHit ? PrioritizeHits ( RaycastHit [ ] hits , LayerMask [ ] layerMasks )
0 commit comments