unity之协程要点
使用协程做计时功能应注意
1.协程中用到的组件,变量等被置空前,应该将协程置空
2.置空协程之前应停止协程
3.为了确保同一个协程同时只运行一次,可在协程开始前添加安全代码:判断改协程是否存在,存在则停止协程并将协程置空
实现方法:
local function setMyTime()
--注意(3)
if this.countdown then
coroutine.stop(this.countdown)
this.countdown = nil
end
this.countdown = coroutine.start(function()
while true do
this.tm=this.tm-1--用到的变量
coroutine.wait(1)
end
end)
end
注意(2)
if this.countdown then
coroutine.stop(this.countdown)
--注意(1)
this.countdown = nil
end
--假设此时需要对this.tm置空
this.tm=nil
标题:unity之协程要点
作者:jerrycookie
地址:https://mmzsblog.cn/articles/2020/06/21/1592708017535.html
-----------------------------
如未加特殊说明,此网站文章均为原创。
网站转载须在文章起始位置标注作者及原文连接,否则保留追究法律责任的权利。
公众号转载请联系网站首页的微信号申请白名单!
