Skip to content

Commit c09f5ef

Browse files
committed
Better handling of multiple AsyncCoroutineRunner
1 parent f49902b commit c09f5ef

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,7 @@ internal static AsyncCoroutineRunner Instance
5353
{
5454
if (instance == null)
5555
{
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-
}
56+
instance = FindObjectOfType<AsyncCoroutineRunner>();
6457
}
6558

6659
// 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.
@@ -78,19 +71,6 @@ internal static AsyncCoroutineRunner Instance
7871
Debug.Log("[AsyncCoroutineRunner] Found a \"AsyncCoroutineRunner\" GameObject but didn't have the AsyncCoroutineRunner component attached. Attaching the script.");
7972
instance = instanceGameObject.AddComponent<AsyncCoroutineRunner>();
8073
}
81-
else
82-
{
83-
if (!instance.enabled)
84-
{
85-
Debug.LogWarning("[AsyncCoroutineRunner] Found a disabled AsyncCoroutineRunner component. Enabling the component.");
86-
instance.enabled = true;
87-
}
88-
if (!instanceGameObject.activeSelf)
89-
{
90-
Debug.LogWarning("[AsyncCoroutineRunner] Found an AsyncCoroutineRunner attached to an inactive GameObject. Setting the GameObject active.");
91-
instanceGameObject.SetActive(true);
92-
}
93-
}
9474
}
9575
}
9676

@@ -99,6 +79,19 @@ internal static AsyncCoroutineRunner Instance
9979
Debug.Log("[AsyncCoroutineRunner] There is no AsyncCoroutineRunner in the scene. Adding a GameObject with AsyncCoroutineRunner attached at the root of the scene.");
10080
instance = new GameObject("AsyncCoroutineRunner").AddComponent<AsyncCoroutineRunner>();
10181
}
82+
else if (!instance.isActiveAndEnabled)
83+
{
84+
if (!instance.enabled)
85+
{
86+
Debug.LogWarning("[AsyncCoroutineRunner] Found a disabled AsyncCoroutineRunner component. Enabling the component.");
87+
instance.enabled = true;
88+
}
89+
if (!instance.gameObject.activeSelf)
90+
{
91+
Debug.LogWarning("[AsyncCoroutineRunner] Found an AsyncCoroutineRunner attached to an inactive GameObject. Setting the GameObject active.");
92+
instance.gameObject.SetActive(true);
93+
}
94+
}
10295

10396
instance.gameObject.hideFlags = HideFlags.None;
10497

@@ -130,7 +123,11 @@ internal static void Post(Action task)
130123

131124
private void Update()
132125
{
133-
Debug.Assert(Instance == this, "[AsyncCoroutineRunner] There should only be one AsyncCoroutineRunner in the scene.");
126+
if (Instance != this)
127+
{
128+
enabled = false;
129+
return;
130+
}
134131
isInstanceRunning = true;
135132

136133
int actionCount;
@@ -163,8 +160,15 @@ private void OnDisable()
163160

164161
private void OnEnable()
165162
{
166-
Debug.Assert(Instance == this, "[AsyncCoroutineRunner] There should only be one AsyncCoroutineRunner in the scene.");
167-
isInstanceRunning = true;
163+
if (Instance != this)
164+
{
165+
Debug.Log("[AsyncCoroutineRunner] Multiple active AsyncCoroutineRunners is present in the scene. Disabling duplicate ones.");
166+
enabled = false;
167+
}
168+
else
169+
{
170+
isInstanceRunning = true;
171+
}
168172
}
169173
}
170174
}

0 commit comments

Comments
 (0)