ScriptableObject Events In Unity (C# Tutorial) | Unity Scriptable Objects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Even I picked it up recently. It for sure has reduced coupling.

I would like to know how you are using SO for sound. For my project I was trying to avoid singletons but my Sound Manager is a singleton.
I created Sound Clip SO which holds information like audio clip, volume, loop, etc. Then my Sound Manager holds a list of Sound Clip SO and adds Audio Source for each of them which plays and stops according to the raised Game Event.
Perhaps, would like to know your approach.

Also, near the end of the video saw same method being called for different Game Events. Won't it be easier to manage, if you simply add a list of Game Events rather than just holding a single Game Event? That way you can drag all your Game Events which will call the same method. Avoiding the need of adding a new Game Event Listener.

👍︎︎ 2 👤︎︎ u/gamesbyBAE 📅︎︎ May 07 2020 🗫︎ replies
Captions
Hey all and welcome to Thousand Ant where we give you unity and game dev tutorials from an experienced indie game developer perspective. I'm Matt. And in this tutorial, we're going to take a look at how to use scriptable objects with events. Let's check it out. Talk about events for a second. So I'm, I don't know. I don't know if I should admit this on the internet, but it actually. Kind of a long time to understand them and understand how to use them. And. It really was only fairly recently that I kind of got comfortable with working with them in my own stuff. And I think obviously I probably still have a lot to learn in this area, but basically what an event is, if you're not familiar is it's a kind of a signal that can be raised up in your project that other objects can listen to and receive a notification from. So it's almost like a broadcast system. Like somebody comes on the loudspeaker. Okay. All passengers come to the check-in desk. All the passengers can hear that and respond. The loudspeaker doesn't actually need to know. Are there 10 passengers is the 11th passenger just added, you know, do we need to add them to the list or whatever? No. They just say all passengers who are interested in getting on the plane, come to the checkout desk. Right. And then everybody can, can respond to that. And theoretically, some of them could do different things based on that. Right. It's not. Come to the desk method. It's just saying now is the time for something to happen? You know, maybe somebody could say, oh shoot, I forgot my lunch. And now's the time for me to run and try to get a sandwich before I get on there. Right. That's an interesting example. And so. The thing that I wanted to try in my untitled cute space game, which I've been doing some dev logs for on the channel is using an event architecture that uses scriptable objects. You can do events just in pure C sharp, right? This is. A unity concept or a game dev concept, but in unity, it's possible to implement events with a kind of a drag and drop interface in the inspector using scriptable objects. And this is based on the talk by Ryan Hippel from unite I'll link the video down below. It's like. Treasure trove of ideas. I think that's pretty influential. And so I wanted to try out the event architecture that he kind of proposed there. And so that's what I've done here. And actually the code is literally just his code taken from his GitHub, which I can also link. So if we go into the unity project here, let's start by looking at this game event class. So here again, this is yet from the unite 2017 talk, all credit due to the author. And so basically what we're doing is we've got the create asset menu attribute. This just means we can create instances of this as an asset in our project file. Right. I really like this approach because it allows me to plug things in, in the inspector. For some reason, that kind of is the way my brain likes to work. Obviously, some people who are more kind of pure code driven might not want to do all that dragging and dropping. So this is. Kind of seasoned to taste matter of architectural aesthetics. Right. But for me, I really liked this drag and drop kind of style. And I think maybe for people who are. Super programmers. Maybe it's nicer to create your connections that way. I don't know, but so basically what we have here is the ability to create an asset. This inherits from the script of block object class, and then we have a list of private list of event listeners. And when we register a listener, we will add an event listener to that list. So we're building up a list of everybody who may want to respond to this event. And we're going to register and unregister them traditionally, you're going to do that in on enable and on disabled. It's important to manage the life cycle of events, right? You don't want to leave an event hanging out after an object has been destroyed, for example. So what we're going to do is make sure that we have the ability to register and unregistered listeners. We're going to pass in a reference to the listener in order to do that. And then we have this public method called res. This is saying, start the broadcast, raise the event, tell everybody that this event is occurring and this is just. Use a four loop to loop over the event, listeners and tell all of them call the on event, raised method from those listeners. So if we take a look at that game event, listener class, we have a couple of tool tips here, event to register. The response to invoke when the event is raised, which is a unity event, right? The unity event allows us to define a lot of behavior from the inspector as well. So this is kind of a nice inspector focused approach, combining scriptable object assets, and then unity events, which allow you to drag and drop and call functions from the inspector. There's some limitations to this. You can only pass in one or two arguments, and some people may not prefer to wire things up that way. But for me, I find it quite nice. Right. Here in on enable. We're going to register the listener. As I mentioned, and the non disabled we're going to unregister the listener. So those on enabling non-disabled are going to be called whenever the object is enabled and if it's disabled or destroyed, and then on event rates. We're calling response dot, invoke. This is our chance to call some code and do whatever we want via the unity event in the inspector. So let's take a look at how this actually works. So in my space game, let's say for example, on our asteroid in space here, let's go into the scene view so we can see it. Here's our asteroid. So our asteroid has the capacity to be destroyed. Now, what I've done is I've created this asteroid exploded asset. Now notice this is just a, basically a blank asset. It doesn't have any fields on it. We're not going to define anything here. We're just going to say, raise this event when this is exploded. So I've created in my asteroid resource script here. There's a public game event, field called asteroid explode and I drag and drop a reference to asteroid explode. And then in my explode asteroid method here, I just call. Asteroid explode dot res. And then I deactivate the game object. So in this method, now I'm raising that asteroid exploded event. Now, what do we do with that? Well, we can do anything with that. Right? We could respond to that in any number of ways. In this case, the response is really simple. I think this is a no. Use for this kind of event. So I have, uh, audio player and I have an asteroid explosion separate. This is a game object with an audio source and a scriptable sound player. I'll actually talk about how I did that another time. And then here we have the game event listener mano behavior. Component attached to this. It has a reference to the asteroid exploded asset. So we drag in the asteroid exploded asset to here to the event. And now when that event is raised, it's going to call whatever. Fine in the response here. So in this case, I'm calling scriptable sound player.play sound. So I'm just playing a sound when the asteroid explodes, just like a sound. I mean, we can see that in action. It's not super exciting. So here we have our little cute mama. And then if we go to space view, we can click on the Astro, which is going to tell the, a little robots to start mining. Um, they're doing a great job there. And if we, uh, go to main diorama and go to the mining scene, click on the asteroid, we can see the resources are going down. It's about to explode. Might be super long. Complete boom. So once that asteroid resource is depleted, it's going to raise the event and that sound is going to be played, or there's a lot of different things that we can do with this. I'm using this to play all the sounds. So when you click on the UI, when you click on things in space, when mining is complete, cargo is delivered, we're raising all of those sounds via events. It's pretty easy to set up. It makes sense, right. That I might want to play an explosion sound, but I also might want to update the UI or, or do something different. And so saying, okay. Yep. An asteroid is exploded. Let's let's do something about that. I feel like is nice. So as you can see, right, I've used this to wire up. All of my UI sounds. We also have an event that's raised when the drones dock with the base, we have an event that's raised when, or I put in like a debug event. Feel that I could fire off to fill up their cargo holds for testing. When the button is pressed. If we look at the UI real quick, we see that we have game event listeners to activate and deactivate all the UI, right? So when the button is pressed, we just raise the event that can then play the sound, deactivate, the UI. And of course you could couple these directly, but I like this, right? Because now everything kind of stays not so tightly coupled. And there's like this little layer of decoupling. And non direct connection between the, the parts of the UI. So we'd just say, yep. The home button was pressed. Do whatever you need to do in the home button is pressed. We could reset the diorama's scene for example, or reset the cameras or whatever we want to do. Right. We can do multiple things based on that event. I'm still early in this, so I haven't hit any of the different complexities and limitations that I might hit with this. I'm really just testing it out. But honestly, as a first. Quick test. I really enjoy working with it. So I recommend that you check it out. I'll put a link to the, get hub with the code from Ryan hippo down in the description. You can grab that and give it a try. All right. So hopefully you guys found that interesting and useful. If you did, please consider subscribing and turning on notifications for the channel. So you can be updated when new videos like this come out. Drop me a comment down below. Have you tried implementing events in your own project? What do you think of this architecture? Have you tried the scriptable object approach? Have you done it with just regular C sharp events? Let me know what you think. I'm always interested to hear you. Guys' feedback. I'll also put some other videos up here for you guys to check out for more content from the channel as always. I appreciate you guys spending a little bit of time with me and thanks so much for watching. I'll see you next time.
Info
Channel: Thousand Ant
Views: 11,179
Rating: undefined out of 5
Keywords: thousand ant, matt mirrorfish, sam eng, unity, unity3d, tutorial, game development, how to, dev, program, programming, code, coding, c#, develop, easy, beginner, scriptableobject event system, scriptableobject events, unity scriptableobject event, unity scriptableobject event system, event, scriptableobject, ryan hipple, richard fine, scriptable cookbook, c# tutorial, unity3d scriptableobject, unity scriptableobject, unity scriptable objects, scriptableobject architecture, unity tutorial
Id: gXD2z_kkAXs
Channel Id: undefined
Length: 11min 16sec (676 seconds)
Published: Thu May 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.