yield js c#

4 篇文章 0 订阅
2 篇文章 0 订阅
可以把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");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值