Delegates and Events in Unity C#

In Unity, delegates are a type of callback function that allow you to subscribe and trigger events. They are a powerful tool that allows you to decouple different parts of your game’s code, making it more flexible and maintainable.

Delegates are essentially pointers to functions. You can think of them as a way to pass a function as an argument to another function, or to store a function as a variable. Unity provides a number of built-in delegates, such as UnityAction, which can be used to define and trigger events.

Setting up Delegates / UnityEvents

Here’s an example of how to use a delegate in Unity:

using UnityEngine;
using UnityEngine.Events;

public class Example : MonoBehaviour
{
    public UnityEvent OnButtonClick;

    void Start()
    {
        // Subscribe to the OnButtonClick event
        OnButtonClick.AddListener(OnButtonClicked);
    }

    void OnButtonClicked()
    {
        Debug.Log("Button clicked!");
    }
}
C#

In this example, we define a public UnityEvent called OnButtonClick. This event can be subscribed to by other parts of the code using the AddListener() method. When the event is triggered, all the subscribed functions will be called. In this case, we’re subscribing to the OnButtonClick event in the Start() method, and defining a function called OnButtonClicked() that will be called when the event is triggered.

Delegates can also be used with parameters. For example:

using UnityEngine;
using UnityEngine.Events;

public class Example : MonoBehaviour
{
    public UnityEvent<string> OnPlayerDeath;

    void Start()
    {
        // Subscribe to the OnPlayerDeath event
        OnPlayerDeath.AddListener(OnPlayerDied);
    }

    void OnPlayerDied(string playerName)
    {
        Debug.Log(playerName + " has died!");
    }
}
C#

In this example, we define a public UnityEvent<string> called OnPlayerDeath, which takes a string parameter. When the event is triggered, we pass in the name of the player who died, which is then used in the OnPlayerDied() function.

Trigger / Invoke Events in Unity

To trigger a UnityEvent simply call the Invoke method as shown below:

using UnityEngine;
using UnityEngine.Events;

public class Example : MonoBehaviour
{
    public UnityEvent OnButtonClick;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Trigger the OnButtonClick event
            OnButtonClick.Invoke();
        }
    }
}
C#

In this example, we’re checking for the Space key to be pressed in the Update() method. When it’s pressed, we call the Invoke() method on the OnButtonClick delegate, which will trigger all the functions that are subscribed to the event. In this case, it would trigger the OnButtonClicked() function we defined earlier.

You can also pass arguments to the event when triggering it, like this:

using UnityEngine;
using UnityEngine.Events;

public class Example : MonoBehaviour
{
    public UnityEvent<string> OnPlayerDeath;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Trigger the OnPlayerDeath event with a parameter
            OnPlayerDeath.Invoke("Player1");
        }
    }
}
C#

In this example, we’re triggering the OnPlayerDeath event with the parameter “Player1”. This would trigger the OnPlayerDied() function we defined earlier, with the playerName argument set to “Player1”.

Leave a Reply