Skip to content

Instantly share code, notes, and snippets.

@Sov3rain
Last active August 30, 2025 11:47
Show Gist options
  • Select an option

  • Save Sov3rain/99296ba2c86989700a94ccc8aaf200c7 to your computer and use it in GitHub Desktop.

Select an option

Save Sov3rain/99296ba2c86989700a94ccc8aaf200c7 to your computer and use it in GitHub Desktop.
Simple 0 dependency Event Bus for Unity C#
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