优化事件系统

This commit is contained in:
mnjnhuang 2025-11-18 16:46:49 +08:00
parent e4a7f21b96
commit 4c2b7203ad

View File

@ -23,12 +23,12 @@ public class EventSvcHandler
{ {
if (!actionBindMap.TryGetValue(eventName, out var value)) if (!actionBindMap.TryGetValue(eventName, out var value))
{ {
if (GetNeedDebugLog()) Debug.LogError($"{msg}: 未绑定的数据类型[{eventName}]"); Debug.LogError($"{msg}: 未绑定的数据类型[{eventName}]");
return false; return false;
} }
if (value == t) return true; if (value == t) return true;
if (GetNeedDebugLog()) Debug.LogError($"{msg}: 未响应的数据类型[{eventName}]"); Debug.LogError($"{msg}: 未响应的数据类型[{eventName}]");
return false; return false;
} }
@ -40,6 +40,7 @@ public class EventSvcHandler
if (!GET_DEFINE_EVENT(eventName, typeof(T), "注册事件失败")) return; if (!GET_DEFINE_EVENT(eventName, typeof(T), "注册事件失败")) return;
actionBroadcast.TryAdd(eventName, null); actionBroadcast.TryAdd(eventName, null);
actionBroadcast[eventName] = (Action<T>)actionBroadcast[eventName] + callback; actionBroadcast[eventName] = (Action<T>)actionBroadcast[eventName] + callback;
if (GetNeedDebugLog()) Debug.Log($"注册事件成功: [{eventName}]");
} }
// ================================ // ================================
@ -50,6 +51,7 @@ public class EventSvcHandler
if (!GET_DEFINE_EVENT(eventName, typeof(T), "注销事件失败")) return; if (!GET_DEFINE_EVENT(eventName, typeof(T), "注销事件失败")) return;
actionBroadcast[eventName] = (Action<T>)actionBroadcast[eventName] - callback; actionBroadcast[eventName] = (Action<T>)actionBroadcast[eventName] - callback;
if (actionBroadcast[eventName] == null) actionBroadcast.Remove(eventName); if (actionBroadcast[eventName] == null) actionBroadcast.Remove(eventName);
if (GetNeedDebugLog()) Debug.Log($"注销事件成功: [{eventName}]");
} }
// ================================ // ================================
@ -61,6 +63,11 @@ public class EventSvcHandler
if (actionBroadcast.TryGetValue(eventName, out var action)) if (actionBroadcast.TryGetValue(eventName, out var action))
{ {
(action as Action<T>)?.Invoke(param); (action as Action<T>)?.Invoke(param);
if (GetNeedDebugLog()) Debug.Log($"触发广播事件: [{eventName}]");
}
else
{
Debug.LogWarning($"触发广播事件错误: 未找到事件[{eventName}]");
} }
} }
} }