[Unity3D]Lua中使用协程coroutine和计时器timer

来源:互联网 发布:淘宝超过时间不能评价 编辑:程序博客网 时间:2024/04/30 10:27

习惯了unity3d的协程用法,那么我们在Unity里面怎么使用:

StartCoroutine

StopCoroutine

WaitForSeconds


首先lua也有coroutine,其实lua中的协程真的是暂停,用法和unity并非相同的概念,我的母的实现就是为了在lua中使用上述和c#统一的协程接口类。

在C#端需要写一个接口缓存lua的function并且对其进行计时回调lua的coroutine继续执行或者让这个协程‘dead’掉。


组合成2个计时器执行某个方法:

[plain] view plaincopyprint?
  1. local gm = {}  
  2.   
  3. function gm.startTimer(name,delayTime,func)  
  4.     local one = CreateCoroutine(function (this,name,func)   
  5.     if not WaitForSeconds(this,delayTime,name) then return end   
  6.         if func~=nil then func() end   
  7.     end)   
  8.     StartCoroutine(one,name,func)  
  9. end  
  10.   
  11. function gm.stopTimer(name)  
  12.     StopCoroutine(name)  
  13. end  
  14.   
  15. return gm  

具体使用例子:

[plain] view plaincopyprint?
  1. GM.startTimer('Chiuan',1.0,function ( )  
  2.     Debug.Log('hello chiuan.')  
  3. end)  
  4.   
  5. --GM.stopTimer('Chiuan')  

最后,具体怎么实现还是直接贴上源码 + demo吧。

http://game.ceeger.com/forum/read.php?tid=18475&fid=16

0 0