原文:http://www.tuicool.com/articles/3IV32m

Unity协程(Coroutine)原理深入剖析

By D.S.Qiu

尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com

记得去年6月份刚开始实习的时候,当时要我写网络层的结构,用到了协程,当时有点懵,完全不知道Unity协程的执行机制是怎么样的,只是知道函数的返回值是IEnumerator类型,函数中使用yield return ,就可以通过StartCoroutine调用了。后来也是一直稀里糊涂地用,上网google些基本都是例子,很少能帮助深入理解Unity协程的原理的。

本文只是从Unity的角度去分析理解协程的内部运行原理,而不是从C#底层的语法实现来介绍(后续有需要再进行介绍),一共分为三部分:

线程(Thread)和协程(Coroutine)

Unity中协程的执行原理

                IEnumerator & Coroutine

之前写过一篇《 Unity协程(Coroutine)管理类——TaskManager工具分享 》主要是介绍TaskManager实现对协程的状态控制,没有Unity后台实现的协程的原理进行深究。虽然之前自己对协程还算有点了解了,但是对Unity如何执行协程的还是一片空白,在UnityGems.com上看到两篇讲解Coroutine,如数家珍,当我看到Advanced Coroutine后面的Hijack类时,顿时觉得十分精巧,眼前一亮,遂动了写文分享之。

线程(Thread)和协程(Coroutine)

D.S.Qiu觉得使用协程的作用一共有两点:1)延时(等待)一段时间执行代码;2)等某个操作完成之后再执行后面的代码。总结起来就是一句话:控制代码在特定的时机执行。

很多初学者,都会下意识地觉得协程是异步执行的,都会觉得协程是C# 线程的替代品,是Unity不使用线程的解决方案。

所以首先,请你牢记:协程不是线程,也不是异步执行的。协程和 MonoBehaviour 的 Update函数一样也是在MainThread中执行的。使用协程你不用考虑同步和锁的问题。

Unity中协程的执行原理

UnityGems.com给出了协程的定义:

A coroutine is a function that is executed partially and, presuming suitable conditions are met, will be resumed at some point in the future until its work is done.

即协程是一个分部执行,遇到条件(yield return 语句)会挂起,直到条件满足才会被唤醒继续执行后面的代码。

Unity在每一帧(Frame)都会去处理对象上的协程。Unity主要是在Update后去处理协程(检查协程的条件是否满足),但也有写特例:

从上图的剖析就明白,协程跟Update()其实一样的,都是Unity每帧对会去处理的函数(如果有的话)。如果MonoBehaviour 是处于激活(active)状态的而且yield的条件满足,就会协程方法的后面代码。还可以发现:如果在一个对象的前期调用协程,协程会立即运行到第一个 yield return 语句处,如果是 yield return null ,就会在同一帧再次被唤醒。如果没有考虑这个细节就会出现一些奇怪的问题『1』。

『1』注 图和结论都是从UnityGems.com 上得来的,经过下面的验证发现与实际不符,D.S.Qiu用的是Unity 4.3.4f1 进行测试的。 经过测试验证,协程至少是每帧的LateUpdate()后去运行。

下面使用 yield return new WaitForSeconds(1f); 在Start,Update 和 LateUpdate 中分别进行测试:

