Skip to content

Commit 8289947

Browse files
committed
Fix test failures and more improvements
1 parent 6c64987 commit 8289947

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

Assets/MRTK/Core/Utilities/Async/AwaiterExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,23 @@ private static void RunOnUnityScheduler(Action action)
141141
{
142142
if (SynchronizationContext.Current == SyncContextUtility.UnitySynchronizationContext)
143143
{
144+
// Take the opportunity to ensure AsyncCoroutineRunner is running when we are on the Unity thread
145+
if (!AsyncCoroutineRunner.IsInstanceRunning)
146+
{
147+
var _ = AsyncCoroutineRunner.Instance;
148+
}
144149
action();
145150
}
146151
else
147152
{
148-
// Make sure there is an instance of AsyncCoroutineRunner before calling AsyncCoroutineRunner.Post
149-
var _ = AsyncCoroutineRunner.Instance;
153+
// Make sure there is a running instance of AsyncCoroutineRunner before calling AsyncCoroutineRunner.Post
154+
// If not warn the user. Note we cannot call AsyncCoroutineRunner.Instance here as that getter contains
155+
// calls to Unity functions that can only be run on the Unity thread
156+
if (!AsyncCoroutineRunner.IsInstanceRunning)
157+
{
158+
Debug.LogWarning("There is no active AsyncCoroutineRunner when an action is posted. Place a GameObject " +
159+
"at the root of the scene and attach the AsyncCoroutineRunner script to make it function properly.");
160+
}
150161
AsyncCoroutineRunner.Post(action);
151162
}
152163
}

Assets/MRTK/Core/Utilities/Async/Internal/AsyncCoroutineRunner.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ internal sealed class AsyncCoroutineRunner : MonoBehaviour
4343
{
4444
private static AsyncCoroutineRunner instance;
4545

46+
private static bool isInstanceRunning = false;
47+
4648
private static readonly Queue<Action> Actions = new Queue<Action>();
4749

4850
internal static AsyncCoroutineRunner Instance
@@ -51,10 +53,17 @@ internal static AsyncCoroutineRunner Instance
5153
{
5254
if (instance == null)
5355
{
54-
instance = FindObjectOfType<AsyncCoroutineRunner>();
56+
AsyncCoroutineRunner[] instances = FindObjectsOfType<AsyncCoroutineRunner>();
57+
Debug.Assert(instances.Length <= 1, "[AsyncCoroutineRunner] There should only be one AsyncCoroutineRunner in the scene.");
58+
instance = instances.Length == 1 ? instances[0] : null;
59+
if (instance != null && !instance.enabled)
60+
{
61+
Debug.LogWarning("[AsyncCoroutineRunner] Found a disabled AsyncCoroutineRunner component. Enabling the component.");
62+
instance.enabled = true;
63+
}
5564
}
5665

57-
// FindObjectOfType() only search for active objects. The FindObjectOfType(bool includeInactive) variant is not available to Unity 2019.4 and earlier so cannot be used.
66+
// FindObjectOfType() only search for objects attached to active GameObjects. The FindObjectOfType(bool includeInactive) variant is not available to Unity 2019.4 and earlier so cannot be used.
5867
// We instead search for GameObject called AsyncCoroutineRunner and see if it has the component attached.
5968
if (instance == null)
6069
{
@@ -117,9 +126,12 @@ internal static void Post(Action task)
117126
}
118127
}
119128

129+
internal static bool IsInstanceRunning => isInstanceRunning;
130+
120131
private void Update()
121132
{
122-
Debug.Assert(Instance != null);
133+
Debug.Assert(Instance == this, "[AsyncCoroutineRunner] There should only be one AsyncCoroutineRunner in the scene.");
134+
isInstanceRunning = true;
123135

124136
int actionCount;
125137

@@ -140,5 +152,19 @@ private void Update()
140152
next();
141153
}
142154
}
155+
156+
private void OnDisable()
157+
{
158+
if (instance == this)
159+
{
160+
isInstanceRunning = false;
161+
}
162+
}
163+
164+
private void Awake()
165+
{
166+
Debug.Assert(Instance == this, "[AsyncCoroutineRunner] There should only be one AsyncCoroutineRunner in the scene.");
167+
isInstanceRunning = true;
168+
}
143169
}
144170
}

0 commit comments

Comments
 (0)