UnityEvent
A zero argument persistent callback that can be saved with the Scene.
Read time 1 minuteLast updated 6 days ago
Definition
- Type: Class
- Namespace: UnityEngine.Events
- Assembly: UnityEngine.CoreModule
- Inherits from: UnityEventBase
- Implements: ISerializationCallbackReceiver
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 |
|---|---|
| AddListener | Adds a non-persistent listener to the UnityEvent. |
| Invoke | Invoke all registered callbacks (runtime and persistent). |
| RemoveListener | Remove a non persistent listener from the UnityEvent. If you have added the same listener multiple times, this method will remove all occurrences of it. |