Skip to content

Commit b7aa674

Browse files
author
David Kline
authored
Merge pull request #2734 from StephenHodgson/vNEXT-BaseInputHandlerRaceCondition
Fixed a race condition with BaseInputHandler
2 parents 008a709 + 535fc0a commit b7aa674

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

Assets/MixedRealityToolkit-SDK/Features/Input/GazeProvider.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
using Microsoft.MixedReality.Toolkit.InputSystem.Pointers;
5-
using Microsoft.MixedReality.Toolkit.InputSystem.Sources;
64
using Microsoft.MixedReality.Toolkit.Core.Definitions.InputSystem;
75
using Microsoft.MixedReality.Toolkit.Core.Definitions.Utilities;
86
using Microsoft.MixedReality.Toolkit.Core.EventDatum.Input;
97
using Microsoft.MixedReality.Toolkit.Core.Interfaces.Devices;
108
using Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem;
119
using Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem.Handlers;
1210
using Microsoft.MixedReality.Toolkit.Core.Utilities;
11+
using Microsoft.MixedReality.Toolkit.Core.Utilities.Async;
1312
using Microsoft.MixedReality.Toolkit.Core.Utilities.Physics;
13+
using Microsoft.MixedReality.Toolkit.InputSystem.Pointers;
14+
using Microsoft.MixedReality.Toolkit.InputSystem.Sources;
1415
using UnityEngine;
1516

1617
namespace Microsoft.MixedReality.Toolkit.SDK.Input
@@ -268,8 +269,10 @@ protected override void OnEnable()
268269
}
269270
}
270271

271-
protected virtual void Start()
272+
protected override void Start()
272273
{
274+
base.Start();
275+
273276
if (cursorPrefab != null)
274277
{
275278
var cursorObj = Instantiate(cursorPrefab, transform.parent);
@@ -335,7 +338,7 @@ protected override void OnDisable()
335338
{
336339
base.OnDisable();
337340
GazePointer.BaseCursor?.SetVisibility(false);
338-
InputSystem.RaiseSourceLost(GazeInputSource);
341+
InputSystem?.RaiseSourceLost(GazeInputSource);
339342
}
340343

341344
#endregion MonoBehaviour Implementation
@@ -385,8 +388,9 @@ private IMixedRealityPointer InitializeGazePointer()
385388
return gazePointer = new InternalGazePointer(this, "Gaze Pointer", null, raycastLayerMasks, maxGazeCollisionDistance, gazeTransform, stabilizer);
386389
}
387390

388-
private void RaiseSourceDetected()
391+
private async void RaiseSourceDetected()
389392
{
393+
await WaitUntilInputSystemValid;
390394
InputSystem.RaiseSourceDetected(GazeInputSource);
391395
GazePointer.BaseCursor?.SetVisibility(true);
392396
}

Assets/MixedRealityToolkit-SDK/Features/Input/Handlers/SpeechInputHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public class SpeechInputHandler : BaseInputHandler, IMixedRealitySpeechHandler
3333

3434
#region MonoBehaviour Implementation
3535

36-
private void Start()
36+
protected override void Start()
3737
{
38+
base.Start();
39+
3840
if (persistentKeywords)
3941
{
4042
Debug.Assert(gameObject.transform.parent == null, "Persistent keyword GameObject must be at the root level of the scene hierarchy.");

Assets/MixedRealityToolkit-SDK/Features/Input/InputSystemGlobalListener.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.MixedReality.Toolkit.Core.Interfaces.InputSystem;
55
using Microsoft.MixedReality.Toolkit.Core.Managers;
6+
using Microsoft.MixedReality.Toolkit.Core.Utilities.Async;
67
using UnityEngine;
78

89
namespace Microsoft.MixedReality.Toolkit.SDK.Input
@@ -15,16 +16,31 @@ public class InputSystemGlobalListener : MonoBehaviour
1516
private static IMixedRealityInputSystem inputSystem = null;
1617
protected static IMixedRealityInputSystem InputSystem => inputSystem ?? (inputSystem = MixedRealityManager.Instance.GetManager<IMixedRealityInputSystem>());
1718

19+
private bool lateInitialize = true;
20+
21+
protected readonly WaitUntil WaitUntilInputSystemValid = new WaitUntil(() => InputSystem != null);
22+
1823
protected virtual void OnEnable()
1924
{
20-
Debug.Assert(MixedRealityManager.IsInitialized, "No Mixed Reality Manager found in the scene. Be sure to run the Mixed Reality Configuration.");
21-
Debug.Assert(InputSystem != null, "No Input System found, Did you set it up in your configuration profile?");
22-
InputSystem.Register(gameObject);
25+
if (MixedRealityManager.IsInitialized && InputSystem != null && !lateInitialize)
26+
{
27+
InputSystem.Register(gameObject);
28+
}
29+
}
30+
31+
protected virtual async void Start()
32+
{
33+
if (lateInitialize)
34+
{
35+
await WaitUntilInputSystemValid;
36+
lateInitialize = false;
37+
InputSystem.Register(gameObject);
38+
}
2339
}
2440

2541
protected virtual void OnDisable()
2642
{
27-
InputSystem.Unregister(gameObject);
43+
InputSystem?.Unregister(gameObject);
2844
}
2945
}
3046
}

Assets/MixedRealityToolkit-SDK/Features/UX/Scripts/Pointers/BaseControllerPointer.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.MixedReality.Toolkit.Core.Managers;
1414
using Microsoft.MixedReality.Toolkit.SDK.Input.Handlers;
1515
using System.Collections;
16+
using Microsoft.MixedReality.Toolkit.Core.Utilities.Async;
1617
using UnityEngine;
1718

1819
namespace Microsoft.MixedReality.Toolkit.SDK.UX.Pointers
@@ -61,6 +62,8 @@ public abstract class BaseControllerPointer : ControllerPoseSynchronizer, IMixed
6162

6263
protected bool IsTeleportRequestActive = false;
6364

65+
private bool lateRegisterTeleport = true;
66+
6467
/// <summary>
6568
/// The forward direction of the targeting ray
6669
/// </summary>
@@ -116,7 +119,23 @@ protected override void OnEnable()
116119
base.OnEnable();
117120
SetCursor();
118121
BaseCursor?.SetVisibility(true);
119-
TeleportSystem?.Register(gameObject);
122+
123+
if (MixedRealityManager.IsInitialized && TeleportSystem != null && !lateRegisterTeleport)
124+
{
125+
TeleportSystem.Register(gameObject);
126+
}
127+
}
128+
129+
protected override async void Start()
130+
{
131+
base.Start();
132+
133+
if (lateRegisterTeleport)
134+
{
135+
await new WaitUntil(() => TeleportSystem != null);
136+
lateRegisterTeleport = false;
137+
TeleportSystem.Register(gameObject);
138+
}
120139
}
121140

