목차

    CoroutineRunner.cs

    using System.Collections;

    using System.Collections.Generic;

    using UnityEditor;

     

    public static class CoroutineRunner

    {

        private static List _coroutineList = null;

        private static List _bufferList = null;

     

        public static IEnumerator Start(IEnumerator iterator)

        {

            if (_coroutineList == null)

                _coroutineList = new List();

            

            if (_bufferList == null)

                _bufferList = new List();

            

            if (_coroutineList.Count == 0)

                EditorApplication.update += Update;

            

            _bufferList.Add(iterator);

     

            return iterator;

        }

     

        private static bool Find(IEnumerator iterator)

        {

            foreach (EditorCoroutine coroutine in _coroutineList)

            {

                if (coroutine.Find(iterator) == true)

                    return true;

            }

     

            return false;

        }

     

        private static void Update()

        {

            _coroutineList.RemoveAll

            (

                coroutine => { return coroutine.MoveNext() == false; }

            );

     

            if (_bufferList.Count > 0)

            {

                foreach (IEnumerator iterator in _bufferList)

                {

                    if (Find(iterator) == false)

                        _coroutineList.Add(new EditorCoroutine(iterator));

                }

     

                _bufferList.Clear();

            }

     

            if (_coroutineList.Count == 0)

            {

                EditorApplication.update -= Update;

            }

        }

     

        private class EditorCoroutine : IEnumerator

        {

            private Stack _stack;

     

            public EditorCoroutine(IEnumerator iterator)

            {

                _stack = new Stack();

                _stack.Push(iterator);

            }

     

            public bool MoveNext()

            {

                IEnumerator i = _stack.Peek();

     

                if (i.MoveNext() == true)

                {

                    object result = i.Current;

                    if (result != null && result is IEnumerator)

                    {

                        _stack.Push((IEnumerator)result);

                    }

     

                    return true;

                }

                else

                {

                    if (_stack.Count > 1)

                    {

                        _stack.Pop();

     

                        return true;

                    }

                }

     

                return false;

            }

     

            public void Reset()

            {

                throw new System.NotSupportedException(

                          "This Operation Is Not Supported.");

            }

     

            public object Current

            {

                get { return _stack.Peek().Current; }

            }

     

            public bool Find(IEnumerator iterator)

            {

                return _stack.Contains(iterator);

            }

        }

    }