【功能】Lua层的全局事件管理系统

1.EventManager 全局的管理类

2.EventType 事件类型

3..Lua层Common工具目录去require对应文件目录的脚本文件

Lua 复制代码
--事件类型
local EventType = {
    TestMsg = 1,
}

return EventType
Lua 复制代码
local EventManager = class();
EventManager.msgMap = {}

local function HaveSameFunc(tagTable, func)
    for i=0, #tagTable do
        if tagTable[i] == func then
            return true
        end
    end
    return false
end

local function GetFuncIndex(table, func)
    if table == nil then
        return
    end
    for i=1, #table do
        if func == table[i] then
            return i
        end
    end
    return
end

--注册
function EventManager.RegisterMsg(msgid, func)
    if EventManager.msgMap[msgid] ~= nil then
        local callfunctable = EventManager.msgMap[msgid]
        if callfunctable == nil then
            callfunctable = {}
            table.insert(callfunctable, func)
            return
        end
        local findfunc = HaveSameFunc(callfunctable, func)
        if not findfunc then
            table.insert(callfunctable, func)
            return
        end
        return
    end
    EventManager.msgMap[msgid] = {}
    table.insert(EventManager.msgMap[msgid], func)
end

--反注册
function EventManager.UnRegisterMsg(msgid, func)
    if EventManager.msgMap[msgid] ~= nil then
        local callfunctable = EventManager.msgMap[msgid]
        if callfunctable == nil then
            return
        end

        local findfunc = HaveSameFunc(callfunctable, func)
        if findfunc then
           local delIndex = GetFuncIndex(callfunctable, func)
           if delIndex then
               table.remove(callfunctable, delIndex)
           end
        end
    end
end

--抛事件
function EventManager.DispatchMsg(msgid,...)
    if EventManager.msgMap[msgid]~= nil then
        local callfunctable = EventManager.msgMap[msgid]
        if callfunctable == nil then
            return
        end
        for i=1, #callfunctable do
            callfunctable[i](...)
        end
    end
end

return EventManager

业务层的使用

Lua 复制代码
local AccountPanelUI = BaseClass("AccountPanelUI",UIBaseView)
local base = UIBaseView

function AccountPanelUI.OnCreate(self)
    base.OnCreate(self)

    self:RegMsg()
end

functiion AccountPanelUI:RegMsg()
    self.funTable = {}

    self.funTable.OnClick= function ()
        print("test event.............")
    end

    EventManager.RegisterMsg(EventType.TestMsg, self.funTable.OnClick)
end

function AccountPanelUI:UnRegMsg()
    EventManager.UnRegisterMsg(EventType.TestMsg, self.funTable.OnClick)
    self.funTable = nil
end

return AccountPanelUI
相关推荐
千忧散11 分钟前
Unity Socket学习笔记 (三)TCP&UDP
笔记·学习·unity·c#
君莫愁。3 小时前
【Unity】构建超实用的有限状态机管理类
unity·c#·游戏引擎·有限状态机
UWA4 小时前
Unreal开发痛点破解!GOT Online新功能:Lua全监控 + LLM内存可视化!
开发语言·lua·unreal
1nullptr4 小时前
Lua迭代器与泛型for
lua
半夏知半秋5 小时前
skynet debug_console控制台中debug指令使用
服务器·开发语言·学习·lua
EQ-雪梨蛋花汤7 小时前
【Unity笔记】Unity Lighting Settings 全解析:一文读懂烘焙光照的每个参数(VR项目Lighting优化)
笔记·unity·vr
h7997108 小时前
redis lua脚本(go)调用教程以及debug调试
redis·golang·lua
BrightMZM1 天前
记录一下Unity的BUG,Trial Version
unity·bug·打包·trial
▍ 小太阳 ☼1 天前
Unity2022Navigation系统打开方式
unity·游戏引擎
qq_170264751 天前
unity升级对ab变更的影响
unity·游戏引擎