void OnUpdate() { float deltaTime = (float)(DateTime.Now.Subtract(previousTimeSinceStartup).TotalMilliseconds / 1000.0f); previousTimeSinceStartup = DateTime.Now; if (coroutineDict.Count == 0) { return; } tempCoroutineList.Clear(); foreach (var pair in coroutineDict) { tempCoroutineList.Add(pair.Value); } for (var i = tempCoroutineList.Count - 1; i >= 0; i--) { List <EditorCoroutine> coroutines = tempCoroutineList[i]; for (int j = coroutines.Count - 1; j >= 0; j--) { EditorCoroutine coroutine = coroutines[j]; bool isDone = coroutine.currentYield.IsDone(deltaTime); if (!isDone || coroutine.errored) { continue; } try // Move next may fail { if (!MoveNext(coroutine)) { coroutines.RemoveAt(j); coroutine.currentYield = null; coroutine.finished = true; } if (coroutines.Count == 0) { coroutineDict.Remove(coroutine.ownerUniqueHash); } } catch (Exception) { // Track the coroutine as having errored coroutine.errored = true; throw; // Rethrow the exception now that we have tracked the coroutine exception } } } // Cleanup coroutines if they or any of their dependent children have errored for (var i = 0; i < tempCoroutineList.Count; i++) { List <EditorCoroutine> coroutines = tempCoroutineList[i]; for (int j = 0; j < coroutines.Count; j++) { EditorCoroutine coroutine = coroutines[j]; if (coroutine.HasErroredRecursive()) { coroutines.RemoveAt(j); coroutine.currentYield = null; coroutine.finished = true; } if (coroutines.Count == 0) { coroutineDict.Remove(coroutine.ownerUniqueHash); } } } }