Unity热更学习--Lua脚本使用C#中的事件、委托和协程
[14]lua调用使用C#中的事件和委托
C#脚本:继续在Student类中声明
//声明委托和事件 public UnityAction dele; public event UnityAction eventAction; public void DoDele() { if (dele != null) dele(); } public void DoEvent() { if (eventAction != null) eventAction(); } public void ClearEvent() { eventAction = null; }
lua中进行调用:
--使用委托 function testFun() print("我在委托中执行 testFun") end --第一次委托添加函数时候要直接赋值 student.dele = testFun student.dele = student.dele + function() print("我是匿名函数哈哈哈") end --不可以直接执行委托 student。dele() student:DoDele() --取出函数 student.dele = student.dele - testFun print("减去TestFun函数") student:DoDele() --清空委托中的函数 student.dele = nil --使用事件 function testFun1() print("我在事件中执行 testFun1") end function testFun2() print("我在事件中执行 testFun2") end student.eventAction = student.eventAction + testFun2 student.eventAction = student.eventAction + testFun1 --触发事件 student:DoEvent() --从事件中移除函数 student.eventAction = student.eventAction - testFun2 print("再次触发事件") student:DoEvent() --不可以直接移除事件 print("事件清除") student:ClearEvent()
[15]lua调用使用Unity中的协程
----使用C#中的协程 --声明一个协程类型 local Timer = nil function Counter() local t = 0 while true do print(t) WaitForSeconds(1) t = t + 1 if t > 60 then StopTimer() break end end end --开启计时器 function StartTimer() Timer = StartCoroutine(Counter) end --停止协程 function StopTimer() StopCoroutine(Timer) Timer = nil end --调用函数开启计时器 StartTimer()
注意在lua调用入口进行LuaCoroutine注册
本文作者:畅知
本文链接:https://www.cnblogs.com/TonyCode/p/18212582
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
Unity学习笔记
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步