辣条 发表于 2012-8-12 21:45

Unity3D yield自己的总结

以前没有接触过yield,光看reference还真有点迷糊,做了一些测试,算是掌握了。
说一下我的理解,如有不对,望大家指正。
可以把yield理解成一种特殊形式的return,它和return一样,会立即把执行权返回父级函数。特别之处在于,yield后面跟的函数或对象会跟一个条件判断,当条件满足时,就会再次回调包含该yield的子函数,并且从yield语句之后继续执行。条件满足之前,执行父函数下面的语句,可以看作异步执行。

例如:

js:
callYieldFunction();
Debug.Log("print second");

function callYieldFunction()
{
    Debug.Log("print first");
    yield new WaitForSeconds(2);
    Debug.Log("print after 2 seconds");
}


c#:

//在c#中必须显示的指明,启动一个纤程以调用含有yield的函数。
   StartCoroutine(callYieldFunction());
//在c#中必须显示的指明,启动一个纤程以调用含有yield的函数。
   StartCoroutine(callYieldFunction());
   Debug.Log("print second");

//在c#中含有yield的函数,返回值必须为IEnumerator
   IEnumerator callYieldFunction()   
   {
Debug.Log("print first");
yield return new WaitForSeconds(2);   
Debug.Log("print after 2 seconds");
   }
   Debug.Log("print second");

//在c#中含有yield的函数,返回值必须为IEnumerator
   IEnumerator callYieldFunction()   
   {
Debug.Log("print first");
yield return new WaitForSeconds(2);   
Debug.Log("print after 2 seconds");
   }


当没有父函数可以返回,本身已经是顶级函数的时候,yield的条件相当于同步执行,程序一直等到条件满足,才继续执行下面的语句。

例如:

js: function Start()
{
   Debug.Log("print first");
   yield new WaitForSeconds(2);
   Debug.Log("print after 2 seconds");
}

c#:IEnumerator Start()   //注意c#中的返回值
{
   Debug.Log("print first");
   yield return new WaitForSeconds(2);   
   Debug.Log("print after 2 seconds");
}


在理解了这个之后,就可以理解使用嵌套的yield,来实现同步的子纤程调用。
例如:因为start函数已经是顶级函数,所以外层的yield会”死在这里“,直到嵌套的纤程执行完毕,再继续执行。

js: function start()
{
   yield StartCoroutine("callYieldFunction");
   Debug.Log("print latest");
}

    function callYieldFunction()
{
    Debug.Log("print first");
    yield new WaitForSeconds(2);
    Debug.Log("print after 2 seconds");
}

c#:
IEnumerator Start()
{
   yield return StartCoroutine("callYieldFunction");   
   Debug.Log("print latest");
}

IEnumerator callYieldFunction()   
{
    Debug.Log("print first");
    yield return new WaitForSeconds(2);   
    Debug.Log("print after 2 seconds");
}

-来源自互联网

wai2dance 发表于 2012-11-26 22:05

沙发   学习了谢谢分享

syhnzz 发表于 2017-2-18 11:03

很不错

质. 发表于 2017-2-18 10:45

楼主是超人

李大本事 发表于 2017-2-18 11:12

好帖就是要顶

shangcunbao 发表于 2017-2-18 11:24

顶顶多好

huhumark 发表于 2017-2-18 10:55

难得一见的好帖

shuiyue654 发表于 2017-3-19 13:28

楼主是超人

silence394394 发表于 2017-3-19 13:39

真心顶

lychao0708 发表于 2017-3-19 14:01

说的非常好
页: [1]
查看完整版本: Unity3D yield自己的总结