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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when you're building unity games should you use events or unity events that's a question that i get all the time and that's what i'm going to answer today we're going to go over what events are what unity events are what the differences are why you might want to use one in place of the other or where you could use both i'm going to give you some simple concrete examples of how you can use them in a game to make really cool things happen and show you the code behind it and then make that available for download as well before i get started though if you have a preference if you really like using just regular events or you prefer unity events drop that in the comments below i'm curious to know what people are using before they get started and see if this video maybe changes anybody's mind also if you don't mind please hit that like button and the subscribe it really helps i appreciate it a lot or just drop in a comment and say hello to demonstrate the difference between unity events and events i've set up a simple example scene where i've got a character that is a collector and wants me to go collect red gems and as i pick up red gems they will send an event back to him that'll do some updates to the screen let's watch and see what happens so i go pick up a gym and a little bit of text appears and updates pick up another gym and it keeps updating and if i get that yellow one i should expect it to not update because it says pick up the red gems no update and i get these last two red ones and he is jumping in for joy and the spikes beyond him are clear so i can run over here and go do something maybe go pick up this giant star that i can't actually pick up so let's talk about how this is set up where we use events in a setup like this versus unity events and why we would really want to use unity events here and not just use regular c-sharp events so let's stop and take a look at how this all actually works here you can see we've got our scene set up with a player a little camera set up using cinemachine and a target group camera to follow him and then we've got a collector object and the collector object has a sprite renderer for his little alien sprite but then i've got this collector script on it and you'll see that it's got an on complete event this is our unity event we'll dive into it in a little bit and then it's got this list of gatherables let's open up the script and take a look at it so here it is in its entirety you can see that it's relatively short there's not a lot to it and some of it down at the bottom is actually just editor stuff to help us out with setting things up so let's go through what this class is doing how it's using events and how it's using unity events how that differs and how we hook it all together so i'm going to zoom in a little bit here and take a look first at the couple serialized fields we have one for our text that's for that text that's saying how many to update get three more get two more get one more we have a list of gatherables and if i go back into unity you notice that that's this list right here or this array of gatherable objects it's actually a list because i recently learned that you can serialize lists and arrays the same so now i just go back and forth for a little while as i try to cement that into my brain so we'll open up the script one more time and look down here at the next field it's this on completed event and i'd mentioned that that is our unity event that's this thing right here on complete and what this is is really an area where i can define a list of actions or things that i want to happen when my oncomplete occurs so whenever i invoke the oncomplete event then all of the things that i've added in this list here will run and i can add more things by hitting the plus or i could hit the minus and just remove them in fact let's clear them all out and go through the process of adding them for anybody who's not familiar with how to add things to a unity event so here it says list is empty and i can hit plus and the first thing that i need to do is assign an object so on the left side you'll see that it says run time only there's also an editor and runtime option i always use runtime only and then there's a field for an object we're going to go select the spikes so if i find my spikes i can click it and just drop it right in here once i do that all of the components that are on the spikes will show up here in this list so right now i've got five because if i go select my spikes i've got five components there's transform sprite render kill on enter polygon collider and game object they're all gonna have game objects so that's the fifth one you see four but there's five because of the game object so go click on it again and game object was the option that we had checked and i just check set active which takes in a boolean parameter so it takes a true or false the default is false which means that it sets the spike to not active then it added another event handler right here with that plus and then we need to assign something to it and in this case i was updating my what was it the remaining text or no the pop-up text so the pop-up text right there and we're going to set the text by just selecting text mesh pro you guy and i believe there's a set text or a text one just have to find it there we go text and then we set the value to well hey thanks again exclamation mark and then the final thing that i was doing was changing his sprite renderer and i don't want to do that i'm going to change it up i'm going to make it so that this gem turns on instead so instead of the sprite renderer changing he'll make this gem appear and just start falling down so i'm going to turn the gem off i've moved it into that position go select my collector and then i'm gonna hit plus add one more event assign that gem yellow select the game object one again and set active and set that to true now it's also worth noting that i could call my own events or my own methods not events really but my own methods on my classes or my scripts if i wanted to here i'm really just setting text and setting properties or setting an active state but there's nothing that prevents me from invoking a method that takes in a parameter with a number like add points and passing in a value of 5 if it takes in a number parameter so here we go i've got my updated version i'm going to hit play just watch it happen real quick so that we see the gem fall down you can see how it kind of works there we go oh beautiful and then we're going to go back into the code let's look at how we actually invoke this event now and then dive into what regular events are and why we might want to use a regular event versus a unity event so let's take a look at the code again remember we have our text and we have our list of collectibles it's currently named gatherables these are all of the things that we need to go gather and then we have a list of collectible named collectables i think i actually want to rename this so i'm going to hit ctrl r and rename this to collectables remaining this is really the collectibles that i haven't gathered yet so in our on enable what we're doing is turning this list into a new list that's a copy of our gatherables really what we're saying is copy this list of gatherable objects and we're going to remove from this list as we go along and once that list is empty we're going to fire off our event so we're creating a copy of this list so we can just remove things from it then we do the magic of events here on line 21 and 22. so we loop through all of our collectibles remaining we use the for each loop which is going to give us an instance or a reference to each of the collectibles in the collectibles remaining list and then we register for the on pickup event by using the plus equals syntax and assigning it to handle pickup what this is is registering for an event with a callback and i'm going to hit f12 to go look at that on pickup event of the collectible remember this is of this collectible class right here so i hit f12 and here you can see my event which is set up quite a bit different from a unity event it says public event action has a type of collectible and it's just named on pickup there's no unity event there's no serialized field we have this event keyword that's a little bit different and the action and this parameter type a lot going on here so what's really happening here is we're setting up this event that we can pass back in a collectible two and you're going to see that here on line 13. first let's take a look at the ontrigger enter 2d so what do we do in here is just check to see if we have a player if a player is the thing that walked into us so if we get a player component then we call the on pickup method by using the question mark dot invoke and then passing in this now what we're actually doing here is invoking this method and passing in our self as a reference parameter or a parameter so that when we need to remove ourself we'll have a reference to it you'll see that in just a moment but the question mark dot part is important because normally or in the old days what we would have to do is check for null references on an event we don't have to do this with a unity event so we would do something like this if onpickup is not equal to null then we would say onpickup and pass in this now that syntax works perfectly fine but the shorthand for it now just allows us to do it with the question mark dot and invoke this will do the exact same thing it'll call that on pickup event but it will only do it if there is something registered and it's not null again a unity event won't be null so you don't need the question mark dot it won't hurt but it won't do anything extra the last thing that we do is just set the collectible to not active and this this is what's happening on our gems our gems get set to not active every time we pick them up so let's go back to the registration here we register for our event and we register with this handler the handle pickup method and you'll see that right below notice that the parameter here matches the parameter in our event if i add another parameter to this method like int number we're gonna have a compiler error in fact it should pop up any second there we go saying that the overload doesn't match the delegate the delegate takes an action of collectible and my method takes a collectible and a number so i need to remove that parameter and then let's look at what happens when onpickup is called our handle pickup will run it'll pass in this collectible which is the object that's being picked up and then we remove it from the collectibles remaining list so we start off with what it is eight and we go down to seven things then we call update text i'm going to scroll down here and just look at update text update text just says text.settext and we count the number of things remaining in our list so we've removed them we went from eight to seven we say hey set text to seven more and then we turn the text off if we get down to zero things so if we have zero things or we have the full count of things available in our list so we haven't picked up anything then we turn the text off we just turn it on when we have some number of remaining things more and we've picked something up at least now let's go back to the unity event because this is where it kind of all ties together so finally after we pick the item up if we're out of items we've picked up our last one we call the on completed event by using on complete event dot invoke again we don't need the question mark because the unity events won't be null themselves just their registered callbacks could be empty so that's really all there is to it that's all you need to make this happen and it's very easy to kind of extend this and start building out more things if i wanted to have something that fires off like on the first pickup i could have a thing that says like if collectibles remaining that count is equal to the gatherables.count minus one then on picked up first event dot invoke and i could set up a unity event like that that just fires off when i pick up the first item and i can do the same in just about any scenario and hopefully you can start to see places where we could set this up just fire off things that we can hook up in game and add some cool customization and cool level design with all right so hopefully you've gotten the idea that you really should be using both of these in most projects you should use unity events for things where you want to do stuff that's specific with the scene or one-off events or one-off types of things that you're going to be doing in your game that are really specific to scenes or specific to a single object where you don't want to write code for it or you don't need to necessarily write code to go toggle an object on and off or change the text of an object you're going to do once and you want to use regular events or c-sharp events or delegates when you're doing things that you don't want to have to go hook up in the editor you don't want to have to go sign this gem or assign this collector to every single gem and say hey jim go do something to this collector you want to be able to say hey go get all of the collectible things and just register for an event on them and do it that way so that i don't have to go in and set things up i don't have to go find objects i don't have to go deal with these references and manage this stuff it's very easy for this stuff to break if you have a lot of references in unity events to control your game it's super simple for things to accidentally get renamed or deleted and recreated and break everything so i like to use unity events when we're doing something in the scene something custom something that's a one-off and regular c-sharp events for things that are working in code where i want one piece of code or one object to listen for a state change on another object and i want that to be the norm now i want to finish this up with this little context menu part at the bottom because anybody that saw it might be curious what is it how does it work why is that even there so here you'll see that we have a method called autofill collectibles that sets up the gatherables by finding all of the objects of type collectable so it finds everything that's a collectible in our scene and then it uses a link statement to filter them by name to only get down to the ones that have the word red in them i use the two lower in case i've put uppercase reds in their names and then we call to list to convert this collection or queryable into a list so that we can assign it to gatherables and if i go into unity you can see how that works so here i've got my list of gatherables i could zero this out clear it out completely right click and um oh not right click there right click on the collector and hit auto fill collectibles and it's going to find all of the red ones if i go rename this gem yellow to gem yellow red and do it again let's try it select my collector right click auto fill collectibles see that it doesn't find it but if i turn it on bam now he finds it that's because it doesn't find objective type doesn't find things that are turned off just kind of wanted to show that too so that's all that is and it's just a simple little trick to be able to find all of the things we could put in some search filters some sorting another really cool and easy way to do this by the way so that we don't even have to have this list of collectibles or gatherables is to just make these all children of the guy so i could select all of these gems in fact i could take this whole gem section and just make it children of the collector and then i could just use the get components in children so i could change it to be get components and children and now would find the hidden objects or the disabled objects and i wouldn't necessarily need to have that reference oh gotta add the s there i wouldn't need to have the references to the collectibles being serialized or the gatherables this could be private and i wouldn't even need to go assign it i could just do that in on enable instead of down in this method so i could clean it up and shorten it up even more but i want to show that you don't necessarily have to have the things be related to be hooked up with events they could be totally unrelated and just have some other reference to it all right that finishes this video hopefully this was helpful and you've kind of got a better idea of when to use events and unity events if you've changed your mind about what to use please drop a comment below also if you have tips or ideas suggestions on um other ways to use unity events and events or just thoughts on the subject let me know and i want to say special thanks to everybody on patreon and all my subscribers and youtube people you guys are awesome i think that's it bye
Info
Channel: Jason Weimann
Views: 52,689
Rating: undefined out of 5
Keywords: unity events, unity event system, c# event, unity event, game development, unity tutorial, c# delegate, unity 2d, unity 2d tutorial, unity events and delegates, unity action, unity events tutorial, unity3d events, c# events, unity tutorial for beginners, game, brackeys, tutorial, unity, c#, game development for beginners, programming, game dev, code, gamedev
Id: oc3sQamIh-Q
Channel Id: undefined
Length: 15min 43sec (943 seconds)
Published: Sun Sep 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.