Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


UnityEvent

A zero argument persistent callback that can be saved with the Scene.
Read time 1 minuteLast updated 6 days ago

Definition

public class UnityEvent : UnityEventBase, ISerializationCallbackReceiver

Remarks

using UnityEngine;using UnityEngine.Events;public class ExampleClass : MonoBehaviour{ UnityEvent m_MyEvent; void Start() { if (m_MyEvent == null) m_MyEvent = new UnityEvent(); m_MyEvent.AddListener(DoSomething); } void Update() { if (Input.anyKeyDown && m_MyEvent != null) { m_MyEvent.Invoke(); } } void DoSomething() { Debug.Log("Callback called"); }}
Note: UnityEvent can also be awaited in any async method.

Constructors

Constructor

Description

UnityEvent()Constructor.

Methods

Method

Description

AddListenerAdds a non-persistent listener to the UnityEvent.
InvokeInvoke all registered callbacks (runtime and persistent).
RemoveListenerRemove a non persistent listener from the UnityEvent. If you have added the same listener multiple times, this method will remove all occurrences of it.

Inheritance