Skip to content

Commit 78d4d44

Browse files
Merge pull request #1350 from StephenHodgson/MRTK-uiRaycastHotfix
Added check for valid uiRaycastResults and fixed pointer registration
2 parents 98f4985 + 87ddac5 commit 78d4d44

File tree

5 files changed

+90
-87
lines changed

5 files changed

+90
-87
lines changed

Assets/HoloToolkit/Input/Scripts/Focus/FocusManager.cs

Lines changed: 78 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

Assets/HoloToolkit/Input/Scripts/Focus/InputSourcePointer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public bool InteractionEnabled
5151
[Obsolete("Will be removed in a later version. Use OnPreRaycast / OnPostRaycast instead.")]
5252
public void UpdatePointer()
5353
{
54-
5554
}
5655

5756
public virtual void OnPreRaycast()
@@ -62,9 +61,9 @@ public virtual void OnPreRaycast()
6261
}
6362
else
6463
{
65-
Debug.Assert(InputSource.SupportsInputInfo(InputSourceId, SupportedInputInfo.Pointing));
64+
Debug.Assert(InputSource.SupportsInputInfo(InputSourceId, SupportedInputInfo.Pointing), string.Format("{0} with id {1} does not support pointing!", InputSource, InputSourceId));
6665

67-
Ray pointingRay = default(Ray);
66+
Ray pointingRay;
6867
if (InputSource.TryGetPointingRay(InputSourceId, out pointingRay))
6968
{
7069
rays[0].CopyRay(pointingRay, FocusManager.Instance.GetPointingExtent(this));

Assets/HoloToolkit/Input/Scripts/Focus/SimpleSinglePointerSelector.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ private void Start()
4545
started = true;
4646

4747
InputManager.AssertIsInitialized();
48-
GazeManager.AssertIsInitialized();
4948
FocusManager.AssertIsInitialized();
49+
GazeManager.AssertIsInitialized();
5050

5151
AddInputManagerListenerIfNeeded();
5252
FindCursorIfNeeded();
5353
ConnectBestAvailablePointer();
54-
55-
Debug.Assert(currentPointer != null, this);
5654
}
5755

5856
private void OnEnable()
@@ -173,6 +171,8 @@ private void SetPointer(IPointingSource newPointer)
173171
Cursor.Pointer = newPointer;
174172
}
175173
}
174+
175+
Debug.Assert(currentPointer != null, "No Pointer Set!");
176176
}
177177

178178
private void ConnectBestAvailablePointer()
@@ -228,7 +228,6 @@ private void HandleInputAction(InputEventData eventData)
228228
// TODO: robertes: see if we can treat voice separately from the other simple committers,
229229
// so voice doesn't steal from a pointing controller. I think input Kind would need
230230
// to come through with the event data.
231-
232231
SetPointer(GazeManager.Instance);
233232
pointerWasChanged = true;
234233
}

Assets/HoloToolkit/Input/Scripts/Gaze/GazeManager.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ public Vector3 GazeNormal
113113
[Tooltip("True to draw a debug view of the ray.")]
114114
public bool DebugDrawRay;
115115
public PointerResult Result { get; set; }
116-
116+
117117
[Obsolete("Will be removed in a later version. Use Rays instead.")]
118118
public Ray Ray { get { return Rays[0]; } }
119-
119+
120120
public RayStep[] Rays { get { return rays; } }
121121

122122
private RayStep[] rays = new RayStep[1] { new RayStep(Vector3.zero, Vector3.zero) };
123-
123+
124124
public float? ExtentOverride
125125
{
126126
get { return MaxGazeCollisionDistance; }
@@ -133,10 +133,7 @@ public LayerMask[] PrioritizedLayerMasksOverride
133133

134134
public bool InteractionEnabled
135135
{
136-
get
137-
{
138-
return true;
139-
}
136+
get { return true; }
140137
}
141138

142139
private float lastHitDistance = 2.0f;
@@ -203,7 +200,7 @@ private void UpdateGazeInfo()
203200
newGazeNormal = Stabilizer.StableRay.direction;
204201
}
205202

206-
Rays[0].UpdateRayStep(newGazeOrigin, newGazeOrigin + (newGazeNormal * FocusManager.Instance.GetPointingExtent (this)));
203+
Rays[0].UpdateRayStep(newGazeOrigin, newGazeOrigin + (newGazeNormal * FocusManager.Instance.GetPointingExtent(this)));
207204
}
208205

209206
UpdateHitPosition();
@@ -212,7 +209,6 @@ private void UpdateGazeInfo()
212209
[Obsolete("Will be removed in a later version. Use OnPreRaycast / OnPostRaycast instead.")]
213210
public void UpdatePointer()
214211
{
215-
216212
}
217213

218214
public virtual void OnPreRaycast()

Assets/HoloToolkit/Input/Scripts/Utilities/Interactions/CustomInputSelector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ private void Awake()
5454
bool spawnControllers = false;
5555

5656
#if UNITY_2017_2_OR_NEWER
57-
spawnControllers = (!XRDevice.isPresent && XRSettings.enabled || Application.isEditor) && simulateHandsInEditor;
57+
spawnControllers = !XRDevice.isPresent && XRSettings.enabled && simulateHandsInEditor;
5858
#else
59-
spawnControllers = !VRDevice.isPresent;
59+
spawnControllers = simulateHandsInEditor;
6060
#endif
6161
if (spawnControllers)
6262
{

0 commit comments

Comments
 (0)