可以把yield理解成一种特殊形式的return,它和return一样,会立即把执行权返回父级函数。特别之处在于,yield后面跟的函数或对象会跟一个条件判断,当条件满足时,就会再次回调包含该yield的子函数,并且从yield语句之后继续执行。条件满足之前,执行父函数下面的语句,可以看作异步执行。

例如://在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的条件相当于同步执行,程序一直等到条件满足,才继续执行下面的语句。

c#: 
   普通浏览 
      复制代码 
IEnumerator Start 
         ( 
         )      
         //注意c#中的返回值 
{ 
   Debug.Log 
         ( 
         "print first" 
         ) 
         ; 
   yield  
         return  
         new WaitForSeconds 
         ( 
         2 
         ) 
         ; 
   Debug.Log 
         ( 
         "print after 2 seconds" 
         ) 
         ; 
}

在理解了这个之后,就可以理解使用嵌套的yield,来实现同步的子纤程调用。

例如:因为start函数已经是顶级函数,所以外层的yield会”死在这里“,直到嵌套的纤程执行完毕,再继续执行。

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" 
         ) 
         ; 
}