unity协程的实现

using System;
using System.Collections;
using System.Collections.Generic;

namespace CSharpearning
{
    public class Coroutine
    {
        internal IEnumerator enumerator;
        internal IEnumerator Enumerator{
            get { return enumerator; }
        }
        public Coroutine()
        {
        }
        public Coroutine(IEnumerator routine){
            this.enumerator = routine;
        }
        internal bool MoveNext(){
            var rator = enumerator.Current as Coroutine;
            if(rator!=null){
                if(rator.MoveNext()){
                    return true;
                }else{
                    return false;
                }
            }else{
                if (enumerator.MoveNext())
                {
                    return true;
                }
                else
                    return false;
            }
        }
    }

    public class WaitForSecond:Coroutine{
        int count = 0;
        public WaitForSecond(int _count){
            this.count = _count;
            this.enumerator = Count();
        }
        IEnumerator Count(){
            while(--count>0){
                yield return true;
            }
        }

    }
    public class CoroutineManager{
        internal List m_Corourtines = new List();
        public Coroutine StartCoroutine(IEnumerator routine){
            var coroutine = new Coroutine(routine);
            m_Corourtines.Add(coroutine);
            return coroutine;
        }

    }
}
 

你可能感兴趣的:(unity)