These are a way to set up events and reference them easily.
To create one, right click in a folder -> Create -> Game Event Scriptable Object.
The game event is raised with the Raise()
method in GameEventScriptableObject
. It also has an overload to send data, Raise(object data)
.
GameEventListener
is the main way to listen for game events. It responds to a game event by invoking a UnityEvent. UnityEvents do arbitrary things, like Unity's Button component. For example, you can drag in a reference to a gameObject, and select GameObject.SetActive(bool)
to make the event activate or deactivate the gameObject.
GameEventScriptableObject
also raises a C# event. Code can directly subscribe to that event, rather than using GameEventListener
. It can be easier to use the C# events if there are a large number of game event scriptable objects.
The C# event isn't for performance reasons. Although UnityEvent generates a lot of garbage when invoked in the editor, it doesn't appear to have that problem in builds.
Unity's Documentation for Scriptable Objects
Video About Game Event Scriptable Objects