博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yield return的作用
阅读量:5215 次
发布时间:2019-06-14

本文共 2670 字,大约阅读时间需要 8 分钟。

测试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(2);        Debug.Log("callYieldFunction print after 2 seconds " + Time.time.ToString());    }    IEnumerator callYieldFunction1()            {        Debug.Log("callYieldFunction1 " + Time.time.ToString());        yield return new WaitForSeconds(2);        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(2);        Debug.Log("callYieldFunction print after 2 seconds " + Time.time.ToString());    }    IEnumerator callYieldFunction1()            {        Debug.Log("callYieldFunction1 " + Time.time.ToString());        yield return new WaitForSeconds(2);        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:整个闭包应该保存了堆栈信息

 

转载于:https://www.cnblogs.com/afan/p/3820837.html

你可能感兴趣的文章
C# winform DataGridView 常见属性
查看>>
逻辑运算和while循环.
查看>>
Nhiberate (一)
查看>>
c#后台计算2个日期之间的天数差
查看>>
安卓开发中遇到的小问题
查看>>
ARTS打卡第3周
查看>>
HDU 2189 悼念512汶川大地震遇难同胞――来生一起走 --生成函数
查看>>
js知识梳理3:创建对象的模式探究
查看>>
linux后台运行和关闭SSH运行,查看后台任务
查看>>
cookies相关概念
查看>>
android动态权限获取
查看>>
CAN总线波形中ACK位电平为什么会偏高?
查看>>
siebel 中 join 使用心得
查看>>
剑指Offer:重建二叉树
查看>>
MyBatis课程2
查看>>
css属性之统一设置文本及div之间的对齐方式
查看>>
PHP大批量更新数据,大批量插入数据,mysql批量更新与插入多种方法
查看>>
[转]如何循序渐进向dotnet架构师发展
查看>>
桥接模式-Bridge(Java实现)
查看>>
dpi 、 dip 、分辨率、屏幕尺寸、px、density 关系以及换算(终结版)
查看>>