66 lines
1.7 KiB
Lua
66 lines
1.7 KiB
Lua
--[[
|
|
[使用方法]
|
|
====初始化====
|
|
local UGCDelegateMulticast = require("Script.Common.UGCDelegateMulticast");
|
|
local DelObj = UGCDelegate.New(self, self.SomeFunction);
|
|
DelObj:Add(self, self.AnotherFunction);
|
|
|
|
====在需要调用的地方====
|
|
DelObj:Broadcast(Par1, Par2, Par3);
|
|
--]]
|
|
|
|
---@class UGCDelegateMulticast
|
|
local UGCDelegateMulticast = LuaClass("UGCDelegateMulticast");
|
|
|
|
--- 绑定回调函数列表
|
|
UGCDelegateMulticast.EventsHandler = {};
|
|
|
|
|
|
--- 构造函数
|
|
---@param InObject any
|
|
---@param InFunc function @函数变量
|
|
function UGCDelegateMulticast:ctor(InObject, InFunc)
|
|
UGCDelegateMulticast:Add(InObject, InFunc);
|
|
end
|
|
|
|
|
|
--- 添加回调函数
|
|
---@param InObject any
|
|
---@param InFunc function @函数变量
|
|
function UGCDelegateMulticast:Add(InObject, InFunc)
|
|
table.insert(UGCDelegateMulticast.EventsHandler, {Object=InObject, Func=InFunc});
|
|
end
|
|
|
|
|
|
--- 移除回调函数
|
|
---@param InObject any
|
|
---@param InFunc function @函数变量
|
|
function UGCDelegateMulticast:Remove(InObject, InFunc)
|
|
for _, FuncData in pairs(UGCDelegateMulticast.EventsHandler) do
|
|
if FuncData.Object == InObject and FuncData.Func == InFunc then
|
|
FuncData.Object = nil;
|
|
FuncData.Func = nil;
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
--- 清除所有的回调绑定
|
|
function UGCDelegateMulticast:Clear()
|
|
self.EventsHandler = {};
|
|
end
|
|
|
|
|
|
--- 调用所有的回调函数
|
|
function UGCDelegateMulticast:Broadcast(...)
|
|
for _, FuncData in pairs(UGCDelegateMulticast.EventsHandler) do
|
|
if FuncData.Object ~= nil then
|
|
FuncData.Func(FuncData.Object, ...);
|
|
elseif FuncData.Func ~= nil then
|
|
FuncData.Func(...);
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
return UGCDelegateMulticast; |