Event Bus
A powerful, type-safe event system for Unity with fluent interface design.
Overview
EventBus provides a robust, decoupled communication system for your Unity projects. Built with type safety and ease of use in mind, it offers a fluent interface for raising and listening to events, with or without payload data.
Key Features
Type-Safe Events: Uses interfaces for event definitions, ensuring compile-time type checking
Fluent Interface: Intuitive builder pattern for clear and concise event handling
Payload Support: Handle events with or without custom data payloads
Event Caching: Optional kick-start feature to receive the last event upon subscription
Zero Dependencies: Works right out of the box with no external dependencies
Why Use It?
Type-Safe Communication
No more string-based events or manual delegate management. Our EventBus ensures all event communications are type-safe and verified at compile time, eliminating runtime errors from typos or wrong event types.
Clean Architecture
Decoupled Systems: Services and components can communicate without direct references
Modular Design: Add or remove features without changing existing code
Easy Maintenance: Change internal logic without affecting other systems
Clear Dependencies: Easily track event flows through your application
Developer Experience
Intuitive API: Simple, fluent interface that's easy to understand and use
IDE Friendly: Full auto-completion support for better productivity
Reduced Boilerplate: No need to write custom event systems or manager classes
Quick Integration: Start using in minutes with minimal setup
Powerful Features
Event Caching: New subscribers can automatically receive the most recent event
Payload Support: Send any type of data with your events
Sender Tracking: Always know which component triggered an event
Flexible Subscription: Subscribe and unsubscribe at any time
Perfect For
Service Architecture: Connect independent services without tight coupling
UI Updates: Keep UI in sync with game state changes
Cross-Scene Communication: Share data between different scenes
State Management: Handle game state changes efficiently
Analytics Integration: Track game events without modifying core systems
Production Ready
Performance Optimized: Efficient event dispatching with minimal overhead
Memory Conscious: Small memory footprint with proper cleanup
Battle Tested: Reliable in production environments
Unity Friendly: Designed specifically for Unity projects
This lightweight yet powerful event system will help you build more maintainable and scalable Unity applications while reducing development time and potential bugs.
API
Quick Start
Events are defined as interface with single Listener method.
Listeners should be inheritted with Event(s).
Raise an event, all listeners will receive this event call. EventBus will also cache the last raised event for listeners with kickstart.
Event Caching
Use KickStart() after Listen() when subscribing to automatically receive last raised event.
Clear Cache
Multiple Events Handling
Listening and Unlistening multiple events become easier.
An example with events in Quick Start. GameControl will listen all events, but not listen ScoreUpdateEvent when disabled.
Advanced Usage
The following example demonstrates how EventBus can elegantly connect services and UI layers without direct references, using a weather monitoring system.
Adding listener method in Event Interface can confirm correct listener method in obsersers.
This example demonstrates several powerful features of EventBus:
Service Decoupling: Services can communicate without direct references
Multiple Listeners: Both UI and Analytics respond to the same event
Automatic Updates: UI receives updates as soon as new data arrives
Clean Architecture: Clear separation between data providers and consumers
Task Utils is using in this example.
Best Practices
Define events as empty interfaces
Use meaningful interface names (e.g., IGameStartEvent, IPlayerDeathEvent)
Keep event payloads simple and serializable
Always unsubscribe when destroying objects to prevent memory leaks
Consider using
KickStart()for state-based eventsComfirm correct listener method by adding method in Event interface
Last updated