-
Notifications
You must be signed in to change notification settings - Fork 2k
Event system
The ScriptableObject event system is a way to avoid the use of the Singleton pattern. We want to avoid using the latter for multiple reasons such as the fact that it allows rigid connections between the systems since it’s not modular. In addition, the pattern does not allow the systems to exist separately and they will always depend on each other. This is of course hard to maintain and reuse.
Event system allows you to raise an event from a trigger such as when you enter a collider set to isTrigger
or by performing an action like pressing a button. Once the event is raised we have what we call an event listener which will be basically waiting for when we have that event raised. On the graph above, we can visualize this system better. Both the script that fires the event and the event listener are Monobehaviours. Since we are using the Scriptable object Event as an anchor between the 2 systems, those Monobehaviour scripts can live in 2 different scenes in a completely independent way. As a reminder, the event will live in the assets folder since it is a ScriptableObject.
- Events examples in the project
- Create an Event Scriptable Object
- Create an Event listener
- Add a new type of Event - Example
- Add a new type of Listener - Example
- More info
Events can be raised with or without parameters. Here is a few examples of events we have in the project:
- Void Events are events that are raised with no parameters. A good example of this is when we raise an event for game exit.
-
Int Events are events that are raised with an
int
parameter. We can use them when we unlock an achievement for instance. -
Load Events are events that are raised with 2 parameters, an array of
GameScene
which are the scenes we want to load and abool
parameter to specify if we want to display a loading screen.
To create an event, right click in the assets folder, under Game Event choose the type of event that fits best to your case.
Select the GameObject to which you want to attach the listener. Add a new script component to it and look for the listener script corresponding to the event you have created.
The Monobehaviour that listens to the event could be an independent script or included in another script. In the project, we are doing so for the scene loading event listener. In this case, the listener is included in the location loader script, meaning that the script acts as container of the scene loading methods but also as a listener. This is particularly useful if we are not using a specific listener more than once in the project.
You can add a new type of event depending on how many parameters you want to use on the raise method. This is an example of the Int event:
using UnityEngine.Events;
using UnityEngine;
[CreateAssetMenu(fileName = "IntGameEvent", menuName = "Game Event/Int")]
public class IntGameEvent : ScriptableObject
{
public UnityAction<int> eventRaised;
public void Raise(int value)
{
eventRaised.Invoke(value);
}
}
Replace the int
variable with the type you need. You can also add more than one parameter.
You can add a new type of Listener depending on how many parameters were used to raise the event. This is an example of the Int Event listener:
using UnityEngine;
using UnityEngine.Events;
[System.Serializable]
public class IntEvent : UnityEvent<int>
{
}
public class IntEventListener : MonoBehaviour
{
public IntGameEvent intGameEvent;
public IntEvent OnEventRaised;
private void OnEnable()
{
//Check if the event exists to avoid errors
if (intGameEvent == null)
{
return;
}
intGameEvent.eventRaised += Respond;
}
private void OnDisable()
{
if (intGameEvent == null)
{
return;
}
intGameEvent.eventRaised -= Respond;
}
public void Respond(int value)
{
if (OnEventRaised == null)
{
return;
}
OnEventRaised.Invoke(value);
}
}
- If you would like to know more about multiple arguments version of UnityEvent, you can see examples in the documentation using up to 4 parameters.
Unity Open Projects - Open-source games made by Unity + the community
We are looking forward to see what you will create ❤
- the Unity Creator Advocacy team
- Game architecture overview
- Playing the game in Unity
- Creating a new playable scene
- Building the game
The game systems explained, with API examples. For programmers.
- Event system
- State machine
- Input
- Object pooling
- Runtime anchors
- Audio system
- Narrative
- Dialogue system
- Cutscenes system
- Inventory and Cooking
- Combat
- User Interface
How-tos for designers to expand the game's gameplay.
- Adding quests
- Adding items
- Creating dialogues
- Making a cutscene