【功能】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
相关推荐
三只坚果6 小时前
blender制作动画导入unity两种方式
unity·游戏引擎·blender
benben0447 小时前
《Unity Shader入门精要》学习笔记二
unity·unity shader
YF云飞8 小时前
Unity音频管理:打造沉浸式游戏音效
游戏·unity·游戏引擎·游戏程序·个人开发
SmalBox13 小时前
【渲染流水线】[逐片元阶段]-[裁剪测试]以UnityURP为例
unity·渲染
与火星的孩子对话16 小时前
Unity高级开发:反射原理深入解析与实践指南 C#
java·unity·c#·游戏引擎·lucene·反射
阿赵3D19 小时前
Unity2022打包安卓报错的奇葩问题
android·unity·安卓
小徐小徐编程不急1 天前
unity实现背包拖拽排序
unity·游戏引擎
萘柰奈1 天前
Unity进阶--C#补充知识点--【Unity跨平台的原理】Mono与IL2CPP
unity·c#·游戏引擎
淡海水1 天前
【原理】Unity GC 对比 C# GC
unity·c#·gc·垃圾回收
阿赵3D2 天前
Unity引擎播放HLS自适应码率流媒体视频
unity·游戏引擎·音视频·流媒体·hls