Skip to content

MuharremTozan/Agentic-Duel / .agent/skills/unity-event-bus

unity-event-bus

Verified

Advanced code-driven event bus with reflection-based bootstrapping. Provides zero-setup global messaging.

references/guide.md

View on GitHub
# Advanced Event Bus Guide

The Advanced Event Bus is a global, type-safe messaging system for Unity. Unlike Event Channels (which use ScriptableObjects), the Event Bus uses **static generic indices** and **Reflection** to decouple components without any Inspector setup.

## 1. When to Use
- **System-to-System Communication**: High-frequency messaging between core managers.
- **Zero-Setup Architectures**: When you want to add events without creating `.asset` files.
- **Data-Heavy Events**: Sending complex structs through the system with zero garbage collection.

## 2. Core Concepts
- **IEvent**: The message (must be a `struct` or `class`).
- **EventBus<T>**: The static hub for a specific event type.
- **EventBinding<T>**: The connection between your code and the bus.

## 3. How to Implement

### Step 1: Define an Event
```csharp
public struct HealthChangedEvent : IEvent {
    public float CurrentHealth;
}
```

### Step 2: Create and Register a Binding
Inside your MonoBehaviour's `OnEnable`:
```csharp
private EventBinding<HealthChangedEvent> _healthBinding;

void OnEnable() {
    _healthBinding = new EventBinding<HealthChangedEvent>(e => Debug.Log(e.CurrentHealth));
    EventBus<HealthChangedEvent>.Register(_healthBinding);
}
```

### Step 3: Raise the Event
```csharp
EventBus<HealthChangedEvent>.Raise(new HealthChangedEvent { CurrentHealth = 50f });
```

## 4. Performance & Cleanup
- **Structs**: Use `struct` for events to avoid Heap allocations (GC-friendly).
- **Auto-Cleanup**: The `EventBusBootstrapper` automatically clears all static references when you exit Play Mode in the Editor, preventing memory leaks in the Unity Editor.
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall