Skip to content

Commit 6c64987

Browse files
committed
Improve the get instance logic
1 parent 9c76ae2 commit 6c64987

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ private static void RunOnUnityScheduler(Action action)
145145
}
146146
else
147147
{
148+
// Make sure there is an instance of AsyncCoroutineRunner before calling AsyncCoroutineRunner.Post
148149
var _ = AsyncCoroutineRunner.Instance;
149150
AsyncCoroutineRunner.Post(action);
150151
}

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,40 @@ internal static AsyncCoroutineRunner Instance
5454
instance = FindObjectOfType<AsyncCoroutineRunner>();
5555
}
5656

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.
58+
// We instead search for GameObject called AsyncCoroutineRunner and see if it has the component attached.
5759
if (instance == null)
5860
{
59-
instance = FindObjectOfType<AsyncCoroutineRunner>(true);
61+
var instanceGameObject = GameObject.Find("AsyncCoroutineRunner");
6062

61-
if (instance != null)
63+
if (instanceGameObject != null)
6264
{
63-
Debug.LogError($"GameObject {instance.gameObject.name} has an inactive AsyncCoroutineRunner attached and " +
64-
"there is no active AsyncCoroutineRunner in the scene. There must be an active AsyncCoroutineRunner attached to a GameObject at the root of the scene.");
65-
return instance;
65+
instance = instanceGameObject.GetComponent<AsyncCoroutineRunner>();
66+
67+
if (instance == null)
68+
{
69+
Debug.Log("[AsyncCoroutineRunner] Found a \"AsyncCoroutineRunner\" GameObject but didn't have the AsyncCoroutineRunner component attached. Attaching the script.");
70+
instance = instanceGameObject.AddComponent<AsyncCoroutineRunner>();
71+
}
72+
else
73+
{
74+
if (!instance.enabled)
75+
{
76+
Debug.LogWarning("[AsyncCoroutineRunner] Found a disabled AsyncCoroutineRunner component. Enabling the component.");
77+
instance.enabled = true;
78+
}
79+
if (!instanceGameObject.activeSelf)
80+
{
81+
Debug.LogWarning("[AsyncCoroutineRunner] Found an AsyncCoroutineRunner attached to an inactive GameObject. Setting the GameObject active.");
82+
instanceGameObject.SetActive(true);
83+
}
84+
}
6685
}
6786
}
6887

6988
if (instance == null)
7089
{
71-
Debug.LogWarning("There is no AsyncCoroutineRunner in the scene. Adding a GameObject with AsyncCoroutineRunner attached at the root of the scene.");
90+
Debug.Log("[AsyncCoroutineRunner] There is no AsyncCoroutineRunner in the scene. Adding a GameObject with AsyncCoroutineRunner attached at the root of the scene.");
7291
instance = new GameObject("AsyncCoroutineRunner").AddComponent<AsyncCoroutineRunner>();
7392
}
7493

@@ -78,15 +97,14 @@ internal static AsyncCoroutineRunner Instance
7897
// This is ultimately to ensure that it persists across scene loads/unloads.
7998
if (instance.transform.parent != null)
8099
{
81-
Debug.LogWarning($"AsyncCoroutineRunner was found as a child of another GameObject {instance.transform.parent}, " +
100+
Debug.LogWarning($"[AsyncCoroutineRunner] AsyncCoroutineRunner was found as a child of another GameObject {instance.transform.parent}, " +
82101
"it must be a root object in the scene. Moving the AsyncCoroutineRunner to the root.");
83102
instance.transform.parent = null;
84103
}
85104

86105
#if !UNITY_EDITOR
87106
DontDestroyOnLoad(instance);
88107
#endif
89-
90108
return instance;
91109
}
92110
}

0 commit comments

Comments
 (0)