Is there a class about unity event system?

I want learn something about event system. So, I’m wondering is there a class about unity event system?

First, I am assuming that when you say ‘class’ you mean ‘course’. I don’t think there is a course per se. A lot of courses include a lecture on it as part of the overall course.

What would you like to know?

Unity events are much like normal events, but you can access them in the editor. With that I mean that you can have a serialized field of type UnityEvent. This will expose the event in the inspector. You can then assign listeners to the event, and when you invoke (fire) the event, all the listeners will be called to handle it.
Usually, UnityEvents don’t take parameters, but you can have UnityEvents that do take a single parameter by specifying a generic type for the event like UnityEvent<float>. This will then be an event that can invoke listeners that expect a float value. When you invoke it, you will specify the float value to pass to all the listeners. However, unity don’t like generic parameters in the inspector, so you need to wrap those in a class without generic parameters, for example:

public class HealthChangedUnityEvent : UnityEvent<float> { }

Listeners on this event must now accept a float value, and it must be passed when invoking the event. Also note that this doesn’t have to be a built-in type. You can specify a struct or class, too, if you want to pass more than one parameter.

Let’s take an example.

Your character has health, and we display this in the UI. When the character takes damage, we want to update the UI.

// The Health class
public class Health : MonoBehaviour
{
    // Use the wrapped event from above
    [SerializeField] HealthChangedUnityEvent _healthChangedEvent;

    private float _currentHealth = 100f;

    public void TakeDamage(float amount)
    {
        // reduce the current health by amount
        _currentHealth -= amount;
        // notify listeners that the health has changed, and supply the new health value
        _healthChangedEvent.Invoke(_health);
    }
}

// The UI class
public class HealthUI : MonoBehaviour
{
    [SerializeField] Text _healthText;

    public void OnHealthChanged(float newHealth)
    {
        // Handle event from Health to update the health display
        _healthText.text = newHealth.ToString();
    }
}

Hook it up:

Now the UI will update whenever TakeDamage is called on the health system.

You can also bind to the event in code:

// This assumes the HealthUI uses a standard singleton pattern
_healthChangedEvent.AddListener(HealthUI.Instance.OnHealthChanged);

That’s pretty much all there is to it.

2 Likes

Thanks Bix!
This is a very simple version of the Observer Pattern (Unity Events System).

There is a TON of stuff that could be made into a course. It would be a good start for GameDev to identify all their courses that already include Events and Call-backs and maybe put them into an “Event System Collection.”

I just finished “Unity UIToolkit: Introduction To Editor Scripting” (Gary was awesome) and there was a fair number of Unity built in event handling (like: loadTasksButton.clicked += LoadTasks;) and some general use call-backs (like: taskText.RegisterCallback(AddTask); & searchBox.RegisterValueChangedCallback(OnSearchTextChange); )

In the final challenge Gary wanted us to update the notificaitons and I wanted to “grey out” and make unclickable the “Load Tasks” button until there was a TaskListSO (scriptable object) in the ObjectField (typeof(TaskListSO)). For the life of me I have no idea how to setup a call-back for a state change / update of the ObjectField. (In the course, we just used the null check If-Then to “notify” the user that nothing was loaded. Which does work, but I want to prevent “bad clicks” and not just handle them.)

If there was an event course or at least a listing of courses that have the event system and call-backs covered, it would be kinda cool. I cannot imagine the course being a long one because most of the call-back stuff is ad hoc and really hard to put into a course, but maybe a lecture on how to read the Unity docs (and unreal docs) on their call-back/event systems (Heck even Android’s and IOS’s (Which are needed for cloud data saves, the payment systems, etc))

IDK of Rick, Ben, and Crew would even want to do something like this because it is such a niche section of coding but it is a section of coding who’s implementation isn’t very well understood by many. (Which is why Unity, Gadot, Unreal, etc all have an event system implemented.)

Privacy & Terms