Last active
October 9, 2017 13:08
-
-
Save nicloay/eb578834890d34c5b720350d48ab6fd5 to your computer and use it in GitHub Desktop.
You can use this as yield return CoroutineWatier(YourCoroutineHere());
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
public class CoroutineWaiter : IEnumerator { | |
public bool IsFinished { get; private set; } | |
private readonly Action _onFinish; | |
readonly Stack<IEnumerator> _stack= new Stack<IEnumerator>(); | |
public CoroutineWaiter(IEnumerator enumerator, Action onFinish = null) | |
{ | |
IsFinished = false; | |
_onFinish = onFinish; | |
_stack.Push(enumerator); | |
} | |
public bool MoveNext() | |
{ | |
UnityEngine.Debug.Log("move next"); | |
while (_stack.Count > 0 && !_stack.Peek().MoveNext()) | |
{ | |
_stack.Pop(); | |
} | |
if (_stack.Count == 0) | |
{ | |
IsFinished = true; | |
if (_onFinish != null) | |
{ | |
_onFinish(); | |
} | |
return false; | |
} | |
IEnumerator activeEnum = _stack.Peek(); | |
if (activeEnum.Current != activeEnum && activeEnum.Current is IEnumerator ) | |
{ | |
_stack.Push(activeEnum.Current as IEnumerator); | |
} | |
return true; | |
} | |
public void Reset() | |
{ | |
_stack.Clear(); | |
} | |
public object Current | |
{ | |
get { return _stack.Peek(); } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment