Example of implementation Event Bus system for Unity, allowing different parts of your application to communicate with each other through events.
The implementation is based on Enums, which makes it easy to define and manage events in your code.
With this system in place, you can create a more organized and efficient application by decoupling different parts of the code that need to communicate with each other.
For more details, please read this article on HackerNoon.
- Install the UniRx library by importing the unitypackage file into your project or through the Unity Package Manager by adding to your manifest.json:
{
"dependencies": {
"com.neuecc.unirx": "7.1.0"
},
"scopedRegistries": [
{
"name": "package.openupm.com",
"url": "https://package.openupm.com",
"scopes": [
"com.neuecc.unirx"
]
}
]
}
- Import the eventify.unitypackage file into your Unity project by choosing Assets > Import Package > Custom Package from the menu.
Raising an event using the extension method Raise
with and without arguments:
// Raising simple event without params
UiEvent.BuyButtonClicked.Raise();
// Raising event with data
UiEvent.MouseClick.Raise<Vector2>(new Vector2())
// Raising network event through Photon Unity Networking
NetEvent.PlayerReady.Raise(_playerId);
Subscribing and unsubscribing to events from different event buses with different arguments:
_disposable = new CompositeDisposable
{
// Subscribing to all different event buses in the same way
UiEvent.ExitBattleClick.Subscribe(OnExitBattleClick),
BackendEvent.BattleReset.Subscribe(OnBattleReset),
DelayedEvent.ChangeWindDirection.Subscribe<Direction>(OnChangeWindDirection),
NetEvent.PlayerReady.Subscribe<PlayerReadyData>(OnPlayerReady),
DebugEvent.KillOpponentUnits.Subscribe(OnKillOpponentUnits),
EditorEvent.SelectScene.Subscribe<string>(OnEditorSelectScene),
};
// Unsubscribe
_disposable.Dispose();
Here's how to filter events from the bus based on the parameters passed:
_disposable = new CompositeDisposable
{
// Subscribe to the SetSoundVolume event with any value
UiEvent.SetSoundVolume.Subscribe<float>(OnSetVolume),
// Subscribe to the SetSoundVolume event
// and call the OnMute method when it is raised with a value of 0f
UiEvent.SetSoundVolume.Where<float>(x => x == 0f).Subscribe(OnMute),
};