UnityAction Vs UnityEvent
I have been using Unity for a while now and I recently stumbled on these two. From what I understand UnityAction performs the same way as an event in traditional C#. They can add functions to themselves and run them when called like Events in traditional C#. I looked into the documentation of UnityAction here: https://docs.unity3d.com/ScriptReference/Events.UnityAction.html However, then what does UnityEvent do? Unity Event also makes methods run using the AddListener. So what's the difference? Thanks in Advance.
Answer by Hellium · Jan 31, 2019 at 09:15 AM
The definition of UnityAction
is the following:
public delegate void UnityAction();
As you can see, it's a simple delegate with no argument, and returning void
.
A UnityEvent
is just another way to handle events with the (great) possibility to be serialized so as to be used in the Editor (Inspector) in contrary to plain C# events
. A simple example is the onClick
event of a Button
.
Here are some interesting resources
Don't the events need some sort of delegate type to be initialized? Like so:
public delegate void UnBlock();
public static event UnBlock UnBlockInitialised;
Don't UnityEvents need that?
No, because UnityEvents
are not handled the same ways as plain C# events
. Internally, UnityEvents
is "just" a class handling an array of UnityAction
.