122141
protected override void OnDisable()
@@ -171,7 +190,7 @@ public string PointerName
171190
}
172191

173192
/// <inheritdoc />
174-
public IMixedRealityInputSource InputSourceParent { get; private set; }
193+
public IMixedRealityInputSource InputSourceParent { get; protected set; }
175194

176195
/// <inheritdoc />
177196
public IMixedRealityCursor BaseCursor { get; set; }

Assets/MixedRealityToolkit-SDK/Features/UX/Scripts/Pointers/TeleportPointer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public override void OnPositionInputChanged(InputEventData<Vector2> eventData)
290290
{
291291
teleportEnabled = true;
292292

293-
TeleportSystem.RaiseTeleportRequest(this, TeleportHotSpot);
293+
TeleportSystem?.RaiseTeleportRequest(this, TeleportHotSpot);
294294
}
295295
else if (canMove)
296296
{
@@ -356,15 +356,15 @@ public override void OnPositionInputChanged(InputEventData<Vector2> eventData)
356356
if (TeleportSurfaceResult == TeleportSurfaceResult.Valid ||
357357
TeleportSurfaceResult == TeleportSurfaceResult.HotSpot)
358358
{
359-
TeleportSystem.RaiseTeleportStarted(this, TeleportHotSpot);
359+
TeleportSystem?.RaiseTeleportStarted(this, TeleportHotSpot);
360360
}
361361
}
362362

363363
if (teleportEnabled)
364364
{
365365
canTeleport = false;
366366
teleportEnabled = false;
367-
TeleportSystem.RaiseTeleportCanceled(this, TeleportHotSpot);
367+
TeleportSystem?.RaiseTeleportCanceled(this, TeleportHotSpot);
368368
}
369369
}
370370

0 commit comments

Comments
 (0)