Astra Unity 2.6.6
Astra Unity Plugin
|
The Astra.Utils.EventBus is an implementation of the publisher/subscriber paradigm which facilitates clean decoupled communication between components. Astra uses the EventBus for UI events in the Character Editor, you are welcome to use and extend on top of this yourself.
Astra.Utils.Events is a central location wherein various core events and their payload configurations are defined. If you need to add an event, define it here using an existing event as a reference.
The Astra.Utils.EventBus instantiates a static EventBus object which maintains a collection of Subscriptions, objects which represent the association between an event, a subscriber, and the subscriber's callback method to be invoked upon event publication. In publisher-subscriber patterns this is sometimes also referred to as a broker
. A new Subscription is created and added to the collection when the Subscribe method is called.
Use Astra.Utils.EventBus.Subscribe to receive a callback when an event is triggered such as a button click on UI element or an asset of has been loaded.
Invoking the Astra.Utils.EventBus.Subscribe method creates a Astra.Utils.EventBus.Subscription representing the association between a given event, subscriber, and callback.
Type parameters:
TBusEvent
: Type of event to which to subscribeTSubscriber
: Type of subscriberParameters:
callback
: Callback to be invoked on publication of given eventsubscriber
: A reference to the subscribing ObjectAstra.Utils.EventBus.Unsubscribe allows you to remove your callback from the watched event.
Note: To avoid memory issues, make sure that objects unsubscribe before they are destroyed, sleep, etc.
Type parameters:
TBusEvent
: Type of event to which to unsubscribeTSubscriber
: Type of subscriberParameters:
subscriber
: A reference to the subscribing ObjectPayload data can be configured inline or by instantiating a new event and passing it into the Publish method.
You can create you own events and make use of the EventBus. All you need to do is create a struct or class that extends Astra.Utils.Events.BusEvent and defines a payload
property or a Astra.Utils.Events.PayloadEvent.
You can then publish in any place when you want your event to trigger.
You can see an example of a custom event in the Assets/Astra/Examples/Events
directory.