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#几乎一样,结构相比第一种更加清晰,但是多了类的转化,性能消耗比上一种更大,更推荐使用上一种协程的实现方式。