【功能】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
相关推荐
l1t4 小时前
用Lua访问DuckDB数据库
数据库·junit·lua·duckdb
星空露珠4 小时前
数独生成题目lua脚本
数据结构·数据库·算法·游戏·lua
爱吃小胖橘11 小时前
Unity资源加载模块全解析
开发语言·unity·c#·游戏引擎
l1t12 小时前
Lua与LuaJIT的安装与使用
算法·junit·单元测试·lua·luajit
千里镜宵烛13 小时前
Lua-迭代器
开发语言·junit·lua
井队Tell13 小时前
打造高清3D虚拟世界|零基础学习Unity HDRP高清渲染管线(第九天)
学习·3d·unity
烛阴1 天前
Lua字符串的利刃:模式匹配的艺术与实践
前端·lua
星空露珠1 天前
数独解题算法lua脚本
开发语言·数据结构·算法·游戏·lua
千年奇葩1 天前
Unity性能优化之:利用CUDA加速Unity实现大规模并行计算。从环境搭建到实战案例
c++·人工智能·unity·游戏引擎·cuda
l1t1 天前
Duckdb rusty_sheet插件使用心得
数据库·sql·lua·duckdb·rusty_sheet