using System; using System.Collections.Generic; public class EventBus { private static Dictionary s_event = new(); public static void Subscribe(Action listener) { Type eventType = typeof(T); if (s_event.TryGetValue(eventType, out var exisitingDelegate)) { s_event[eventType] = Delegate.Combine(exisitingDelegate, listener); } else { s_event[eventType] = listener; } } public static void Unsubscribe(Action listener) { Type eventType = typeof(T); if (s_event.TryGetValue(eventType, out var existingDelegate)) { var newDelegate = Delegate.Remove(existingDelegate, listener); if (newDelegate == null) { s_event.Remove(eventType); } else { s_event[eventType] = newDelegate; } } } public static void Raise(T eventArgs) { if (s_event.TryGetValue(typeof(T), out var exisingDelegate)) { (exisingDelegate as Action)?.Invoke(eventArgs); } } }