Class in Incantium.Events.Send | Assembled in Incantium.ScriptableEvents
Extends ScriptableObject
The EventBus is a ScriptableObject that other scripts can subscribe to and listen to invocations from other scripts.
The main use case is to decouple code that needs to be developed and tested separately. In many circumstances, game objects that are depended on other systems (through references) cannot be tested in isolation without their references being set. The EventBus solves this problem by creating a common point to which the dependent system are coupled.
As seen in the image at the top, the EventBus ScriptableObject has a build-in custom inspector where every subscription is visible. It also has an Invoke button to invoke every subscriber from the inspector for testing purposes, alongside a Reset button to remove all subscriptions.
using Incantium.Events.Send;
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
[SerializeField]
private EventBus<string> eventBus;
// Subscribe to the event anywhere you want.
private void Start() => eventBus.onReceive += Receive;
// Don't forget to unsubscribe from the event at some point.
private void OnDestroy() => eventBus.onReceive -= Receive;
// Listen to the event.
private void Receive(string message) => Debug.Log(message);
// Invoke the event when needed.
public void OnClick() => eventBus.Send("Hello World");
}Info: This package includes basic event bus implementations for default/void, boolean, integer, float, double, string, Vector2, Vector3, and GameObject.
Event, when subscribed to, will be notified when something else send a message.
Amount of event handlers connected to this event.
Method to call all methods subscribed to onReceive to deliver a message.
Method to remove all methods from this event.