using UnityEngine;
using System.Collections;public class TestCoroutine : MonoBehaviour {  private bool isStartCall = false;  //Makesure Update() and LateUpdate() Log only once  private bool isUpdateCall = false;  private bool isLateUpdateCall = false;  // Use this for initialization  void Start () {   if (!isStartCall)   {    Debug.Log("Start Call Begin");    StartCoroutine(StartCoutine());    Debug.Log("Start Call End");    isStartCall = true;   }   }  IEnumerator StartCoutine()  {     Debug.Log("This is Start Coroutine Call Before");   yield return new WaitForSeconds(1f);   Debug.Log("This is Start Coroutine Call After");    }  // Update is called once per frame  void Update () {   if (!isUpdateCall)   {    Debug.Log("Update Call Begin");    StartCoroutine(UpdateCoutine());    Debug.Log("Update Call End");    isUpdateCall = true;   }  }  IEnumerator UpdateCoutine()  {   Debug.Log("This is Update Coroutine Call Before");   yield return new WaitForSeconds(1f);   Debug.Log("This is Update Coroutine Call After");  }  void LateUpdate()  {   if (!isLateUpdateCall)   {    Debug.Log("LateUpdate Call Begin");    StartCoroutine(LateCoutine());    Debug.Log("LateUpdate Call End");    isLateUpdateCall = true;   }  }  IEnumerator LateCoutine()  {   Debug.Log("This is Late Coroutine Call Before");   yield return new WaitForSeconds(1f);   Debug.Log("This is Late Coroutine Call After");  } }

得到日志输入结果如下:

然后将yield return new WaitForSeconds(1f);改为 yield return null; 发现日志输入结果和上面是一样的,没有出现上面说的情况:

using UnityEngine;
using System.Collections;public class TestCoroutine : MonoBehaviour {  private bool isStartCall = false;  //Makesure Update() and LateUpdate() Log only once  private bool isUpdateCall = false;  private bool isLateUpdateCall = false;  // Use this for initialization  void Start () {   if (!isStartCall)   {    Debug.Log("Start Call Begin");    StartCoroutine(StartCoutine());    Debug.Log("Start Call End");    isStartCall = true;   }   }  IEnumerator StartCoutine()  {     Debug.Log("This is Start Coroutine Call Before");   yield return null;   Debug.Log("This is Start Coroutine Call After");    }  // Update is called once per frame  void Update () {   if (!isUpdateCall)   {    Debug.Log("Update Call Begin");    StartCoroutine(UpdateCoutine());    Debug.Log("Update Call End");    isUpdateCall = true;   }  }  IEnumerator UpdateCoutine()  {   Debug.Log("This is Update Coroutine Call Before");   yield return null;   Debug.Log("This is Update Coroutine Call After");  }  void LateUpdate()  {   if (!isLateUpdateCall)   {    Debug.Log("LateUpdate Call Begin");    StartCoroutine(LateCoutine());    Debug.Log("LateUpdate Call End");    isLateUpdateCall = true;   }  }  IEnumerator LateCoutine()  {   Debug.Log("This is Late Coroutine Call Before");   yield return null;   Debug.Log("This is Late Coroutine Call After");  } }

前面在介绍TaskManager工具时,说到MonoBehaviour 没有针对特定的协程提供Stop方法,其实不然,可以通过MonoBehaviour enabled = false 或者 gameObject.active = false 就可以停止协程的执行『2』。

经过验证,『2』的结论也是错误的,正确的结论是,MonoBehaviour.enabled = false 协程会照常运行,但 gameObject.SetActive(false) 后协程却全部停止,即使在Inspector把  gameObject 激活还是没有继续执行:

using UnityEngine;
using System.Collections;public class TestCoroutine : MonoBehaviour {  private bool isStartCall = false;  //Makesure Update() and LateUpdate() Log only once  private bool isUpdateCall = false;  private bool isLateUpdateCall = false;  // Use this for initialization  void Start () {   if (!isStartCall)   {    Debug.Log("Start Call Begin");    StartCoroutine(StartCoutine());    Debug.Log("Start Call End");    isStartCall = true;   }   }  IEnumerator StartCoutine()  {     Debug.Log("This is Start Coroutine Call Before");   yield return new WaitForSeconds(1f);   Debug.Log("This is Start Coroutine Call After");    }  // Update is called once per frame  void Update () {   if (!isUpdateCall)   {    Debug.Log("Update Call Begin");    StartCoroutine(UpdateCoutine());    Debug.Log("Update Call End");    isUpdateCall = true;    this.enabled = false;    //this.gameObject.SetActive(false);   }  }  IEnumerator UpdateCoutine()  {   Debug.Log("This is Update Coroutine Call Before");   yield return new WaitForSeconds(1f);   Debug.Log("This is Update Coroutine Call After");   yield return new WaitForSeconds(1f);   Debug.Log("This is Update Coroutine Call Second");  }  void LateUpdate()  {   if (!isLateUpdateCall)   {    Debug.Log("LateUpdate Call Begin");    StartCoroutine(LateCoutine());    Debug.Log("LateUpdate Call End");    isLateUpdateCall = true;   }  }  IEnumerator LateCoutine()  {   Debug.Log("This is Late Coroutine Call Before");   yield return null;   Debug.Log("This is Late Coroutine Call After");  } }

先在Update中调用 this.enabled = false; 得到的结果:

然后把 this.enabled = false; 注释掉,换成 this.gameObject.SetActive(false); 得到的结果如下:

        整理得到 :通过设置MonoBehaviour脚本的enabled对协程是没有影响的,但如果 gameObject.SetActive(false) 则已经启动的协程则完全停止了,即使在Inspector把gameObject 激活还是没有继续执行。也就说协程虽然是在MonoBehvaviour启动的(StartCoroutine)但是协程函数的地位完全是跟MonoBehaviour是一个层次的,不受MonoBehaviour的状态影响,但跟MonoBehaviour脚本一样受gameObject 控制,也应该是和MonoBehaviour脚本一样每帧“轮询” yield 的条件是否满足。

yield 后面可以有的表达式:

a) null - the coroutine executes the next time that it is eligible

b) WaitForEndOfFrame - the coroutine executes on the frame, after all of the rendering and GUI is complete

c) WaitForFixedUpdate - causes this coroutine to execute at the next physics step, after all physics is calculated

