Whats is the difference between UnityEvent and UnityAction ?

Hi guys,

I was wondering what is the difference between UnityEvent and UnityAction ?

Thank you

A unity event is a event like a C# event, the UnityAction is more or less a delegate for unity. They are used together you can use a UnityAction to define what a UnityEvent accepts.

I’d say 100% of the time, don’t use UnityEvents. Use standard Delegates and Events, or use an event action. (using System.)

UnityEvents give you the added ability to add events through the inspector which in my personal experience leads to bad practices.

1 Like

That’s a bad thing? :eyes:

1 Like

Can you explain why you think so?

1 Like

I can see reason for the UnityEvents system, but primarily, it’s best practice to stick to the C# events. I can only ever imagine one case use of using unity events, and that’s for the UI buttons, which I rarely use the unity events for, I still end up registering the events through code with a lambda expression.

At the end of the day, it’s a Unity thing…not a C# thing. It’s a C# thing under neath, but if you ever do anything outside Unity stuff, that UnityEvent is useless. You shouldn’t get lazy with assigning events through the inspector. You should be properly declaring them through script.

so why is that bad, its a tool to keep in the tool belt. Since if you subcribe to events in code, that is done per class for the most part. But if done via the inspector, it can be done per object, and there are a lot cases where you may want multuiple objects of 1 type subcribed to differnt events.

9 Likes

It’s bad. lol.

You shouldn’t be able to serialize events like that. If you’re gonna use UnityEvents, at least understand what a delegate and event actually is, so if you encounter a situation where you need to subscribe events through code, you know how.

You still arent answering the question like i said, it isnt black and white, both delgate and event in c# as well as UnityEvent and UnityAction got there purposes. I use them in both ways for many of my projects. Its just know what is the right tool for hte right job.

There are a lot cases where it makes sense to assign in the inspector and searlize and also lots of cases where it makes sense to just do it via code in the class./

Hi,

So basically UnityEvent does not bring nothing more that the couple Delegate/Action of C#, is that right ? What’s is C# events ?

Thank you

It all depends what you want to do and who plugs in the actions/delegates.

Sometimes, when you work in a team and an artist wants to change some ui parameters through an event it may be good to expose it. It doesn’t mean you cannot attach a delegate to an UnityEvent through code. This dual way of doing things protects your actions from being detached from the inspector because you can attach to the event programmatically but it also enables an artist to work with how a button will look when pressed.

Not always. You can manage to hide complexity in code by using inspectors.
For example, ViewControllers don’t need know anything about view logic (How view animate etc.)
This make ViewController focus only on mapping view and business logic and inspector help for visual events.

5 Likes

Don’t argue with three year old posts.

12 Likes

Unless if it’s every three years

10 Likes

Guys, we should continue this talk XD

Subzero, I think you should never use standard Delegates and Events. It’s a very bad practice. They make your code less readable and make your project less friendly for non-developers.

SubZeroGaming was last seen:

Oct 25, 2016

3 Likes