EventSystem = EventSystem or {} EventSystem.Events = {} --添加监听 function EventSystem:AddListener(EventType, Func, Object) if EventType == nil or Func == nil then UE.LogError("EventSystem:AddListener EventType or Func is nil!") return end local FuncData = {} FuncData.Object = Object FuncData.Func = Func if EventSystem.Events[EventType]==nil then local NewEventFuncs={} table.insert(NewEventFuncs ,FuncData) EventSystem.Events[EventType] = NewEventFuncs UE.Log("EventSystem:AddListener EventType[%s], Func[%s]", tostring(EventType), tostring(Func)) else table.insert(EventSystem.Events[EventType], FuncData) UE.Log("EventSystem:AddListener EventType[%s], Func[%s]", tostring(EventType), tostring(Func)) end end --移除监听 function EventSystem:RemoveListener(EventType, Func, Object) if EventType == nil or Func == nil then UE.LogError("EventSystem:RemoveListener EventType or Func is nil!") return end local EventFuncs = EventSystem.Events[EventType] if EventFuncs ~= nil then for i, FuncData in pairs(EventFuncs) do if FuncData.Func == Func and FuncData.Object == Object then local result = table.removeKey(EventFuncs, i) UE.Log("EventSystem:RemoveListener EventType[%s], Func[%s]", tostring(EventType), tostring(result)) break end end else UE.Log("EventSystem:RemoveListener EventFuncs[%s] is nil!", tostring(EventType)) end end --派发事件 function EventSystem:SendEvent(EventType, ...) UE.Log("EventSystem:SendEvent self[%s], EventType[%s]", tostring(self), tostring(EventType)) if EventType ~= nil then local EventFuncs = EventSystem.Events[EventType]; if EventFuncs ~= nil then for i, FuncData in pairs(EventFuncs) do if FuncData.Object ~= nil then FuncData.Func(FuncData.Object, ...) else FuncData.Func(...) end end else UE.Log("EventSystem:SendEvent EventFuncs[%s] is nil!", tostring(EventType)) end end end --- SetTimer ---@param Obj UObject ---@param Fun fun() ---@param IntevalSeconds float ---@return TimerHandle function EventSystem.SetTimer(Obj, Fun, IntevalSeconds) UE.Log("[Timer] 设置定时器 %s:%s", UE.GetPathName(Obj), tostring(Fun)) local TimerDelegate = ObjectExtend.CreateDelegate(Obj, Fun, Obj) local Handle = KismetSystemLibrary.K2_SetTimerDelegateForLua(TimerDelegate, Obj, IntevalSeconds, false) local R = {} R.Object = Obj R.Handle = Handle return R end --- SetTimerLoop ---@param Obj UObject ---@param Fun fun() ---@param IntevalSeconds float ---@return TimerHandle function EventSystem.SetTimerLoop(Obj, Fun, IntevalSeconds) local TimerDelegate = ObjectExtend.CreateDelegate(Obj, Fun, Obj); local Handle = KismetSystemLibrary.K2_SetTimerDelegateForLua(TimerDelegate, Obj, IntevalSeconds, true); local R = {}; R.Object = Obj; R.Handle = Handle; UE.Log("[Timer] 设置循环定时器 %s:%s H:%s", UE.GetPathName(Obj), tostring(Fun), tostring(R)); return R; end --- StopTimer ---@param HandlePair TimerHandle function EventSystem.StopTimer(HandlePair) if HandlePair == nil then assert(false, "[UGC][EventSystem.StopTimer] HandlePair is nil") else UE.Log("[Timer] 停止定时器 Object:%s Handle:%s", UE.GetPathName(HandlePair.Object), tostring(HandlePair.Handle)); KismetSystemLibrary.K2_ClearTimerHandle(HandlePair.Object, HandlePair.Handle); end end return EventSystem