d) WaitForSeconds - causes the coroutine not to execute for a given game time period

e) WWW - waits for a web request to complete (resumes as if WaitForSeconds or null)

f) Another coroutine - in which case the new coroutine will run to completion before the yielder is resumed

值得注意的是 WaitForSeconds()受Time.timeScale影响,当Time.timeScale = 0f 时,yield return new WaitForSecond(x) 将不会满足。

IEnumerator & Coroutine

协程其实就是一个IEnumerator(迭代器),IEnumerator 接口有两个方法 Current 和 MoveNext() ,前面介绍的TaskManager就是利用者两个方法对协程进行了管理,这里在介绍一个协程的交叉调用类 Hijack(参见附件):

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections;[RequireComponent(typeof(GUIText))] public class Hijack : MonoBehaviour {  //This will hold the counting up coroutine  IEnumerator _countUp;  //This will hold the counting down coroutine  IEnumerator _countDown;  //This is the coroutine we are currently  //hijacking  IEnumerator _current;  //A value that will be updated by the coroutine  //that is currently running  int value = 0;  void Start()  {   //Create our count up coroutine   _countUp = CountUp();   //Create our count down coroutine   _countDown = CountDown();   //Start our own coroutine for the hijack   StartCoroutine(DoHijack());  }  void Update()  {   //Show the current value on the screen   guiText.text = value.ToString();  }  void OnGUI()  {   //Switch between the different functions   if(GUILayout.Button("Switch functions"))   {    if(_current == _countUp)     _current = _countDown;    else     _current = _countUp;   }  }  IEnumerator DoHijack()  {   while(true)   {    //Check if we have a current coroutine and MoveNext on it if we do    if(_current != null && _current.MoveNext())    {     //Return whatever the coroutine yielded, so we will yield the     //same thing     yield return _current.Current;    }    else     //Otherwise wait for the next frame     yield return null;   }  }  IEnumerator CountUp()  {   //We have a local increment so the routines   //get independently faster depending on how   //long they have been active   float increment = 0;   while(true)   {    //Exit if the Q button is pressed    if(Input.GetKey(KeyCode.Q))     break;    increment+=Time.deltaTime;    value += Mathf.RoundToInt(increment);    yield return null;   }  }  IEnumerator CountDown()  {   float increment = 0f;   while(true)   {    if(Input.GetKey(KeyCode.Q))     break;    increment+=Time.deltaTime;    value -= Mathf.RoundToInt(increment);    //This coroutine returns a yield instruction    yield return new WaitForSeconds(0.1f);   }  } }

上面的代码实现是两个协程交替调用,对有这种需求来说实在太精妙了。

小结:

今天仔细看了下UnityGems.com 有关Coroutine的两篇文章,虽然第一篇(参考①)现在验证的结果有很多错误,但对于理解协程还是不错的,尤其是当我发现Hijack这个脚本时,就迫不及待分享给大家。

本来没觉得会有UnityGems.com上的文章会有错误的,无意测试了发现还是有很大的出入,当然这也不是说原来作者没有经过验证就妄加揣测,D.S.Qiu觉得很有可能是Unity内部的实现机制改变了,这种东西完全可以改动,Unity虽然开发了很多年了,但是其实在实际开发中还是有很多坑,越发觉得Unity的无力,虽说容易上手,但是填坑的功夫也是必不可少的。

看来很多结论还是要通过自己的验证才行,贸然复制粘贴很难出真知,切记!

如果您对D.S.Qiu有任何建议或意见可以在文章后面评论,或者发邮件(gd.s.qiu@gmail.com)交流,您的鼓励和支持是我前进的动力,希望能有更多更好的分享。

转载请在文首注明出处: http://dsqiu.iteye.com/blog/2028503

更多精彩请关注D.S.Qiu的博客和微博(ID:静水逐风)

参考:

①UnityGems.com:  http://unitygems.com/coroutines/

②UnityGems.com:  http://unitygems.com/advanced-coroutines/

转载于:https://www.cnblogs.com/smallrock/articles/4000381.html

unity3D协程(Coroutine)原理深入剖析相关推荐

  1. Tornado 异步协程coroutine原理

    协程定义: 协程,又称微线程,纤程.英文名Coroutine. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在执行过程中又调用了C,C执行完毕返回,B执行完毕返回,最后是A执行完毕 ...

  2. Unity 协程(Coroutine)原理与用法详解

    前言: 协程在Unity中是一个很重要的概念,我们知道,在使用Unity进行游戏开发时,一般(注意是一般)不考虑多线程,那么如何处理一些在主任务之外的需求呢,Unity给我们提供了协程这种方式 为啥在 ...

  3. tornado协程(coroutine)原理

    tornado中的协程是如何工作的 本文将按以下结构进行组织,说明tornado中协程的执行原理 协程定义 生成器和yield语义 Future对象 ioloop对象 函数装饰器coroutine 总 ...

  4. 【Unity】Unity协程(Coroutine)的原理与应用

    文章目录 前言 一.什么是协程 二.应用场景 1.异步加载资源 2.将一个复杂程序分帧执行 3.定时器 三.协程的使用 注意事项 四.Unity协程的底层原理 1. 协程本体:C#的迭代器函数 2. ...

  5. 协程的原理及协程在高并发服务中的应用

    协程的原理 协程(coroutine)跟具有操作系统概念的线程不一样,实际上协程就是类函数一样的程序组件,你可以在一个线程里面轻松创建数十万个协程,就像数十万次函数调用一样.只不过函数只有一个调用入口 ...

  6. plsql develop怎么停止job_Kotlin协程实现原理:CoroutineScopeamp;Job

    今天我们来聊聊Kotlin的协程Coroutine. 如果你还没有接触过协程,推荐你先阅读这篇入门级文章What? 你还不知道Kotlin Coroutine? 如果你已经接触过协程,但对协程的原理存 ...

  7. pdf 深入理解kotlin协程_Kotlin协程实现原理:挂起与恢复

    今天我们来聊聊Kotlin的协程Coroutine. 如果你还没有接触过协程,推荐你先阅读这篇入门级文章What? 你还不知道Kotlin Coroutine? 如果你已经接触过协程,但对协程的原理存 ...

  8. c++ 协程_Python3 协程(coroutine)介绍

    本文首发于 at7h 的个人博客. 目前 Python 语言的协程从实现来说可分为两类: 一种是基于传统生成器的协程,叫做 generator-based coroutines,通过包装 genera ...

  9. Kotlin协程实现原理

    前言 本篇解析Kotlin/JVM中的协程的实现原理. 初看suspend关键字 下面的例子模拟一个网络请求: class Temp {suspend fun fetchData(argument: ...

最新文章

  1. ACM-ICPC (10/19)
  2. linux查看系统内存和cpu使用率,查看Linux系统内存、CPU、磁盘使用率
  3. [bzoj2729][HNOI2012]排队 题解 (排列组合 高精)
  4. 蛇形数组打印(两种形式)
  5. 花30万买鸿蒙汽车,值吗?
  6. C++_012C++11的语法新特性
  7. LeetCode学习记录(4-6)
  8. springsecurity3的验证过程
  9. AAC音频的解码算法
  10. 中兴ZXDSL831驱动
  11. 简图记录-曾国藩家训 观后感
  12. 上月用得好好的支付宝获取月账单的Java接口,月初突然返回“入参不合法”的解决方法
  13. 20个高权重的博客列表
  14. 机器学习之数据集划分-k折交叉验证法(k-fold cross validation)
  15. flutter开发的ios应用没法通过爱思等工具浏览文件目录Document
  16. 如何实现网页分享到微信,微博,空间
  17. Android之NFC
  18. 云原生周报 | 2021下半年CNCF开源项目发展总结;Cilium 1.11发布;BFE Server及控制面更新
  19. java import javax.mail.*报错原因
  20. android 百度输入法,Android系统预置百度输入法

热门文章

  1. 函数调用关系python_追踪python函数调用关系
  2. 知识蒸馏,中文文本分类,教师模型BERT,学生模型biLSTM
  3. 基于R-Net、QA-Net和BiDAF实现中文观点型问题机器阅读理解
  4. ajax 请求struts1,jquery ajax +struts1.3
  5. 访问不了_etherscan访问不了的替代方案
  6. 每天执行一次批处理_关于静态批处理/动态批处理/GPU Instancing /SRP Batcher的详细剖析...
  7. ORA-15260 diskgroup space exhausted Problem
  8. php携程 线程,244,android线程与协程以及携程的使用方法
  9. vue中通过ref属性来获取dom的引用
  10. 洛谷P1130 红牌 动态规划