Search Unity

Whats is the difference between UnityEvent and UnityAction ?

Discussion in 'Scripting' started by bali33, Apr 27, 2015.

Thread Status:
Not open for further replies.
  1. bali33

    bali33

    Joined:
    Aug 14, 2011
    Posts:
    232
    Hi guys,

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

    Thank you
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    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.
     
  3. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    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.
     
    akipox38 likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That's a bad thing? o_O
     
    Michael_Berna likes this.
  5. Deleted User

    Deleted User

    Guest

    Can you explain why you think so?
     
    Michael_Berna likes this.
  6. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    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.
     
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    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.
     
  8. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    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.
     
  9. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    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./
     
  10. bali33

    bali33

    Joined:
    Aug 14, 2011
    Posts:
    232
    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
     
  11. Gburdeti

    Gburdeti

    Joined:
    Dec 23, 2015
    Posts:
    4
    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.
     
  12. benzsuankularb

    benzsuankularb

    Joined:
    Apr 10, 2013
    Posts:
    132
    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.
     
  13. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Don't argue with three year old posts.
     
  14. skullthug

    skullthug

    Joined:
    Oct 16, 2011
    Posts:
    202
    Unless if it's every three years
     
  15. tetto_green

    tetto_green

    Joined:
    Dec 22, 2015
    Posts:
    35
    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.
     
  16. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    SubZeroGaming was last seen:

    Oct 25, 2016
     
    oNetopereko, Aethermol and Bunny83 like this.
Thread Status:
Not open for further replies.