Unity provides the UnityEvents API as a Unity-specific alternative to standard C# events and delegates. The main advantage of Unity events over standard C# events is that Unity events are serializable, meaning you can configure them in the Inspector window.
A UnityEvent
can be added to any MonoBehaviour
and is executed at runtime like a standard C# delegate. When a UnityEvent
is declared in a MonoBehaviour
it appears in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary window where you can define callbacks that persist between Edit time and runtime.
Unity events have similar limitations to standard C# delegates:
UnityEngine.Object
as the target and the unmanaged (C++) counterpart object has been destroyed, the callback will not be invoked. Refer to Null references for more context.using UnityEngine.Events
UnityEvent
Select the GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary with the script component that contains your declared UnityEvent
field(s).
Click the + button under the name of an event to add a slot for a callback.
Select the UnityEngine.Object you want to receive the callback. You can use the object selector or drag and drop an object into the field.
Select the function you want to be called when the event happens. The dropdown selector is populated with filtered lists of appropriate methods available on the GameObject and its components.
Repeat steps 1–4 as required to add additional callbacks for the same event.
When configuring a UnityEvent
in the Inspector window there are two types of function calls that are supported:
UnityEvent
being invoked. This is appropriate for values that vary at runtime, for example a float
representing a variable amount of damage that a character sustains on each attack. The UI filters the callbacks and only shows the dynamic functions with signatures that are valid for the type of UnityEvent
. For example, if you have a UnityEvent<string>
, the function selector lists any functions that accept a string
parameter under the Dynamic string header.By default a UnityEvent
in a Monobehaviour
binds dynamically to a void
function. But you can create a UnityEvent
with up to four generic type parameters as shown in the following example:
using UnityEngine;
using UnityEngine.Events;
public class GenericTest : MonoBehaviour
{
public UnityEvent<int, int, bool, string> myEvent;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
if (myEvent == null)
{
myEvent = new UnityEvent<int, int, bool, string>();
}
myEvent.AddListener(Ping);
}
// Update is called once per frame
void Update()
{
if (Input.anyKeyDown && myEvent != null)
{
myEvent.Invoke(5, 6, true, "Hello");
}
}
void Ping(int i, int j, bool print, string text)
{
if (print)
{
Debug.Log("Ping: " + text + i + j);
}
}
}