热更新_ToLua学习示例 06_LuaCoroutine2

该文展示了如何在Unity3D中使用ToLua库自定义封装Lua协程,包括WaitForSeconds、WaitForFixedUpdate、WaitForEndOfFrame等控制流程,以及对WWW请求的处理。相比于原生协程,这种做法提供了更接近C#的使用体验,但可能带来额外的性能开销。
摘要由CSDN通过智能技术生成
function CoExample()            
            WaitForSeconds(1)--作者封装的协程等待一秒
            print('WaitForSeconds end time: '.. UnityEngine.Time.time)            
            WaitForFixedUpdate()--等待固定时间更新
            print('WaitForFixedUpdate end frameCount: '..UnityEngine.Time.frameCount)
            WaitForEndOfFrame()--等待一帧
            print('WaitForEndOfFrame end frameCount: '..UnityEngine.Time.frameCount)
            Yield(null)
            print('yield null end frameCount: '..UnityEngine.Time.frameCount)
            Yield(0)
            print('yield(0) end frameCime: '..UnityEngine.Time.frameCount)
            local www = UnityEngine.WWW('http://www.baidu.com')
            Yield(www)
            print('yield(www) end time: '.. UnityEngine.Time.time)
            local s = tolua.tolstring(www.bytes)
            print(s:sub(1, 128))
            print('coroutine over')
        end

        function TestCo()            
            StartCoroutine(CoExample)                                   
        end

        local coDelay = nil

        function Delay()
	        local c = 1

	        while true do
		        WaitForSeconds(1) 
		        print('Count: '..c)
		        c = c + 1
	        end
        end

        function StartDelay()
	        coDelay = StartCoroutine(Delay)            
        end

        function StopDelay()
	        StopCoroutine(coDelay)
            coDelay = nil
        end

这个是ToLua作者全部自己封装的Lua协程,相比上一种,这种方法使用了大量自己封装的类,使得lua协程实现的逻辑跟C#几乎一样,结构相比第一种更加清晰,但是多了类的转化,性能消耗比上一种更大,更推荐使用上一种协程的实现方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值