Last active
August 30, 2025 11:47
-
-
Save Sov3rain/99296ba2c86989700a94ccc8aaf200c7 to your computer and use it in GitHub Desktop.
Simple 0 dependency Event Bus for Unity C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using UnityEngine; | |
| using System.Collections.Generic; | |
| public static class EventManager<T> | |
| { | |
| private static readonly List<Action<T>> _listeners = new List<Action<T>>(8); | |
| public static void AddListener(Action<T> handler) | |
| { | |
| if (!_listeners.Contains(handler)) | |
| _listeners.Add(handler); | |
| } | |
| public static void RemoveListener(Action<T> handler) | |
| { | |
| _listeners.Remove(handler); | |
| } | |
| public static void RemoveAllListeners() => _listeners.Clear(); | |
| public static void Invoke(T arg) | |
| { | |
| for (int i = 0; i < _listeners.Count; i++) | |
| { | |
| try | |
| { | |
| _listeners[i](arg); | |
| } | |
| catch (Exception ex) | |
| { | |
| Debug.LogError( | |
| $"[EventManager<{typeof(T).Name}>] Listener threw an exception: {ex}"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment