Ejemplo n.º 1
0
        /// <summary>
        /// 线程调度,每一帧执行一次
        /// </summary>
        public static int Update()
        {
            for (int i = 0; i < queue.Count; i++)
            {
                YCContext c = queue[i];
                try
                {
                    if (!c.IsFinished())
                    {
                        c.Step();
                    }
                }
                catch (System.Exception e)
                {
                    // 有报错,需要进行收尾操作,并摘除掉
                    c.Error();
                    queue.RemoveAt(i);
                    i--;
                    UnityEngine.Debug.LogError(e);
                }

                if (c.IsFinished())
                {
                    // 调度完毕,摘除掉
                    queue.RemoveAt(i);
                    i--;
                }
            }
            return(1);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 扔到协程中执行
        /// 如果指明了owner,那么当owner=null时协程将自动被回收不予执行
        /// 如果指明了f,那么当有异常时会自动调用f来进行收尾工作,相对于try块的finally语句
        /// </summary>
        public static YCCoroutine DispatchService(IEnumerator c,
                                                  GameObject owner, CoroutineErrorCallback f)
        {
            YCContext ycc = new YCContext(c, owner);

            // 立刻先执行一把
            try
            {
                ycc.Step();
            }
            catch (System.Exception e)
            {
                // 有报错,需要进行收尾操作,并摘除掉
                ycc.Error();
                UnityEngine.Debug.LogError(e);
                return(null);
            }

            if (ycc.IsFinished())
            {
                // 立刻就执行完毕了,就不要加到队列中
                return(null);
            }

            // 加到队列中继续执行
            queue.Add(ycc);
            return(new YCCoroutine(ycc, owner, f));
        }
Ejemplo n.º 3
0
 public YCCoroutine(YCContext c, GameObject owner, CoroutineErrorCallback f)
 {
     this.context  = c;
     this.owner    = owner;
     this.hasOwner = (owner != null);
     this.f        = f;
 }