Event Bus
Declarations
EventBus.Subscribe(EventType eventType, UnityAction listener);
EventBus.Subscribe<T0>(EventType eventType, UnityAction<T0> listener);
EventBus.Subscribe<T0, T1>(EventType eventType, UnityAction<T0, T1> listener);
EventBus.Subscribe<T0, T1, T2>(EventType eventType, UnityAction<T0, T1, T2> listener);
EventBus.Unsubscribe(EventType eventType, UnityAction listener);
EventBus.Unsubscribe<T0>(EventType eventType, UnityAction<T0> listener);
EventBus.Unsubscribe<T0, T1>(EventType eventType, UnityAction<T0, T1> listener);
EventBus.Unsubscribe<T0, T1, T2>(EventType eventType, UnityAction<T0, T1, T2> listener);
EventBus.Publish(EventType eventType);
EventBus.Publish<T0>(EventType eventType, T0 parameter);
EventBus.Publish<T0, T1>(EventType eventType, T0 parameter0, T1 parameter1);
EventBus.Publish<T0, T1, T2>(EventType eventType, T0 parameter0, T1 parameter1, T2 parameter2);
Description
Enum based event bus sytem that allows for generic parameters to be passed through to the listeners.
You can subscribe to events through the Subscribe function.
You can unsubscribe to events through the Unsubscribe function.
You can publish events through the Publish function.
using UnityEngine;
public class CoinPickup : MonoBehaviour
{
private float value = 0f;
private bool isPickedUp = false;
private void OnEnable()
{
EventBus.Subscribe(EventType.GAME_START, InitializeCoinData);
}
private void OnDisable()
{
EventBus.Unsubscribe(EventType.GAME_START, InitializeCoinData);
}
private void InitializeCoinData()
{
value = 10f;
isPickedUp = false;
}
}
using UnityEngine;
public class CoinPickup : MonoBehaviour
{
private float value = 0f;
private bool isPickedUp = false;
private void OnEnable()
{
EventBus.Subscribe(EventType.GAME_START, InitializeCoinData);
EventBus.Subscribe(EventType.Coin_Pickup, Pickup);
}
private void OnDisable()
{
EventBus.Unsubscribe(EventType.GAME_START, InitializeCoinData);
EventBus.Unsubscribe(EventType.Coin_Pickup, Pickup);
}
private void InitializeCoinData()
{
value = 10f;
isPickedUp = false;
}
private void Pickup()
{
isPickedUp = true;
gameObject.SetActive(false);
}
}
public class PlayerController : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Coin")
{
EventBus.Publish(EventType.Coin_Pickup);
}
}
}
using UnityEngine;
public class CoinPickup : MonoBehaviour
{
private float value = 0f;
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
EventBus.Publish(EventType.Coin_Pickup, value, this.gameObject);
}
}
}
public class PlayerController : MonoBehaviour
{
private float coins = 0f;
private void OnEnable()
{
EventBus.Subscribe<float, GameObject>(EventType.Coin_Pickup, PickupCoin);
}
private void OnDisable()
{
EventBus.Unsubscribe<float, GameObject>(EventType.Coin_Pickup, PickupCoin);
}
private void PickupCoin(float value, GameObject coin)
{
coins += value;
coin.gameObject.SetActive(false);
}
}
Properites
Events | private readonly Dictionary of EventType and UnityEventBase that organizes all the events. |
Public Methods
Subscribe | Used to subscribe to a specific event. |
Unsubscribe | Used to unsubscribe to a specific event. |
Publish | Used to publish an event. |