加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 22:07:43 所属栏目:大数据 来源:网络整理
导读:习惯了unity3d的协程用法,那么我们在Unity里面怎么使用: StartCoroutine StopCoroutine WaitForSeconds 首先lua也有coroutine,其实lua中的协程真的是暂停,用法和unity并非相同的概念,我的母的实现就是为了在lua中使用上述和c#统一的协程接口类。 在C#端

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

StartCoroutine

StopCoroutine

WaitForSeconds


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

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


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

local gm = {}

function gm.startTimer(name,delayTime,func)
	local one = CreateCoroutine(function (this,name,func) 
	if not WaitForSeconds(this,name) then return end 
		if func~=nil then func() end 
	end) 
	StartCoroutine(one,func)
end

function gm.stopTimer(name)
	StopCoroutine(name)
end

return gm

具体使用例子:

GM.startTimer('Chiuan',1.0,function ( )
	Debug.Log('hello chiuan.')
end)

--GM.stopTimer('Chiuan')

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读