Hello fellow Unity developers,
I’ ll be sharing information on how Scriptable Objects can be used as an Event System in this article.
I suppose you’ ve already read the previous 3 posts on Scriptable Objects. If not please follow the links below for some foundation information and use-cases:
As the 4th and last post of Scriptable Objects series, we’ ll be discussing the implementations to develop an event system with using Scriptable Objects.
So the basic idea comes from the article below:
We’ ll be discussing a similar architecture for the event system more or less. Let’ s dive into the technical details:
We’ ve several different structures in our event system:
- Event Listeners
- Event Proxies
- Event Raisers
These’ re the event classes. Other classes can listen or raise these events. The implementation is as follows:
As you can see here our interface has 3 methods:
- AddListener receives an IEventListener so events will know which listeners listen to them
- RemoveListener is to remove a listener
- Raise is to raise the event so all listeners notified.
Event listeners are relatively simple. The interface has only the event receiving functionality as follows:
Event proxies are monobehaviours which automatically listen to a registered event and call unity events whenever the event is raised. These components allow developer implementing logic without any coding if the events are configured to be raised in proper places. Once the skeleton of the logic is implemented with events raised in the required places, event proxies mediate the events to script executions.
We may say these are some helper components. These are simple components that raise a registered event whenever a particular thing happens in the game. You can see an example raiser below:
This one raises the registered game event when a trigger message sent to the gameobject that has this component.
You might want to see this repo for a good implementation of an event system:
Also this one as a brief event system implementation:
Hope you enjoyed reading,