Events or UnityEvents?????????

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ม.ค. 2025

ความคิดเห็น • 336

  • @christianconnolly1296
    @christianconnolly1296 4 ปีที่แล้ว +105

    What have you named the cactus behind you?

    • @Unity3dCollege
      @Unity3dCollege  4 ปีที่แล้ว +57

      Wow... now it needs a name.
      It has xmas 🎄 lights on it... anyone have suggestions?

    • @christianconnolly1296
      @christianconnolly1296 4 ปีที่แล้ว +15

      @@Unity3dCollege Pokey Joe?

    • @ccristian1715
      @ccristian1715 4 ปีที่แล้ว +84

      @@Unity3dCollege GameObject :D.

    • @alexanderlogunov5147
      @alexanderlogunov5147 4 ปีที่แล้ว +5

      @@Unity3dCollege newgo I guess
      (from new game object)

    • @vlad1m1rk0rnev
      @vlad1m1rk0rnev 4 ปีที่แล้ว +9

      @@Unity3dCollege Coollider? :)

  • @RenegadeVile
    @RenegadeVile ปีที่แล้ว +3

    Common C# convention is to use EventHandler or EventHandler as your delegate, instead of Action. Both work, of course.

    • @biskitpagla
      @biskitpagla ปีที่แล้ว

      C# is way too general-purpose for opposing conventions to come to an agreement. In case of Unity, almost everyone and their moms are using Action over EventHandler unless they have reasons not to. Nonetheless, it makes more sense to set conventions project-wise and have simple guidelines language-wise.

    • @RenegadeVile
      @RenegadeVile ปีที่แล้ว

      @@biskitpagla This must be a personal experience-type situation then, because I've seen EventHandler used far more than Action personally and in code snippets/articles online. I do agree it's just something you and your team need to decide on for consistency's sake.

  • @thewhiterabbit4268
    @thewhiterabbit4268 4 ปีที่แล้ว +22

    We usually use C# events when designers are not supposed to touch or change things (C# events is more natural for our coders workflow and keep it scale-able easy), when ever there's a script where 3D-artist / Designers need to handle their stuff we go for UnityEvents, you teach them quick how it works with +/- and in which order it executes, saves time for our programmers.

  • @CamoflaugeDinosaue
    @CamoflaugeDinosaue 4 ปีที่แล้ว +27

    Always answering the good questions the most people don't even know to ask.

  • @nonyabusiness-f9e
    @nonyabusiness-f9e 11 หลายเดือนก่อน +1

    i recently learned how to use the new input system with the input action call back (which sends out a unity event) and i love it. i can definitely see how using that same system for other things would be super handy.

  • @unitywithzaher1374
    @unitywithzaher1374 4 ปีที่แล้ว +7

    The context menu is the most grateful thing in this video man
    it could save you a lot of time!
    thanks for this valuable information

    • @nikodemos71
      @nikodemos71 3 ปีที่แล้ว +1

      Completely agree, I was mind blown seeing, I knew the "dumb" way of dragging everything to your list had to have an optimization, just didn't knew what it was xD

  • @vlad1m1rk0rnev
    @vlad1m1rk0rnev 4 ปีที่แล้ว +20

    Great topic. Myself use System event all the time and UnityEvent when it comes to UI - all the UI elements normally have a lot of predefined events and event system itself goes very smoothly with UI.

  • @Stevedeniese
    @Stevedeniese 4 ปีที่แล้ว +2

    This is literally the video I've been looking for for the last couple weeks. Perfect explanation between the use cases, did not realise you could pass parameters in a delegate, so that completely changes my understanding on how delegates work for the better!

  • @ZackDaly
    @ZackDaly 3 ปีที่แล้ว

    Jason! Thank you for another absolutely stellar tutorial. The education you've given me for free on TH-cam is something I will always be grateful for. I hope someday I'll be in the financial spot to pay you back but, for the moment, thank you very, very much from the bottom of my poor heart. Haha. THANK YOU, SIR!

  • @HadiLePanda
    @HadiLePanda 4 ปีที่แล้ว +3

    Really love the improvement on the tutorial presentation, really clear and visual now with the images & rectangles!
    The concrete examples also make it easy to understand and visualise the knowledge.
    Good job and I'm really excited for future tutorials of this type :)

  • @bahtiyarozdere9303
    @bahtiyarozdere9303 4 ปีที่แล้ว +2

    That Context Menu Help was amazing. Thanks a lot!

  • @abnejne4737
    @abnejne4737 4 ปีที่แล้ว

    I have been working with Unity for a couple of years now. And been working on a serious project for about a year, but it's like I still learn something new every time I see one of your videos.

  • @chazlewis8114
    @chazlewis8114 3 ปีที่แล้ว

    Omg, all your vids have been so helpful but this one is an absolute godsend for me. You deserve a medal dude. Thank you so much!

  • @sergiorodrigoroyo5079
    @sergiorodrigoroyo5079 4 ปีที่แล้ว

    This video was really high quality in all aspects. Crystal clear explanation with good editing to highlight how the code relates to the editor. Fairly clean code that reflects the fact that you've done business software for a while. I think you talk a bit too much on some other videos I've seen, meaning that you could be a bit more concise. But this video was amazing. You've got a like and a subscribe :) Keep up the great work.

  • @LukeClemens
    @LukeClemens 4 ปีที่แล้ว +9

    Great video! You should also mention that Unity events are 38x SLOWER than C# events. That fact is no big deal for most things that events are used for, but if you're going to be firing events rapidly in an update or something, at 38x factor could certainly be noticeable.

    • @AvoidingCrisis
      @AvoidingCrisis 2 ปีที่แล้ว

      Hi Luke! Do you know where I can check the performance difference? Is it in documentation or an empirical test?
      Thank you very much for the help

  • @johnhauge2178
    @johnhauge2178 4 ปีที่แล้ว +3

    I am building an interactive book for a theater, and it's littered with custom events, considering I didn't even know about events, this just made my life so much easier!

  • @SunnyValleyStudio
    @SunnyValleyStudio 4 ปีที่แล้ว +1

    That is a great tutorial! One thing I wish it had was the difference between "event Action" and "Action" delegate. From what I understand the difference is the "event Action" protects us from setting the OnPickup = null and prevents other classes from calling on it ".Invoke()". In any case great tutorial! I have learner a loot! Thank you for making it.

    • @MasterofGalaxies4628
      @MasterofGalaxies4628 3 ปีที่แล้ว +1

      As I understand it, the "event" keyword lets you register multiple functions to whatever delegate you're declaring so that they will all run when the delegate is invoked. If you don't have that keyword, I think you'll only ever be able to register one function to a delegate at a time (though I'm not quite sure I have that completely right).

  • @MattRaffel
    @MattRaffel 4 ปีที่แล้ว +1

    I wish more people make videos like you did. This was will organized. Audio was good and clear. Thank you.

  • @aaronroot2172
    @aaronroot2172 4 ปีที่แล้ว +7

    I’ve only used unity events before this video. I’m pretty code savvy so I’ll probably be using “regular” events in the future, as it sounds better. Also that data context thing you had seems super nice and very helpful (the code part at least)

  • @WilliamB383
    @WilliamB383 4 ปีที่แล้ว

    Great Stuff Jason!
    There's a lot to unpack here but it was all presented clearly and simply. You nailed the difference between C# events and Unity events for me. You also provided answers to questions I did not even know to ask.
    Thank You

  • @Lonegamemaster
    @Lonegamemaster 3 ปีที่แล้ว

    After searching half the day this video is what finally helped me solve my problem. Thank you!

  • @Chronomatrix
    @Chronomatrix 3 ปีที่แล้ว

    Recently learned how to implement an Event Bus Pattern and I'm in love! What a beautiful thing! I don't have to worry about dependencies anymore.

  • @aoberthuer
    @aoberthuer 4 ปีที่แล้ว

    These videos are so helpful! There are so many ways to achieve things, it is always difficult to decide which approach to use. Mostly so, because it is difficult to find clear advice on when to use which approach and which not. Great!

  •  4 ปีที่แล้ว +1

    I think a nice addition to this comparison would be UniRx too, great content :)

  • @studentofthecraft
    @studentofthecraft 4 ปีที่แล้ว

    This is why I recommend your channel to everyone in my office. Great stuff !!

  • @somewhatjp
    @somewhatjp 4 ปีที่แล้ว

    i never used UnityEvents but this was super cool to see a simple example that uses BOTH (since i feel like ive seen so many examples of just explaining one or the other)
    made total sense, thanks :)

  • @infokubarcade4510
    @infokubarcade4510 4 ปีที่แล้ว

    Very nice things. I completely forgot UnityEvent exists, bc i didn't have the needs but it's very nice.
    I understood in your other answers why you didn't unsubscribe, so ok.
    The final trick is amazing, i never use ContextMenu stuff, really useful !
    So yeah, i learn't quite a few things here ! And i'm super glad you use the things you said in your previous videos !! The list serialization and the $ quote ! Amazing !

  • @mudumbiarun2926
    @mudumbiarun2926 4 ปีที่แล้ว

    Hello you are a pure teacher just like my dad he teaches Botany mostly Plants Science and you teaches Computer Science Nice to have a teacher like you Got to know about how to trigger Unity Events

  • @halivudestevez2
    @halivudestevez2 2 ปีที่แล้ว

    I was coming for the idea. Before the video as a c# developer, would do most of the events with c# events, except visual events, or gameobject related events. Now lets see the video.

  • @thepolyglotprogrammer
    @thepolyglotprogrammer 2 ปีที่แล้ว

    I love the tips about enhancing developer experience and editor improvements. Good job buddy!

  • @biskitpagla
    @biskitpagla ปีที่แล้ว +3

    For people having issues because of the light theme: try to keep a light source behind your monitor and always follow the rule of "light themes in lightened rooms and dark themes when in dark rooms".
    I know this is super-obvious but way too many people ignore these for whatever reason and have absurd setups that maximize eye strain.

    • @0xF81
      @0xF81 ปีที่แล้ว +2

      Weird, I'm using black themes everywhere even though my room is always lightened

  • @explosivekeeper
    @explosivekeeper 6 หลายเดือนก่อน +1

    You are really good at explaining thing. Thanx!

  • @jarvisx86
    @jarvisx86 4 ปีที่แล้ว

    Hey Jason, thanks so much for these videos. I'm super green when it comes to Unity so your knowledge is very much appreciated!

  • @JayadevHaddadi
    @JayadevHaddadi 3 ปีที่แล้ว

    I love that you shared the code for this! Thanks Jason:)

  • @brownkathy2158
    @brownkathy2158 4 ปีที่แล้ว +14

    I use static events(Actions) in the code to decouple the ui from the respective classes, for example an OnScoreChanged event that will notify the UI text, or OnHealthChanged for the health bar. I UnityEvents for things that I want a game designer to change easily
    and also when I am creating menus for the game. This way I can say that when you click 'Options' you disable 'MainMenu' and enable 'Options' game object.

  • @CarstenGermer
    @CarstenGermer 4 ปีที่แล้ว +2

    I usually start weighing the decision for Unity and regular events by 'Does level building need to change what goes on here?' If not, regular C#. That in mind, C# events make it easier to add similarly behaving types of objects later. Plus, for bigger projects or projects that may grow more complex in the future it's cleaner and very convenient (later) to have one general or multiple specialized 'event handlers', which can be neatly done with C# events. You can bind events via an event handler easily to Unity objects or UI elements using the editor and with some experience you will create a mix'n match between the two that has projects easy and clean to extend in code or levels.

  • @PhoenixTheGame
    @PhoenixTheGame 2 ปีที่แล้ว +2

    Basically
    Delegate events:
    listeners are hooked up at run-time and the other classes has limited access to the listener list and cannot invoke the event or remove all listeners so that they are protected from sabotage by other classes
    Unity events - listeners hooked up at design-time or run-time, other classes have somewhat limited access to the listeners list and can invoke the event and remove all listeners
    Performance Comparsion
    For 10,000,000 Invoked Events
    Turns out that Delegate Events are 38x times faster than Unity Events
    Personally i recommend delegate events for organized structure & better performance

  • @Luciphear
    @Luciphear 4 ปีที่แล้ว

    Thank you for this video! Been toying around with collectibles and this definitely opens up a lot of ideas of how I'd like to set up a few things in my project.

  • @williamleebuckley
    @williamleebuckley 4 ปีที่แล้ว +2

    One thing I've run into, not so much a problem but an FYI. If I rename a UnityEvent in code, it clears my editor assignments to that event. Done that during a few refactors where I didn't remember what had exactly been assigned. Version Control makes it possible to double check, but I definitely find that to be a weakness of editor-assignment vs code, for what it's worth.

    • @Unity3dCollege
      @Unity3dCollege  4 ปีที่แล้ว

      this is one of the biggest issues I run into as well :)

    • @kordeyrow
      @kordeyrow 4 ปีที่แล้ว +1

      You can use the attribute "[FormerlySerializedAs]" to prevent that.
      blogs.unity3d.com/2015/02/03/renaming-serialized-fields/

    • @williamleebuckley
      @williamleebuckley 4 ปีที่แล้ว +1

      @@kordeyrow Good tip, didn't know that. Don't know that my OCD can abide that, but still really good to know.

    • @kordeyrow
      @kordeyrow 4 ปีที่แล้ว

      @@williamleebuckley I dont know if I undertood right what you meant. After renaming the UnityEvent you can remove the "[FormerlySerializedAs]" atribute.

  • @aiartsev
    @aiartsev 4 ปีที่แล้ว

    I think we could've used a discussion on performance and architectural considerations for UnityEvents vs Events. In my team we've moved away from UnityEvents for everything non-UI in order to keep our business layer better separated. Also I think we could benefit from a discussion on the differences of declaring Actiton versus event Action as both are technically possible to use.

  • @capdim4739
    @capdim4739 4 ปีที่แล้ว +1

    I use them pretty much exactly as you describe. 95% of the time I use c# events and only use unity events for very specific one off things that happen in a particular scene so you dont have to clutter code for little edge cases. My only problem with unity events is its easy to lose the serialization on the call backs and that there is no compiler feedback, so I use custom attributes on anything invoked by a unityEvent and a little hacky editor script that lets me see all of those things, as well as logging out everytime a unity event is called and logging out the method name. Useful for when you come back to a scene after weeks and unexplainable things are being called lol

  • @Braneloc
    @Braneloc 4 ปีที่แล้ว +2

    First ! Usually use normal events, but I've been programming for a very, very long time.
    Nice to see some more uses for unity events :)

    • @Unity3dCollege
      @Unity3dCollege  4 ปีที่แล้ว +1

      Same here, i always have to remember the good times to use unityevents :)

  • @Ivan_Helsing
    @Ivan_Helsing 2 ปีที่แล้ว +1

    Hello. Wery useful explanation. Thank you for that. You realy make good quality explanation video.
    It help me with many thinks.

  • @MasterofGalaxies4628
    @MasterofGalaxies4628 2 ปีที่แล้ว +2

    One blend of both events I hit upon not that long ago is wrapping a UnityEvent with a C# event like this:
    [SerializeField]
    private UnityEvent _someEvent;
    public event UnityAction OnSomeEvent
    {
    add => _someEvent.AddListener(value);
    remove => _someEvent.RemoveListener(value);
    }
    The specific implementation I used this for was a character prefab death event, so that stuff like Animator and ParticleSystem manipulation could be set up in the Inspector easily while also allowing the decoupling, strong typing, compile-time safety, and most of the other benefits of a C# event for anything external that might need to care about an event you could probably otherwise write as a stock C# event.
    I do want to emphasize that this was for a PREFAB, i.e. something that probably needs to be built with independence of any specific Scene, as opposed to a level-specific event like in this video. Honestly, though, I feel this particular setup, so long as it isn't overused and abused, is a pretty strong and durable architecture for marrying the benefits of both event types.

    • @ironbard4901
      @ironbard4901 ปีที่แล้ว

      Beautifully written and thought out reply : D

  • @pt8306
    @pt8306 3 ปีที่แล้ว +1

    I'm the same. Most of my objects have some sort of C# event on them, and this is great for doing things like telling all objects in a raycast to execute their "Hit" function (although this doesn't need events, delegates or interfaces will do fine), or for subscribing to events that need to be watched closely (such as an inventory UI needing to update itself whenever the OnItemAdded event is triggered by a linked inventory). Overall, C# events are my go-to rather than UnityEvents, and are great for controlling functionality between 2 items where the link between them is known at creation time (like two objects that will ALWAYS interact with each other). Another thing you might want to do is wrap a C# event inside a ScriptableObject, as a "Event Object", and let your objects take them as parameters. This lets your artists do things like use the asset menu to create a "GameOver" event, then you can have your GameManager cann that GameOver event (referenced in the inspector), and then other objects that are completely unrelated can simply take an Event in the inspector to listen to it, which gives good-quality event control similar to C# events, but allows you to use the inspector.

  • @Javilingys
    @Javilingys 4 ปีที่แล้ว +26

    Code from this guy is very cool .. LINQ, private + [SerializeField], Lambda, Hard Consctrctions, etc. It's so cool ) Very hard for me, cause i'm a beginner (maybe), but i wanna learn intermediate or advance style of coding. Other youtubers show simple code with bad practices =((

    • @travlake
      @travlake 4 ปีที่แล้ว +10

      Pro tip: try using Rider instead of Visual Studio. It basically teaches you to do all this stuff by suggesting re-writes to your code. Jason has a video recommending it

    • @milhouse8166
      @milhouse8166 4 ปีที่แล้ว +1

      @@travlake I found this comment helpful. Thanks

    • @nerdin8or
      @nerdin8or 4 ปีที่แล้ว +3

      Agreed. TH-camrs and Udemy instructors will often just show quick & dirty code that does the thing in the video or course title. I am loving how well Jason teaches us to write clean, scalable code.

    • @CSNomad
      @CSNomad 4 ปีที่แล้ว +5

      CodeMonkey does a pretty job of covering intermediate/advance code stuff.

    • @Songfugel
      @Songfugel 3 ปีที่แล้ว +5

      @@CSNomad Not really, he kind of expects you to know it already since he trailblazes thorough everything so fast without explaining half of the things he should have. His content is amazing, but not beginner friendly at all
      You really notice it if you go over the same content from Jason or Brackey, you realize how much important stuff CM skips in his videos, because they are so obvious to him

  • @samiatailia3394
    @samiatailia3394 4 ปีที่แล้ว

    Thanks, This quick cours helped me a lot. Looking for more new lessons.

  • @epsil2511
    @epsil2511 4 ปีที่แล้ว

    usefull, clear, short, just perfect as usual!

  • @12apidxHDxGamerx
    @12apidxHDxGamerx 4 ปีที่แล้ว +1

    Hi Jason and/or Jason’s community.
    I’ve recently decided I’m going to get serious about learning game development and I’m going to start with trying Unity.
    Literally all I know about code is the small random bits I’ve picked up from watching random videos from this channel. I really appreciate your videos, man.
    My question is, are there some more specific videos here you would suggest a complete and total beginner to watch to get started?
    Especially interested in any videos that cover fundamentals and good practices to have from the start. Thank you so much!!

    • @12apidxHDxGamerx
      @12apidxHDxGamerx 4 ปีที่แล้ว

      Okay...well. My first step should have been checking the play list and seeing “beginner? Start here” LOL
      I swear I always forget TH-cam playlists are a thing for some reason.
      Still would appreciate any recommendations though, thanks all!!

  • @BenByford
    @BenByford 3 ปีที่แล้ว +1

    still no idea, seems like you could just hook up the referrences in code without using events at all? whats the strong case for using events?

  • @Elandir
    @Elandir 4 ปีที่แล้ว

    Nicely exposed Jason.
    I make extensive use of the two kind of events and I think there are two thinks worth of mention:
    First, regarding the 'references flow', when using Unity events, the object triggering the event is the one with references to the object with the handler methods while when usind C# events the object with the handler method is the one which must be aware of the object triggering the event when subscribing (true, you can make c# style subcriptions with unity events but for me, if you are going to do that, then you should be using c# events from the beginning). This is interesting as allow you to approach the relations and 'object-to-object awareness' in two opposite ways.
    Second, in my experience c# events are far more easy to debug. Is easier to check who is subscribed to who and trace back what is happening. In the other hand, Unity Events are faster and more simple to set up (again, only considering Unity events set up from the editor as they don't make much sense when used as c# style).
    Usually my rule of thumb is 'if events are going to be static, meaning the suscription is performed in editor time and not going to change at runtime Unity events are usually the way to go; in the other hand, if I'm going to do/undo suscriptions at runtime then go for c# events'

  • @alexchacon778
    @alexchacon778 4 ปีที่แล้ว +4

    "Work Hard and Be Nice to People"
    What a great slogan.

    • @Unity3dCollege
      @Unity3dCollege  4 ปีที่แล้ว +2

      Ya when I saw it i had to grab that thing :) great daily reminder :)

    • @frankzander5946
      @frankzander5946 4 ปีที่แล้ว

      @@Unity3dCollege Yeah I like that moantra too: shortened it for me to "work hard, be nice" ...easier to say to myself ;-)

    • @frankzander5946
      @frankzander5946 4 ปีที่แล้ว

      mantra i.e.

  • @ShawnBlais
    @ShawnBlais 4 ปีที่แล้ว +121

    I think this misses the main reason you use UnityEvents vs Events which is an architectural one regarding coupling. UnityEvents (that are configured in the GUI) reach outwards into the app and execute methods, and are tightly bound to the scene graph. Events dispatch things blindly, and someone from above must subscribe. This is the first fundamental difference. The second is that UnityEvents force you into the Gui, which is more suited to non-coders.
    So basically for me, I see UnityEvents as generally an architectural anti-pattern, but recognize that for beginners, or non-programmers, they are might still be the best approach. As a developer myself though, I literally never use them, too hard to maintain, and you leave your compiler in the dark... ie I can't trace back to who turned something off, or called some method, cause the call binding is buried in some component somewhere. If you set up too many of these "game objects calling business logic" things in your scene, you project will turn into spaghetti really quick.

    • @uweeby69
      @uweeby69 4 ปีที่แล้ว +11

      Unityevents don't force you into the gui. I use them from code only often.

    • @ShawnBlais
      @ShawnBlais 4 ปีที่แล้ว +1

      @Jacob Ya that definitely is not so bad, as at least it's usually a local connection, like the child is directly connecting to parent. Doing that's in the Scene Gui is not going to bite you too often, though still pretty easy for that link to get broken, and you don't notice. If you wire up a listener via code (button.onClick.addListener), you'll usually get a runtime error if anything is wrong, which is preferable to the silent failure of some fxn call just not being made. So I prefer to just grab a reference to the btn in my view controller and add the listener:
      * Will throw RTE is btn is missing/unlinked
      * No hidden dependencies/connections that are not visible to compiler
      * Compiler can always trace back the caller of some important fxn (ending up in your view controller, and not some generic btn)
      * Less fiddling with massive dropdowns and finnicky gui
      * More consistent. When you have to use UnityEvents, they act like your other Events in the app. Don't need to wonder if something is mapped in Scene or Code, it's always Code.

    • @ShawnBlais
      @ShawnBlais 4 ปีที่แล้ว +2

      @@uweeby69 Ya that's a good pt, they don't force you into it, more like they have full GUI support which is better for those non-technical users, where Events can only be used through code.

    • @muratcanagic
      @muratcanagic 4 ปีที่แล้ว +1

      @@ShawnBlais so you say i am a master coder and screw you. Unity Events offers both and Events are for only coding, so Events are better?

    • @Norivee
      @Norivee 4 ปีที่แล้ว +4

      @@muratcanagic Unity Events are better in every way except for performance basically. So if you call an event pretty often, you'd probably want to use an Action event.

  • @andersmartensson8659
    @andersmartensson8659 4 ปีที่แล้ว +3

    Used a lot of UnitEvents with scriptable objects (like in the famous video describing the method), but transferred to mostly C# events. It became a mess with many scriptable objects, and what you pointed out, when changing or editing in the Editor, such connections can be forgotten. With the switch to C# events, you have it all in code, don't have to remember setting it up in the editor again. However, it seems like I'm moving towards some "C# event manager", like Game Manager, but for events, with public callable methods, so other classes can invoke the events. Don't know if this is a good way forward though.

  • @phillipeaugusto1637
    @phillipeaugusto1637 4 ปีที่แล้ว

    Thanks for this video Jason.

  • @manuelmanteconpolo6776
    @manuelmanteconpolo6776 4 ปีที่แล้ว

    This kind of videos are really useful thank you so much

  • @GameDevCoffeeBreak
    @GameDevCoffeeBreak 4 ปีที่แล้ว

    awesome video, thanks for this Jason!

  • @mikenspired
    @mikenspired 4 ปีที่แล้ว +3

    Most Important Questions for "When to use Unity Events vs UnityActions or delegates"
    Is there a possibility your designer or customer ever wants to trigger something when the event occurs, then use a UnityEvent.
    Do you want this event to be HIDDEN from the designer or customer? then use a C# event or UnityAction.
    I also keep reading comments saying "Unity Events force coupling in the GUI, or force you to use the gui". You can add listeners via code!
    I don't feel like Jason did a good job explaining this. I use UnityEvents almost entirely for the "Designer or Customer", but I never use them my self in GUI and instead subscribe to them just like you would a C# event via code.

  • @axux4509
    @axux4509 4 ปีที่แล้ว +2

    Hello, I have a question for experienced people here. Is adding event listeners to too many game objects on a scene a good choice? Can it reduce frame performance? Let's say we have a puzzle game where every piece has an instance of a class that listens for game events and calls its own functions and behaves accordingly. Is this a good design pattern? (1 event causes n objects to react vs. 1 controller controls n objects.) C# event system makes things really easier and quicker to implement this way. Or should we code a single controller to control multiple objects in such scenario?
    I also wonder the interior working system of events in C#. Do they listen for invokes in a loop(which would impact performance), or do they have a special system that links all the listeners to the event and only calls them when needed? (like a linker)

    • @Unity3dCollege
      @Unity3dCollege  4 ปีที่แล้ว +1

      Having the listeners won't be an issue. Only adding and removing them is slow, bit invoking them is really quick, almost like a simple method call.

    • @axux4509
      @axux4509 4 ปีที่แล้ว +1

      @@Unity3dCollege Thank you for your answer.

  • @trinosan
    @trinosan 4 ปีที่แล้ว +5

    Reminder that it's good practice to unsubscribe from events when destroyed or disabled
    Also, in some cases you could have a public *static* event on those collectibles, so you only need to subscribe once

    • @Unity3dCollege
      @Unity3dCollege  4 ปีที่แล้ว +4

      Unsubscribing is definitely something I shoulda covered :)
      Making them static may cause an issue though if we wanted to have multiple collectors collecting different things.
      But if that's not the scenario then there's no reason it couldn't be a single static subscription instead of all of them in a loop, great point :)

    • @frankzander5946
      @frankzander5946 4 ปีที่แล้ว +1

      @@Unity3dCollege I always make sure that the subscibe stmt is adjacent to the unsubscribe to make it easy to track if I missed some anywhere. something like this:
      // delegate to collect everything we need to de - register on Destroy...
      private delegate void Dreg();
      private Dreg dreg;
      private void Awake()
      {
      // register for the event and create an entry in the de-register list
      //for later execution
      AttackSubEvents.NewThustTarget += SetEngineThrust;
      dreg += () => { AttackSubEvents.NewThustTarget -= SetEngineThrust; };
      }
      void OnDestroy() { dreg(); }

  • @VioletteValkyria
    @VioletteValkyria 4 ปีที่แล้ว

    Another distinction is performances. I found a blog the other day that did a test case of both event types and UnityEvent have a much larger overhead! That said, you shouldn't have thousands of events running in loops, so you would probably only notice a difference if your game is already very tight!

  • @willpetillo1189
    @willpetillo1189 4 ปีที่แล้ว

    When thinking about what type of architecture to use, I find it helpful to think about the relationship between the objects along two considerations (1) is the connection a fundamental dependency between the objects (coin always affects text or text always listens to coins) or incidental to the context (a design choice for this scene, maybe different later)? For brevity, I call the former "code-based" and the latter "scene-based". (2) the direction of dependency relative to the direction of the flow of logic. That is, if A happens then B happens: is A dependent on B, B dependent on A, or A and B both dependent on C (which also knows about A and B)? Combine these considerations and you have a 2D graph. Code-based, A -> B dependency = your standard Component.Method() call. Code-based, B -> A dependency = C# Events. Scene-based, A -> B dependency = UnityEvents. Scene-based B-> dependency = those ScriptableObject events that Ryan Hipple talked about in that Unite Austin 2017 talk (or something similar). Code-based, A, B -> C = statics. Scene-based A, B -> C = Singleton. Personally, I find A -> B dependencies much easier to follow (since it matches the flow of how things happen) and use that pattern by default unless there is a compelling reason to reverse it. Also, I prefer creating reusable tools over project specific code whenever possible and making connections on the scene level, rather than hard-wiring them into the code, lends itself better to tool development. Between these two preferences, I use UnityEvents a lot. There are downsides, but most of those are fairly easy to overcome: ScriptableObjects for non-serializable static arguments, a helper script with serializable classes for all the commonly used dynamic arguments, and then writing custom scripts with standard, code-based dependencies when UnityEvent lists start getting long and including lots of related items.

  • @Rizzan8
    @Rizzan8 4 ปีที่แล้ว

    When it comes to C# events, according to the standard we should be using event EventHandler / event EventHandler, not event Action / event Action.
    I haven't heard about UnityEvents before, but I doubt I will use them. I dislike referencing stuff in the inspector. I have PTSD because pretty often my Unity loses those references at random which causes multiple random NullReferenceExceptions.

    • @Unity3dCollege
      @Unity3dCollege  4 ปีที่แล้ว

      If ita for an external audience or library id go with the event handler, but for internal projects I really prefer the action syntax to avoid extra code. Undertand the view completely tho :)

  • @simoncodrington
    @simoncodrington 4 ปีที่แล้ว

    Great explanation mate.

  • @Lyserg1260
    @Lyserg1260 4 ปีที่แล้ว +1

    I mostly use UnityEvents for simple UI actions like buttons or OnPointerEnter trying to not subscribe more than one method and mostly those that come from the same object like closing a popup window or highlighting an object.
    For actions related to game mechanics i prefer regular C# events as they are much easier to debug and trace in code. You can easily find all the event references in your code.

  • @halivudestevez2
    @halivudestevez2 2 ปีที่แล้ว

    that context menu thing is great! Before this I always did Editor scripts for the sake of 1 button for the inspector....

    • @halivudestevez2
      @halivudestevez2 2 ปีที่แล้ว

      ( that is an event, too. an Editor event on the Component inspector ;) )

  • @sergiorodrigoroyo5079
    @sergiorodrigoroyo5079 4 ปีที่แล้ว +1

    One thing I found about UnityEvents is that private variables on the callee don't seem to change at all. So if object A exposes an event and object B's method is the method called when that event happens, if this method changes any of B's private variables, that doesn't work and variables seem to have their default values. I might try this again, but this happened to me a few months ago. Is this normal? I wonder whether I did something wrong, but maybe this is a case for C# events?

  • @Mrmder
    @Mrmder 4 ปีที่แล้ว

    Not all heroes wear capes, but I really think you should consider wearing one Jason.

  • @Ryan-ww7un
    @Ryan-ww7un 3 ปีที่แล้ว

    Why don't you need to pass the collectible when you register HandlePickup in the foreach loop?

  • @longuemire748
    @longuemire748 3 ปีที่แล้ว

    Thank you,
    I have a question about unityEvent.
    I don't understand how to remove a listener once it has been added from the inspector. I keep a trace of the gameObject function in a UnityAction but when I remove it, nothing happens in the inspector. Is this normal?

  • @olegmagarill1320
    @olegmagarill1320 4 ปีที่แล้ว

    Thank you for this tutorial, I really like what are you doing, you helped me alot!)

  • @paulkohler8868
    @paulkohler8868 4 ปีที่แล้ว

    I use C# Events a lot for modular sorts of subscriber patterns. Very recently, I'm exploring adding UnityEvent fields (with very specific T0, T1, etc) to some scriptable objects to see if it's a viable way to expose ability scripting to a designer.

  • @DungeonNumber5
    @DungeonNumber5 4 ปีที่แล้ว +2

    I developed a preference of handling any type of events without UnityEvents.
    Reason: persistent callbacks on UnityEvents are by definition not managed from code, thus they are the potential source of *weird behaviour* and they may straight up break the abstractions (when object A totally should not care about object B, but _some programmer_ linked them directly via UnityEvent).
    If anything is controlled by persistent callbacks on UnityEvents, then for the sake of clarity and integrity of your code it should have been controlled by regular callbacks.

    • @Unity3dCollege
      @Unity3dCollege  4 ปีที่แล้ว +1

      I did the same for quite a while. But for some game types, UnityEvents seem to make a lot of sense (especially when working w/ designers who aren't programmers)

  • @crinklecutchipscringe9167
    @crinklecutchipscringe9167 3 ปีที่แล้ว +4

    To be honest, I keep forgetting how to use events so that's why I'm here xD

  • @Impossible_Object
    @Impossible_Object 2 ปีที่แล้ว

    This was super helpful. thanks!

  • @Urasawa92
    @Urasawa92 4 ปีที่แล้ว

    8:38 - that syntax doesn't always work perfectly fine! It is not multithread safe, as some other thread might remove the last element after we checked for null. Always use the ? operator

  • @yudanaim6849
    @yudanaim6849 4 ปีที่แล้ว

    Hi can you save us from the json serializer of unity. UnityWebRequest.
    I have data.json file and webgl don't reads it but inside the editor it works fine.
    Please help.
    I tried many things and nothing seems to work for me.
    Is there a simple way to get a data.json file for webgl?

  • @summerWTFE
    @summerWTFE 4 ปีที่แล้ว +2

    Love the Thumbnail 😁

  • @poudelbinay6587
    @poudelbinay6587 3 ปีที่แล้ว

    Whats the difference between using ,
    1 public event Action someEvent; and
    2 public Action someEvent ?

  • @alperenbayraktar397
    @alperenbayraktar397 4 ปีที่แล้ว

    What about sending information over events? Which one do you prefer? I could not manage to do it with UnityEvents.

  • @skizooooooooo
    @skizooooooooo 4 ปีที่แล้ว

    Great editing!

  • @afriendlyfox
    @afriendlyfox 5 หลายเดือนก่อน

    I would love to use C# events, but to subscribe to a C# event on some kind of object you need a reference to it, and I don't really want to depend on the GameManager in every single script that needs to know about GameLose event.. decoupling things is the very reason I want to use events. What is a clean solution to this?
    I am thinking of using a ScriptableObject as a mediator between them and depending on that instead, which could work and would make it easier to test the reaction to the event on every object separately or make prefabs that don't search for or depend on anything scene-specific, but I have no idea if calling events from them is any less performant.

  • @Okapy5689
    @Okapy5689 4 ปีที่แล้ว +1

    Hello, will you do please some deep tutorial on multithreading in unity (c# jobs system) or multiplayer (photon unity networking)?

  • @MaZyYTube
    @MaZyYTube 4 ปีที่แล้ว

    Ohh funny. I just made german version of this same topic but not uploaded yet. I also talk about "Why to use unityevents, (static) events" + scriptableobjects to avoid singletons.
    Good video btw.

  • @nikunjgoyal9461
    @nikunjgoyal9461 4 ปีที่แล้ว +1

    Hey,plz can you tell how to create portal in your game so that player can go from one world to another world(while both world will be different and full of enemies and both worlds are in same scene.Also,for FPS games.)
    Plz tell

  • @manzell
    @manzell 3 ปีที่แล้ว

    Late question - I use UnityEvents but have stopped using the inspector to hook it up, instead using the AddListener calls. Is there any reason to use C# events in that case?

  • @franm.3099
    @franm.3099 4 ปีที่แล้ว +1

    Why do you use private [SerializeField] instead of declaring the variable as public?

    • @admadea
      @admadea 4 ปีที่แล้ว +3

      Prevents the variable from being accessed by external scripts. Helpful for reducing spaghetti code, as well as preventing a few other issues.

    • @Unity3dCollege
      @Unity3dCollege  4 ปีที่แล้ว +2

      Exactly that reason, it prevents accidental misuse. (by myself or others... and trust me, i've misused public vars before :)

  • @NaejDoree
    @NaejDoree 4 ปีที่แล้ว

    you can also hook up stuff in code for unityEvents too with AddListener/RemoveListener (and that's why I only use unityEvents) it's less pretty than having the custom opertaor but it allows to combine both wolrds

  • @alexanderlogunov5147
    @alexanderlogunov5147 4 ปีที่แล้ว +1

    I prefer to use default c# events or custom observable pattern.
    I would like to use Unity Events, when I need to delegate job to junior developers, because they are much easier to understand and as far as I know they work in main thread when default events are asynchronous

  • @darkmaigo
    @darkmaigo ปีที่แล้ว

    Both are great I can't decide on one .. I bet I'll use them both

  • @quinnjackson1113
    @quinnjackson1113 4 ปีที่แล้ว

    I have been using a lot of ScriptableObject events for the past year, but all 3 types (unity, c# and SO) are useful in different situations.

  • @lvladimirov
    @lvladimirov 2 ปีที่แล้ว

    Cool video. I was wondering if you have tested the performance between these two cases:
    1. Have an event that will alert its listeners to turn on and off their Renderers for example.
    2. Instead of triggering an event, simply activate/deactivate a GameObject and have the "listeners" monitor the state of the GO in LateUpdate so that they know when to change the state of the renderers.
    I dont know how events are structured, but listeners sure must check agains something in order to monitor for events, so at best it should be the same, but more likely a boolean check should be better than an event. (even when its not actually a boolean var but a state of a GO). What are your thoughts?

  • @robosergTV
    @robosergTV 4 ปีที่แล้ว

    If you already use references to obj to register events it makes no sense NOT to directly accessing the data then, no need for events, if you have a direct reference

  • @עידובלייכר
    @עידובלייכר 4 ปีที่แล้ว

    Jason my dream to get comment from you, you are the best!

  • @AlexTheProMacGamer
    @AlexTheProMacGamer 4 ปีที่แล้ว +1

    I kind of agree with Shawn Blais's comment, UnityEvents doesn't really convince me neither. It seems that UnityEvents turn the observer pattern upside down, because the observable (the game object that contains the UnityEvents) is now directly aware of its listeners, and the whole point of calling something an "event" is precisely to decouple the observer from the observable.

  • @leventalpsal
    @leventalpsal 4 ปีที่แล้ว

    You can use the Unity Events like normal events, registering to the Unity Events from the code, without defining actions on the inspector. So in that case, what is the advantage of standart events vs Unity events? What about invoking an Unity Event from another class, do you think that's a good idea when needed? Do you think this is an advantage over normal events?

    • @GabrielSantos-io5qx
      @GabrielSantos-io5qx 4 ปีที่แล้ว

      Do you have any particular scenario in mind where you would actually need to invoke an event on another class?

  • @muzboz
    @muzboz 4 ปีที่แล้ว

    Awesome, thanks Jason! :D

  • @TheBeardedDoog
    @TheBeardedDoog 4 ปีที่แล้ว

    Suggestion: Show the difference between dynamic input and static input to unity event. Hint: You can use dynamic input to prevent having multiple methods or relying on object references. Simplest example, wire a Toggle.OnValueChanged -> gameObject. setActive using dynamic bool

  • @urzytkownikYT
    @urzytkownikYT 4 ปีที่แล้ว

    Well that can be answered quickly. If you're beginner or not a programmer you use UnityEvents. Whenever else c# events.

  • @modelworkzseo
    @modelworkzseo 2 ปีที่แล้ว +1

    Here's the gem summarized: 11:37