Unity3D中的Coroutine及其使用(延时、定时调用函数)
http://blog.csdn.net/nizihabi/article/details/47606887
一、Coroutine(协程)的概念和本质
在网上的一些资料当中,一直将Coroutine当作一个线程来描述,这样是不准确的。因为Coroutine并不是一个新的线程,它仍旧是属于主线程的一部分。Coroutine本质上是一种轻量级的thread,它的开销会比使用thread少很多。多个Coroutine可以按照次序在一个thread里面执行,一个Coroutine如果处于block状态,可以交出执行权,让其他的Coroutine继续执行。
而在Unity当中,Coroutine是在所有的Update函数执行完成之后才开始执行的,也就是在LateUpdate()之后执行,主要是用来协助主线程进行工作,就像手动添加了一个另外的update()函数在里面一样,一般情况下我们用于延时触发的功能。下面是关于MonoBehavior的执行顺序图,可以看到Coroutine是在LateUpdate之后的。
而我们注意到上图中,Coroutine左边标注着几个yield模块,这个就是我们下面要讲的如何使用Coroutine里的内容了。
二、Coroutine的使用
- using UnityEngine;
- using System.Collections;
- public class ExampleClass : MonoBehaviour
- {
- IEnumerator WaitAndPrint()
- {
- // suspend execution for 5 seconds
- yield return new WaitForSeconds(5);
- print("WaitAndPrint " + Time.time);
- }
- IEnumerator Start()
- {
- print("Starting " + Time.time);
- // Start function WaitAndPrint as a coroutine
- yield return StartCoroutine("WaitAndPrint");
- print("Done " + Time.time);
- }
- }
大家应该能得到下面的输出(本人的Unity版本是5.0.1f1)
- using UnityEngine;
- using System.Collections;
- public class ExampleClass : MonoBehaviour
- {
- IEnumerator WaitAndPrint()
- {
- // suspend execution for 5 seconds
- yield return new WaitForSeconds(5);
- print("WaitAndPrint " + Time.time);
- }
- void Start()
- {
- print("Starting " + Time.time);
- // Start function WaitAndPrint as a coroutine
- StartCoroutine("WaitAndPrint");
- print("Done " + Time.time);
- }
- }
得到的输出如下:
在Unity3D中,使用StopCoroutine(string methodName)来终止一个协同程序,使用StopAllCoroutines()来终止所有可以终止的协同程序,但这两个方法都只能终止该MonoBehaviour中的协同程序
还有一种终止Coroutine的方法,将gameObject的active属性设为False,但是设回true的时候并不会启动Coroutine。
注意:将Coroutine所在的脚本设为enable = false并不能够将Coroutine停止,因为它跟Monobehavior是同层级的。
Unity3D中的Coroutine及其使用(延时、定时调用函数)的更多相关文章
- Unity3D中的Coroutine详解
Unity中的coroutine是通过yield expression;来实现的.官方脚本中到处会看到这样的代码. 疑问: yield是什么? Coroutine是什么? unity的coroutin ...
- Unity3D中的Coroutine具体解释
本文太乱,推荐frankjfwang的:全面解析Coroutine技术 Unity中的coroutine是通过yield expression;来实现的.官方脚本中到处会看到这种代码. 疑问: yie ...
- 【Unity3D/C#】Unity3D中的Coroutine详解
Unity中的coroutine是通过yield expression;来实现的.官方脚本中到处会看到这样的代码. 疑问: yield是什么? Coroutine是什么? unity的coroutin ...
- Javascript中的定时调用函数setInterval()和setTimeout()
首先介绍这两个函数 一.setInterval() 按照指定的周期来调用函数或表达式,执行多次.(时间单位:ms) timer = setInterval("content =documen ...
- JavaScript定时调用函数(SetInterval与setTimeout)
setTimeout和setInterval的语法同样.它们都有两个參数.一个是将要运行的代码字符串.另一个是以毫秒为单位的时间间隔,当过了那个时间段之后就将运行那段代码. 只是这两个函数还是有差别的 ...
- 怎样理解在函数中声明var x = y = 1后调用函数时, x是局部变量, y是全局变量
下面这段代码在执行的时候, 打印的结果是1, Error: undefined; function fn() { var x = y = 1; } fn(); console.log(y); // 1 ...
- 【吐血推荐】简要分析unity3d中剪不断理还乱的yield
在学习unity3d的时候很容易看到下面这个例子: void Start () { StartCoroutine(Destroy()); } IEnumerator Destroy(){ yield ...
- 【Unity3D】简要分析unity3d中剪不断理还乱的yield
在学习unity3d的时候很容易看到下面这个例子: void Start () { StartCoroutine(Destroy()); } IEnumerator Destroy(){ yield ...
- 【转】简要分析unity3d中剪不断理还乱的yield
在学习unity3d的时候很容易看到下面这个例子: 1 void Start () { 2 StartCoroutine(Destroy()); 3 } 4 5 IEnumerator Destroy ...
随机推荐
- Manager模块 队列 管道 进程池
Manager模块 作用: 多进程共享变量. Manager的字典类型: 如果value是简单类型,比如int,可以直接赋值给共享变量,并可以后续直接修改 如果value是复杂类型 ,比如list, ...
- pip源提示“not a trusted or secure host” 解决
问题:The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored ...
- c/c++的一些小知识点3
---恢复内容开始--- ---恢复内容结束---
- Linux下安装 activemq 并指定jdk 1.8
1. 下载安装包 <apache-activemq-5.15.4-bin.tar.gz> 下载地址:https://pan.baidu.com/s/18xzjBAchjWqsHNA1HuY ...
- ASP.NET中指定自定义HTTP响应标头
新建一个类HideServerHeaderHelper,继承 IHttpModule,然后重写 OnPreSendRequestHeaders,Dispose,Init方法,如下代码所示 using ...
- hive深入使用
Hive表的创建和数据类型 https://cwiki.apache.org/confluence/display/Hive/Home 管理表和外部的区别 # 管理表 1. 内部表也称之为MANAGE ...
- 解决iOS11 UIScrollView下移问题
iOS11 系统为UIScrollView增加一个contentInsetAdjustmentBehavior属性,默认为UIScrollViewContentInsetAdjustmentAutom ...
- DNN优势
- android中handler和bundle有什么区别和联系 都是用来传递消息吗都是信息的载体吗
1.handler是消息处理者,通常重写Handler的handleMessage()方法,在方法中处理接收到的不同消息,例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Ha ...
- 类加载(一):static块 和 Class.forName
1. class Some { static{ System.out.println("1"); } public Some(){ System.out.println(" ...