Unity 协程的C# 实现

直接上代码:

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

namespace Server {
    /// 
    /// 协程类
    /// 
    public sealed class Coroutine {
        private Coroutine() { }

        /// 
        /// 协程实例
        /// 
        private static Coroutine instance;

        static Coroutine() {
            instance = new Coroutine();
        }

        /// 
        /// 所有的迭代器
        /// 
        private List itors = new List();

        /// 
        /// 启动协程
        /// 
        /// 
        public static void StartCoroutine(IEnumerator iCoroutine) {
            instance.itors.Add(new IEnumeratorContainer(iCoroutine));
        }

        /// 
        /// 更新___主线程逻辑循环调用
        /// 
        public static void Update() {
            
            //循环处理
            int len = instance.itors.Count;
            for (int i=len-1;i>=0;i--) {
                if (instance.itors[i].Frame()) {
                    instance.itors.RemoveAt(i);
                }
            }
        }

        /// 
        /// 迭代器容器
        /// 
        private class IEnumeratorContainer {
            //按帧延时, 按时间延时

            /// 
            /// 迭代器_自身
            /// 
            private IEnumerator itor;

            /// 
            /// 是否等待
            /// 
            private bool isWait {
                get {
                    return waitMillisecond > 0 || waitFrame >= 0;
                }
            }

            /// 
            /// 子迭代器
            /// 
            private IEnumeratorContainer childContainer;

            /// 
            /// 等待的秒数
            /// 
            private int waitMillisecond = -1;

            /// 
            /// 等待的帧
            /// 
            private int waitFrame = -1;

            /// 
            /// 开始等待的时间
            /// 
            private DateTime waitStartTime;

            /// 
            /// 创建迭代
            /// 
            /// 
            public IEnumeratorContainer(IEnumerator itor) {
                this.itor = itor;
            }

            /// 
            /// 一帧
            /// 
            /// 表示否最后一帧
            public bool Frame() {
                if (isWait) {   //处理等待
                    if (waitFrame >= 0) {
                        waitFrame--;
                    }
                    if (waitMillisecond > 0) {
                        TimeSpan ts = DateTime.Now - waitStartTime;
                        if (ts.TotalMilliseconds > waitMillisecond) {
                            waitMillisecond = -1;
                        }
                    }
                }
                if (isWait) //还需要等待
                    return false;


                bool isHave = true;
                if (childContainer != null) {//子迭代不为空,出来子迭代
                    if (childContainer.Frame()) {//子迭代处理完了
                        childContainer = null;
                        return Frame(); //处理自己
                    }
                } else {
                    isHave = itor.MoveNext();
                    if (isHave) { //做处理
                        if (itor.Current is IEnumerator) {//当前是个迭代
                            IEnumerator chitor = itor.Current as IEnumerator;
                            childContainer = new IEnumeratorContainer(chitor);
                        } else if (itor.Current is WaitCoroutine) { //当前是等待处理
                            WaitCoroutine wc = itor.Current as WaitCoroutine;
                            AddAttributes(wc);
                        }
                    }
                }
                return !isHave;
            }

            /// 
            /// 添加等待属性
            /// 
            /// 
            private void AddAttributes(WaitCoroutine wc) {
                switch (wc.style) {
                    case 0:      //秒钟
                        waitMillisecond = wc.value * 1000;
                        waitStartTime = DateTime.Now;
                        break;
                    case 1:     //毫秒
                        waitMillisecond = wc.value;
                        waitStartTime = DateTime.Now;
                        break;
                    case 2:
                        waitFrame = wc.value;
                        break;
                }
            }

            /// 
            /// 添加儿子
            /// 
            private void AddChild(IEnumerator _itor) {
                childContainer = new IEnumeratorContainer(_itor);
            }

        }
    }

    /// 
    /// 等待基类
    /// 
    public abstract class WaitCoroutine {
        /// 
        /// 类型 0等待 秒种  1等待毫秒值   2等待帧数
        /// 
        public int style;

        /// 
        /// 数值
        /// 
        public int value;
    }

    /// 
    /// 等待秒
    /// 
    public class WaitSecond : WaitCoroutine {
        /// 
        /// 秒钟
        /// 
        /// 
        public WaitSecond(int second) {
            style = 0;
            value = second;
        }
    }

    /// 
    /// 等待毫秒值 
    /// 
    public class WaitMillisecond : WaitCoroutine {

        /// 
        /// 
        /// 
        /// 
        public WaitMillisecond(int millisecond) {
            style = 1;
            value = millisecond;
        }
    }

    /// 
    /// 等待帧数
    /// 
    public class WaitFrame : WaitCoroutine {
        /// 
        /// 
        /// 
        /// 
        public WaitFrame(int frame) {
            style = 2;
            value = frame;
        }
    }


}

 

你可能感兴趣的:(unity笔记,c#)