What is an Event System in Unity?
The primary roles of the Unity Event System are as follows:
- Manage which GameObject is considered selected
- Control/Manage which Input Module is in use
- Change/Manage Raycasting (if required)
- Updating all Input Modules as required
If you’re a Unity developer, you must have heard of singletons, mostly we use them to avoid the pain of referencing MonoBehaviour instances to each and every script we need them in.
Event System: Singletons
So first, let’s take a look at how singletons are used :
You create a static variable of the type of the class you’re making a singleton of, then in the Start (or Awake) function you set the value of the singleton to be the current script, and this should be the only MonoBehaviour of it’s type in the whole scene.
So let’s say we need another such singleton and each singleton has a public method.
Now to call the two methods, in two different classes, we can use these singletons :
For smaller projects, this approach is absolutely harmless, but as the project size increases, more and more classes are added, you add more and more features and responsiveness in your game (e.g. play sound when hit, show blood, shake camera etc.), in such a scenario you’ll just keep filling your project with singletons to manage it properly, which will make it even more difficult to manage.
Creating Your Own Event System
A way to avoid this is creating your own event system and using it instead of countless singletons. It can be used like a central system to receive messages from all parts of your project and telling everyone who wants to know about those messages.
Player took damage? Just tell the central system that happened, it will tell everyone who’s got something to do with that (every class that has a function to fire when player is hurt).
Let’s see how to create an event system :
We create a delegate to with proper parameters, it’s just the introduction so I’ve not included any parameter. Then we create an event
OnButtonClick which we then raise with the help of
RaiseOnButtonClick. It checks if the event is not null and if not then invokes it.
Now we can simply call the raiser function to invoke OnButtonClick event. I shall do that by creating 2 subscriber scripts to demonstrate how Events can be used to perform multiple tasks in multiple classes.
GameEvents.OnButtonClick += Method2 is the syntax to subscribe to the event, while
GameEvents.OnButtonClick -= Method2, is the syntax to unsubscribe from the event and it’s a good practice to always unsubscribe from the events in the
OnDestroy method to avoid inviting bugs to party in your project.
And then we can change out Test.cs to call the raiser function.
In this pattern it’s very safe to subscribe and unsubscribe to events in the event system, and it is a very effective way of avoiding too many singletons.
It might seem a bit of a work at the start but once you get the hang of it, you won’t have a project without a central Event-System.
I’ll keep posting about useful things to learn and and short guides in future, follow me on twitter (@BuluSama) to stay updated about my next YouTube tutorial.
I hope you learnt something new from this post and will start using event systems.
Have a nice and productive day!!!
Want to contribute to TechLater.com?
We have a number of indie developers writing for TechLater.com, and we’d love to provide you with a platform to chronicle your journey – be it game development, coding, designing, ethical hacking or any other computer science.
It’s easy. Simply register here, and start writing!
Get notified of new articles and have fresh tech news delivered straight to your inbox.