关于unity协程等待方法WaitForSecondsRealtime的发现

public class WaitForSecondsRealtime : CustomYieldInstruction
    {
     
        private float m_WaitUntilTime = -1f;

        /// 
        ///   The given amount of seconds that the yield instruction will wait for.
        /// 
        public float waitTime
        {
     
            get;
            set;
        }

        public override bool keepWaiting
        {
     
            get
            {
     
                bool flag = this.m_WaitUntilTime < 0f;
                if (flag)
                {
     
                    this.m_WaitUntilTime = Time.realtimeSinceStartup + this.waitTime;
                }
                bool flag2 = Time.realtimeSinceStartup < this.m_WaitUntilTime;
                bool flag3 = !flag2;
                if (flag3)
                {
     
                    this.m_WaitUntilTime = -1f;
                }
                return flag2;
            }
        }
        /// 
        ///   Creates a yield instruction to wait for a given number of seconds using unscaled time.
        /// 
        /// 
        public WaitForSecondsRealtime(float time)
        {
     
            this.waitTime = time;
        }
    }

从源码里可以看出,在等待时间到达的那一帧,flag2为fales,所以m_WaitUntilTime 被置为了-1,当同一帧再次获取keepWaiting值时,flag2就重新报变为true了。因为update在协程前调用,所以当update里每帧都获取keepWaiting时,协程就无法进行下去。
如果该协程对象已经结束,那keepWaiting会保持为fales的状态,直到被再次获取。所以协程结束后可以用keepWaiting去判断该协程是否还在运行,但是一旦调用后,keepWaiting将变为true

你可能感兴趣的:(知识点备忘,unity3d)