【功能】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
相关推荐
与火星的孩子对话2 小时前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
幻世界4 小时前
【Unity智能模型系列】Unity + MediaPipe + Sentis + ArcFace模型:构建高效人脸识别比对系统
unity·游戏引擎
死也不注释20 小时前
【Unity 编辑器工具开发:GUILayout 与 EditorGUILayout 对比分析】
unity·编辑器·游戏引擎
小赖同学啊1 天前
物联网中的Unity/Unreal引擎集成:数字孪生与可视化控制
物联网·unity·游戏引擎
Zlzxzw1 天前
使用unity创建项目,进行动画制作
unity·游戏引擎
X_StarX2 天前
【Unity笔记01】基于单例模式的简单UI框架
笔记·ui·unity·单例模式·游戏引擎·游戏开发·大学生
九班长2 天前
Golang服务端处理Unity 3D游戏地图与碰撞的详细实现
3d·unity·golang
ysn111112 天前
NGUI实现反向定位到层级面板结点
unity
Thomas_YXQ2 天前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
旷世奇才李先生2 天前
Lua 安装使用教程
开发语言·lua