• Unity3D 协程 浅谈


    协程

    一:什么是协同程序。

     协同程序,即主程序在运行的同时开启另外一段处理逻辑,类似于开启一个线程。

    注意:必须在MonoBehaviour

    两种开启方法:

     I:StartCoroutine(string methodName);

    最多只能传递一个参数,比较消耗性能。

    销毁:开启线程并在线程结束前终止线程(自己销毁本身)

     

    II:StartCoroutine(IEnumerator routinue);

    只能等待线程的终止而不能随时终止

    销毁:StopAllStoroutines(),  

    协同程序所在的gameObject 的 active属性设置为false,再次设置active为true,协同程序将不会再次开启。

    如果将协同程序所在的脚本的enable设置为false则不会消失。这是因为协同程序被开启后作为一个线程在运行,

    而MonoBehaviour也是一个线程,他们成为互不干扰的的模块。他们公用一个对象,只有在这个对象不可见才能同时终止这两个线程。然而为了

    管理我们额外开启的线程,Unity3D将协同程序的调用放在了MonoBehaviour中,这样在编程时就可以方便的调用指定的脚本中的协同程序,而不是

    无法去管理,特别是对于只根据方法名来判断线程的方式在多人开发中很容易出错,这样的设计是为了保证对象,脚本的条件管理,并防止重名。

     

    yield :代表先执行完本局语句(不管时间多长),或者执行完本yield方法调用,才执行后面的语句。

    看下面的例子,数字代表着执行的顺序

     private void Awake()
        {
            Debug.Log("11");
            StartCoroutine(DoTest());
            Debug.Log("333");
        }
        // Use this for initialization
        void Start()
        {
            Debug.Log("444");
        }
    
        IEnumerator DoTest()
        {
            Debug.Log("222");
            yield return new WaitForSeconds(1.0f);
            Debug.Log("555");
        }
    

      

    void Awake()
        {
            Debug.Log("00");
            StartCoroutine(StartTest());
            Debug.Log("2");
        }
        IEnumerator StartTest()
        {
            yield return StartCoroutine("Do");
            Debug.Log("4");
        }
    
        IEnumerator Do()
        {
            Debug.Log("1 Now");
            yield return null;
            Debug.Log("3");
        }

    理解:协程不是线程,也不是异步执行(知道就行)。

    1.协程和MonoBehaviour的Update函数一样,也是在MainThread中执行的(一定得明白这句话意思)。

    	void Start () {
            StartCoroutine(HelloCoroutine());
    	}
    	
    	void Update () {
            Debug.Log("Update...");
        }
        void LateUpdate()
        {
            Debug.Log("LateUpdate...");
        }
        IEnumerator HelloCoroutine()
        {
            while (true)
            {
                Debug.Log("Coroutine...");
                yield return null;
            }
        }


    
    
     
    对比以上代码和两张截图。这样写协程,好像是高级一点的Update写法。至少应该可以看出,这种写法的协程可以完成update的功能。

    2.与update不一样的地方。
     IEnumerator Count()
        {
            int seconds = 0;
            while (true)
            {
                for (float timer = 0; timer < 2; timer += Time.deltaTime)
                    yield return 0;
    
                seconds++;
                Debug.Log(seconds + " seconds have passed since the Coroutine started.");
            }
        }

      

    3.yield

      yiled return null  等同于 yield return 0

    我这边的理解是,停止正在执行的方法,并从下一帧开始执行(一般是0.02秒,与Update的每一帧是一样的,具体看Unity设置的timer)。

    4.协程是可以传递参数的。

    5.协程还可以嵌套协程。

    IEnumerator HelloCoroutinue()
        {
            Debug.Log("Start----");
            yield return StartCoroutine(Wait(0.2f)); //  yield return new WaitForSeconds(0.2f);最终达到的效果是一样的。
            Debug.Log("End----");
    }
        IEnumerator Wait(float s)
        {
            for(float timer =0;timer< s;timer+=Time.deltaTime)
            {
                Debug.Log("当前 timer" + timer.ToString());
                yield return 0; // yield return null;
            }
            Debug.Log("Wait.....");
        }
    

      

    看截图中画线的时间差,再次验证了与Update好像。暂停的时间都是一样的。

    可以看到暂停了当前的方法去执行yield return后的方法。

    补充注意: 

               a.多个协程可以同时运行,它们会根据各自的启动顺序来更新;

        b.如果你想让多个脚本访问一个协程,可以定义为静态的协程;

      (这篇博客是参考网上某篇博客,自己简化,加入了一点点自己的理解)

  • 相关阅读:
    Appium异常:An unknown serverside error occurred while processing the command. Original error: Could n
    华为手机usb调试已打开,adb devices不显示设备
    apache nginx 伪静态
    Laravel 下 Redis 基本操作
    GD添加文字+缩略图合成一张图
    RN 清理缓和和RN小助手工具安装
    uniapp 安卓截图native.js canvas 支持测试有点问题
    laravel redis key的prefix前缀
    PHP合并透明图背景为黑色时候的处理
    java 11下载
  • 原文地址:https://www.cnblogs.com/u3ddjw/p/6711642.html
Copyright © 2020-2023  润新知