@@ -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
@@ -537,10 +539,13 @@ private void UpdatePointer(PointerData pointer)
537539 /// </summary>
538540 private void RaycastPhysics ( PointerData pointer , LayerMask [ ] prioritizedLayerMasks )
539541 {
540- RaycastHit physicsHit = default ( RaycastHit ) ;
541- RayStep rayStep = default ( RayStep ) ;
542542 bool isHit = false ;
543543 int rayStepIndex = 0 ;
544+ RayStep rayStep = default ( RayStep ) ;
545+ RaycastHit physicsHit = default ( RaycastHit ) ;
546+
547+ Debug . Assert ( pointer . PointingSource . Rays != null , "No valid rays for " + pointer . GetType ( ) ) ;
548+ Debug . Assert ( pointer . PointingSource . Rays . Length > 0 , "No valid rays for " + pointer . GetType ( ) ) ;
544549
545550 // Check raycast for each step in the pointing source
546551 for ( int i = 0 ; i < pointer . PointingSource . Rays . Length ; i ++ )
@@ -562,7 +567,7 @@ private void RaycastPhysics(PointerData pointer, LayerMask[] prioritizedLayerMas
562567 }
563568 else
564569 {
565- pointer . UpdateHit ( GetPointingExtent ( pointer ) ) ;
570+ pointer . UpdateHit ( GetPointingExtent ( pointer . PointingSource ) ) ;
566571 }
567572 }
568573
@@ -601,6 +606,9 @@ private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMas
601606 RayStep rayStep = default ( RayStep ) ;
602607 int rayStepIndex = 0 ;
603608
609+ Debug . Assert ( pointer . PointingSource . Rays != null , "No valid rays for " + pointer . GetType ( ) ) ;
610+ Debug . Assert ( pointer . PointingSource . Rays . Length > 0 , "No valid rays for " + pointer . GetType ( ) ) ;
611+
604612 // Cast rays for every step until we score a hit
605613 for ( int i = 0 ; i < pointer . PointingSource . Rays . Length ; i ++ )
606614 {
@@ -613,7 +621,7 @@ private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMas
613621 }
614622
615623 // Check if we need to overwrite the physics raycast info
616- if ( ( pointer . End . Object == null || overridePhysicsRaycast ) && uiRaycastResult . module . eventCamera == UIRaycastCamera )
624+ if ( ( pointer . End . Object == null || overridePhysicsRaycast ) && uiRaycastResult . isValid && uiRaycastResult . module . eventCamera == UIRaycastCamera )
617625 {
618626 newUiRaycastPosition . x = uiRaycastResult . screenPosition . x ;
619627 newUiRaycastPosition . y = uiRaycastResult . screenPosition . y ;
@@ -783,32 +791,33 @@ private void RaisePointerSpecificFocusChangedEvents(IPointingSource pointer, Gam
783791 }
784792 }
785793
786- private PointerData GetPointer ( IPointingSource pointingSource )
794+ private bool GetPointerData ( IPointingSource pointingSource , out PointerData pointerData )
787795 {
788- int ? iPointer = TryGetPointerIndex ( pointingSource ) ;
789- Debug . Assert ( iPointer != null ) ;
790- return pointers [ iPointer . Value ] ;
796+ int pointerIndex ;
797+
798+ if ( TryGetPointerIndex ( pointingSource , out pointerIndex ) )
799+ {
800+ pointerData = pointers [ pointerIndex ] ;
801+ return true ;
802+ }
803+
804+ pointerData = null ;
805+ return false ;
791806 }
792807
793- private int ? TryGetPointerIndex ( IPointingSource pointingSource )
808+ private bool TryGetPointerIndex ( IPointingSource pointingSource , out int pointerIndex )
794809 {
795- int ? found = null ;
796-
797810 for ( int i = 0 ; i < pointers . Count ; i ++ )
798811 {
799- if ( pointers [ i ] . PointingSource == pointingSource )
812+ if ( pointingSource == pointers [ i ] . PointingSource )
800813 {
801- found = i ;
802- break ;
814+ pointerIndex = i ;
815+ return true ;
803816 }
804817 }
805818
806- return found ;
807- }
808-
809- private float GetPointingExtent ( PointerData pointer )
810- {
811- return ( pointer . PointingSource . ExtentOverride ?? pointingExtent ) ;
819+ pointerIndex = - 1 ;
820+ return false ;
812821 }
813822
814823 private RaycastHit ? PrioritizeHits ( RaycastHit [ ] hits , LayerMask [ ] layerMasks )
0 commit comments