Good day!Today I would like to briefly describe the use of events to get rid of garbage and provide complex and dynamic functionality to the code. It should be noted that this article is more focused on beginners who want to learn more about event systems.
We will analyze this whole thing on the example of a sound management system. This system will allow us to enable / disable music and sounds in the game settings.
Before we create a class manager that manages sound and music settings, we will create a simple serializable class. It will be used as a data model for saving to JSON.
using System.Collections; using System.Collections.Generic; using UnityEngine;
Now you can start writing a class manager. We will install it on the very first stage. This manager will be a global object and will not be deleted when going from scene to scene.
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO;
Now that the manager is ready, you can create an empty object on your starting scene and name it, for example
"_AUDIO_MANAGER" , and then add our class manager to it. You can do this simply by calling the component's add-on menu on the object and selecting
“Game Managers” => “Audio Manager” .
After that, we need to write a component that we will dock to each object with an AudioSource.
using System.Collections; using System.Collections.Generic; using UnityEngine;
In this way we can control the sounds / music in the game. This example does not in any way tell how to do it correctly, but only demonstrates the work of the system of events and listeners in Unity3D.
And finally, I want to talk about what we have used now. In the example below, a delegate was declared from which the listener was created:
public delegate void AudioSettingsChanged(); public event AudioSettingsChanged OnAudioSettingsChanged;
You can set the listener to perform under certain conditions and cling to them certain methods that will be executed when these conditions are reached.
And with the help of delegates, on the basis of which we created a listener, you can create callback functions. This can be especially useful for asynchronous methods (for example, when sending asynchronous POST requests).
I hope you will benefit from my little experience in this matter and you can apply this example to your projects. Also I will be happy to answer your questions (if someone does not understand something).