This module is part of the Unity Essentials ecosystem and follows the same lightweight, editor-first approach. Unity Essentials is a lightweight, modular set of editor utilities and helpers that streamline Unity development. It focuses on clean, dependency-free tools that work well together.
All utilities are under the UnityEssentials namespace.
using UnityEssentials;Install the Unity Essentials entry package via Unity's Package Manager, then install modules from the Tools menu.
-
Add the entry package (via Git URL)
- Window → Package Manager
- "+" → "Add package from git URL…"
- Paste:
https://github.com/EggyStudio/UnityEssentials.git
-
Install or update Unity Essentials packages
- Tools → Install & Update UnityEssentials
- Install all or select individual modules; run again anytime to update
Quick overview: Strongly‑typed, lightweight event bus for decoupled messaging between MonoBehaviours. Events implement
IEvent; listeners registerEventBinding<T>and receive callbacks whenEventBus<T>.Raiseis called.
A minimal, generic publish/subscribe utility intended for scene‑local messaging. Event types are plain interfaces implementing IEvent. Listeners create an EventBinding<T> and register it with the corresponding EventBus<T>. When an event is raised, registered bindings are invoked in a safe snapshot so modifications during dispatch do not break iteration. In the editor, all buses are cleared on exiting Play Mode.
- Strongly‑typed events
- Define any number of event types by implementing
IEvent - Generic static bus per event type:
EventBus<T>
- Define any number of event types by implementing
- Simple bindings
EventBinding<T>supports bothAction<T>(with payload) andAction(no‑args) handlers, plus additiveAdd/Remove
- Safe dispatch
- Snapshot taken on
Raiseprevents collection modification issues during callbacks
- Snapshot taken on
- Editor integration
- All buses cleared automatically on Exiting Play Mode (via
EventBusUtilities)
- All buses cleared automatically on Exiting Play Mode (via
- Minimal surface
Register,Deregister, andRaiseonly; no external dependencies
- Unity 6000.0+
- Add your event types and listener scripts to a scene
- Uses
EventBusUtilitiesto initialize and clear buses; it relies on type discovery forIEventimplementors
- Define an event type
public struct PlayerDamaged : IEvent
{
public int Amount;
public Vector3 HitPoint;
}- Listen for an event
public class DamageListener : MonoBehaviour
{
private EventBinding<PlayerDamaged> _binding;
private void OnEnable()
{
_binding = new EventBinding<PlayerDamaged>(OnPlayerDamaged);
EventBus<PlayerDamaged>.Register(_binding);
// Optionally also react without payload
_binding.Add(() => Debug.Log("Damage received"));
}
private void OnDisable()
{
EventBus<PlayerDamaged>.Deregister(_binding);
_binding = null;
}
private void OnPlayerDamaged(PlayerDamaged evt)
{
Debug.Log($"Damage: {evt.Amount} at {evt.HitPoint}");
}
}- Raise an event
// From anywhere (main thread)
EventBus<PlayerDamaged>.Raise(new PlayerDamaged { Amount = 10, HitPoint = transform.position });Additional notes
- Multiple handlers can be added to the same binding via
Add(Action<T>)andAdd(Action); remove withRemove(...) - Always deregister in
OnDisable(or when no longer needed) to avoid stale listeners
- Event storage
- Each
EventBus<T>maintains aHashSet<IEventBinding<T>>of current bindings
- Each
- Dispatch
- On
Raise, a snapshot of current bindings is taken; each binding is checked for continued membership and invoked (OnEvent(@event)thenOnEventNoArgs())
- On
- Lifecycle helpers
EventBusUtilitiesdiscoversIEventtypes at startup and prepares their bus types; on Exiting Play Mode, it reflects a private staticClearon each bus to release bindings
- Threading: The bus is not thread‑safe; publish/subscribe on the main thread
- Lifetime: Bindings are static‑lifetime per app domain; deregister to prevent leaks
- Exceptions: A listener throwing will propagate unless you guard inside your handler
- Ordering:
HashSetis unordered; handler invocation order is unspecified - Scope: This is a simple in‑process bus; no persistence, buffering, or cross‑scene routing is provided
Runtime/EventBus.cs– Generic bus with Register/Deregister/Raise and internal ClearRuntime/EventBinding.cs– Binding with payload and no‑args actions, add/remove helpers; definesIEventRuntime/EventBusUtility.cs– Type discovery, PlayerLoop/editor hooks, bus initialization, and global clearRuntime/Examples/– Simple usage examplesRuntime/UnityEssentials.EventBus.asmdef– Runtime assembly definition
unity, event-bus, pubsub, messaging, decoupled, events, runtime
