zoukankan      html  css  js  c++  java
  • Unity3d之协程自实现测试

    using UnityEngine;
    using System.Collections;
    
    public class TestStartCoroutine : MonoBehaviour
    {
        IEnumerator m_etor;
        bool m_moveNext;
        MyWaitForSeconds m_waiter;
    
        #region MyRegion
    
        class MyWaitForSeconds
        {
            int m_seconds;
            float m_timer;
    
            public MyWaitForSeconds(int seconds)
            {
                m_seconds = seconds;
                m_timer = 0;
            }
    
            public bool Update(float deltaTime)
            {
                m_timer += deltaTime;
                return m_timer >= m_seconds;
            }
        }
    
        #endregion
    
        void Start()
        {
            MyStartCoroutine(T1());
        }
    
        public void MyStartCoroutine(IEnumerator etor)
        {
            m_etor = etor;
            m_moveNext = true;
        }
    
        IEnumerator T1()
        {
            Debug.Log("#1");
    
            int i = 0;
            while (i++ < 60)
            {
                Debug.Log("#2  " + i);
                yield return null;
            }
    
            Debug.Log("#3");
    
            yield return new MyWaitForSeconds(3);
    
            Debug.Log("#4");
        }
    
        void Update()
        {
            if (m_moveNext)
            {
                if (!m_etor.MoveNext())
                    m_moveNext = false;
                else if (m_etor.Current is MyWaitForSeconds)
                {
                    m_waiter = (MyWaitForSeconds)m_etor.Current;
                    m_moveNext = false;
                }
            }
    
            if (m_waiter != null)
            {
                if (m_waiter.Update(Time.deltaTime))
                {
                    m_waiter = null;
                    m_moveNext = true;
                }
            }
        }
    }
    

      Unity3d的协程是利用枚举器来实现的,我猜可能是跟以上测试代码一样,在一帧里让枚举器的指针加1,从而可以等待1帧。

  • 相关阅读:
    linux系统root用户忘记密码的重置方法
    Linux系统的初始化配置
    LINUX awk 函数
    随机产生一个密码,要求同时包含大小写以及数字这三种字符。
    sed 函数 linux
    grep 函数
    linux sort 函数
    从零开始的JAVA -4. 运算符与表达式
    cp
    PATH
  • 原文地址:https://www.cnblogs.com/jietian331/p/4748627.html
Copyright © 2011-2022 走看看