public void StopCoroutine(CoroutineNode node) { if (_dicCoroutines.ContainsKey(node.Id)) { _dicCoroutines.Remove(node.Id); } }
/** * Starts a coroutine, the coroutine does not run immediately but on the * next call to UpdateAllCoroutines. The execution of a coroutine can * be paused at any point using the yield statement. The yield return value * specifies when the coroutine is resumed. */ public CoroutineNode StartCoroutine(IEnumerator fiber) { // if function does not have a yield, fiber will be null and we no-op if (fiber == null) { return(null); } // create coroutine node and run until we reach first yield CoroutineNode coroutine = new CoroutineNode(fiber, _idx++); AddCoroutine(coroutine); return(coroutine); }
/** * Runs all active coroutines until their next yield. Caller must provide * the current frame and time. This allows for schedulers to run under * frame and time regimes other than the Unity's main game loop. */ public void UpdateAllCoroutines(int time) { if (currentTime != time) { currentFrame++; } currentTime = time; foreach (KeyValuePair <int, CoroutineNode> kv in _dicCoroutines) { // store listNext before coroutine finishes and is removed from the list CoroutineNode coroutine = kv.Value; if (coroutine.waitForFrame > 0 && currentFrame >= coroutine.waitForFrame) { coroutine.waitForFrame = -1; UpdateCoroutine(coroutine); } else if (coroutine.waitForTime > 0 && time >= coroutine.waitForTime) { coroutine.waitForTime = -1; UpdateCoroutine(coroutine); } else if (coroutine.waitForCoroutine != null && coroutine.waitForCoroutine.finished) { coroutine.waitForCoroutine = null; UpdateCoroutine(coroutine); } else if (coroutine.waitForUnityObject != null && coroutine.waitForUnityObject.finished)//lonewolfwilliams { coroutine.waitForUnityObject = null; UpdateCoroutine(coroutine); } else if (coroutine.waitForFrame == -1 && coroutine.waitForTime == -1 && coroutine.waitForCoroutine == null && coroutine.waitForUnityObject == null) { // initial update UpdateCoroutine(coroutine); } } foreach (int node in _tobeRemovedNodes) { _dicCoroutines.Remove(node); } _tobeRemovedNodes.Clear(); }
private void RemoveCoroutine(CoroutineNode coroutine) { IEnumerator fiber = coroutine.fiber; if (coroutine.fiber.MoveNext()) { if (fiber.Current == null)//next frame { coroutine.waitForFrame = currentFrame + 1; return; } System.Object yieldCommand = fiber.Current; if (yieldCommand.GetType() == typeof(WaitForFrames)) { WaitForFrames wff = yieldCommand as WaitForFrames; coroutine.waitForFrame = currentFrame + wff.frames; } else if (yieldCommand.GetType() == typeof(WaitForMS)) { WaitForMS wfms = yieldCommand as WaitForMS; coroutine.waitForTime = currentTime + wfms.ms; } else if (yieldCommand.GetType() == typeof(CoroutineNode)) { coroutine.waitForCoroutine = (CoroutineNode)yieldCommand; } else if (yieldCommand is IYieldWrapper) //lonewolfwilliams { coroutine.waitForUnityObject = yieldCommand as IYieldWrapper; } else { throw new System.ArgumentException("CoroutineScheduler: Unexpected coroutine yield type: " + yieldCommand.GetType()); } } else { // coroutine finished coroutine.finished = true; // StopCoroutine(coroutine); _tobeRemovedNodes.Add(coroutine.Id); } }
private void AddCoroutine(CoroutineNode coroutine) { _dicCoroutines[coroutine.Id] = coroutine; }