2013年10月15日星期二

Unity3D中js c#语言的yield

      可以把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());
   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");
}

没有评论:

发表评论