There are 2 ways to use events in Lua:
1. The event system defined in C# is called at the Lua layer. I also wrote one https://blog.csdn.net/baidu_39447417/article/details/79764962
2. Define the event system in the Lua layer, the code is as follows
EventSystem={}
--添加事件 eventType-->事件类型 func-->方法
function EventSystem.AddListener(eventType,func)
if(eventType==nil or func==nil)then
log('在EventSystem.AddListener中eventType或func为空')
return
end
if(EventSystem[eventType]==nil)then
local a={}
table.insert(a,func)
EventSystem[eventType]=a
else
table.insert(EventSystem[eventType],func)
end
end
--移除事件
function EventSystem.RemoveListener(eventType,func)
if(eventType==nil or func==nil)then
log('在EventSystem.RemoveListener中eventType或func为空')
return
end
local a=EventSystem[eventType]
if(a~=nil)then
for k,v in pairs(a) do
if(v==func)then
a[k]=nil
end
end
end
end
--派发事件
function EventSystem.SendEvent(eventType,...)
if(eventType~=nil)then
local a=EventSystem[eventType]
if(a~=nil)then
for k,v in pairs(a) do
v(...)
end
end
end
end
return EventSystem
Be sure to note that if the event has a listener, there must be a removal listener