 Howdy. Welcome back. So we are going to focus on the UI system logic now. So we're actually going to get into some coding in this particular lecture. So let's move on and jump into Unity and start to get our code all set up for the UI system. Okay, so now we're back here in Unity and we've got our UI system script attached to our UI group. Alright, so the reason why I do that is because it is going to control everything that is UI. So canvas, screens, maybe the event system. For now we're just going to focus on creating a reusable system that switches screens appropriately for us and also fades and also plays animation and also fires an event when anything, any sort of screen switch happens. So let's actually go and take a look at the UI system and get things rolling. Okay, so the first thing I want to do is set up a private variable. Okay, so this private variable is going to store all of the screens that are inside of this particular UI system. And the way that we're going to do this is we're going to look for a specific type of component that is sitting on a game object in our scene. Okay, so this specific type of component is going to be a IP UI screen class. That's the type. Okay, and it is a component because it derives from mono to mono behavior. Alright, so this private component, it's an array because we have lots of screens is going to be called screens and it equals a new component array and initialize to nothing. That's why we put the zero in there. So basically then when we start this particular game object, the UI system, okay, so when the game starts or this level starts, the start function is going to be fired. And what we want to do is we want to get all the screens. We want to try to find every screen that is below or parented underneath. Okay, so let's go back to you here. So parented underneath the UI system. So what we're going to do is we're going to say screens is equal to get components in children. Okay, so we're going to say get components in children. And we're going to say IP UI screen. Alright, so perfect. And we could actually make this public so that we can see if it's working. Save that. And let's see what we get. So I'm going to select the UI system over here and let's hit play. And you can see that we only get one screen. That's weird. Okay. Well, the get components in children method actually takes a argument that says include inactive. So I'm going to set that true because I actually accidentally or I didn't accidentally, but this happens. I had turned off this screen. Okay. And this makes it a lot easier for when designers are setting up the UI and they've left certain things off, you can automatically search for this. So I'm going to play. And now we get both screens. Perfect. And you'll notice that they're in the order that they are parented at that. That also comes in handy down the road. Okay. Perfect. So let's move on now that we have the screen. So we're getting the screen data here. Okay. So what we want to do is we want to be able to switch screens. We know that this UI systems core responsibility, its main function is to switch screens. And to do that, I'm going to create a method called public or that's public. And it's called switch screen. Okay. And we're going to take in a screen that we want to switch to. Okay. So this is the argument for this method. So I'm going to say IP UI screen and just give it a name of a screen. Awesome. Perfect. And the thing is before we roll into setting up the code for this, the nice thing about this particular system, it's all event based. We're not going to be actually updating anything. We're just going to be using events to pass information back and forth. Okay. We're just going to be telling these components what to do when they need to happen. We're not going to constantly be checking for state or anything like that inside of an update function. So again, it makes this a lot more flexible because it is literally just based off of states. Okay. Perfect. Okay. So the next step here, I need a couple more variables and I'm going to start typing these out. Okay. And what I'm going to do is I'm going to say, let's create another variable and this is going to be called the current screen. Okay. So I'm going to say private IP UI screen. Okay. And this is going to be called current screen and we're making it private because I don't want anyone else to, I don't want any other script to be able to set this. Okay. So I'm going to make it private, but I want to allow people to actually get the current screen from the UI system if they so choose if they just want to check that. So in order to do that, I'm going to create a C sharp property. So I'm going to say public IP UI screen. We're going to call this current screen and it's good practice. Whenever you're doing this, whenever you have a private variable, you keep the first letter lower case. Okay. And for a property, you make it uppercase and that's how really you start to understand that that's a property when you're looking through the intelligence. So I just want to get or retrieve the current screen. So I'm going to say return current screen like so. All right. So I am working on another course that will teach the entirety of C sharp for unity. So I'm not going to go into the details of what properties are. If you don't necessarily understand them, there is a lot of information out on the web. So what I'm going to do is move on and add another private variable. And this is going to be called the previous screen because I want to be able to store the previous screen that we were on. That's good information. Okay. And you can create a another public property. Okay. That is the previous screen just to give people access previous screen. All right. That's all good information that the UI system should be responsible for. Okay. We're just going to space these out so they're easy to read. All right. So with that, we are now ready. Okay. To stub in or actually fully flesh out the switch screens method. So the first thing I want to do is just check to make sure that we do in fact have a screen and this particular variable that is being passed into this method is not null. So I'm going to say if a screen and we can do that because money behavior inherits the I nullable or the, yeah, the nullable interface. Again, I will talk more about that in the C sharp course, but for now that is why you're able to do this. Okay. So then we're going to say, okay, so we're going to say if current screen does already exist. So if we have a screen already in this particular variable, then what we want to do is we want to say current screen.close. And now we don't actually have that set up already. So I'm just going to leave that stubbed in. I'm just going to comment it out and because we are going to stub in the code for this later on for the screen. All right. And then what I want to do is say that the previous screen is equal to the current screen. So we're saying when we come in here, if we do already have a screen that we're on, okay, we're going to close the screen and then we're going to set the previous screen to this current screen. And then we're going to move on to the next set of lines. So we're going to say then that the current screen is equal to the screen that we're passing in. So this particular variable right here is equal to a screen. So now we've basically changed positions. So what was currently in the current screen is now the previous screen. And the current screen is the screen that we're passing in. Okay. Awesome. So now we're going to say current screen. Whoops. So current screen dot game object dot set active to true. Just to make sure that it's on. Just in case. Right. And then we're going to say current screen dot start screen. And again, that is not implemented yet. So I'm just going to start that out. So basically we're saying, Hey, start your function or start your, your activity. Okay. And we're going to stub all that in, in the next lecture. So it'll make more sense once we get there. Okay. And then finally, what we're going to need to do is, is have an event. So now we, we want to allow other objects to receive an event that they want, we want them to listen for this call that says, Hey, we just switched the screen. Let's say we use this for firing off a piece of audio or something like that. Okay. This is super useful. So let me show you how this is done. So what I'm going to do is actually put in another using statement. And I'm going to say using unity engine dot events. Okay. I also don't need this to be public anymore because we know it works. So now what I'm going to do is set up that new event. So I'm going to first create a header here. And we're just going to call this the system events. All right. And then I'm going to create a new unity event. And we're going to call this on switched screen is equal to a new unity event. And what's going to happen is now anybody that is listening for this event. And we're going to show you here once I get the setup will receive that event and then perform their action. So it's a really slick way inside of unity to quickly set up script communication. Okay. So now down here in our switch screen method, what we're going to do is we're going to check first just to make sure that that on switch screen event is not null. So it actually has some listeners attached to it. And if it does, we're going to say on switch screen dot invoke, which means to fire it broadcast it yell, if you will, whatever, whatever you want, right. All right. So that means that yeah, if we have listeners attached to this event that we're going to broadcast out our, our event or we're going to say, Hey, we're fired. We fired our event basically. Okay. And that, believe it or not, excuse me, completes the switch screen method. Okay. The last couple of methods I want to implement here. Okay. What is the go to previous screen. So I want to be able to say go to previous screen. We'll set the back button up at that. So we'll say public void go to previous screen. And this one's really easy, which is awesome. You can see how simple the script is. And really, I, you know, when I was starting all this stuff out, I made complicated scripts. I set up complicated systems and stuff like that. And really after a while, I started realizing exactly what, you know, you needed to do like this, the simplicity of it also. That's what I'm, I'm trying to show here. So we're going to say, if we have a previous screen, first off, I want to make sure that we don't throw a null reference exception. If that's true, then we're going to switch the screen. No, we can reuse that function. And we can say go to the previous screen. And when that happens, we go up to switch screens and we set the current screen to the previous screen. So on and so forth. So now this is a very reusable method. Switch screens method. All right. So the last two methods I want to set up are for loading a scene from the UI system. And now we're not going to go through the actual loader script component in this particular code course. But I want to put this in here to show you that it is this UI system is also responsible for. And loading scenes as well. So what I'm going to do is create a coroutine as well. So I enumerator and we're going to say, wait to load scene. All right. And we're going to give it a scene index. Okay. Whoops. Perfect. We also need a scene index for this because we want to be able to pass in a value. Okay. So then all I need to do to get this to work is say start coroutine. And then we say wait to load scene. And we'll pass in the scene index that's being passed into this particular method, the load scene method. Perfect. All right. For now, what I'm going to do is just say yield return null just so we don't get any errors inside of unity. So with that, we've completed the bare bones at least portions of the UI system. This is actually a functional script. It will start to switch screens for us. We just need to fill out the UI screen. Okay. So let's just go back into unity and make sure it all compiles and we don't get any errors, which we shouldn't. Perfect. And now you'll notice that over here, we actually have this really cool setup for our event. And like I was saying, it allows us to add objects. So let's say I wanted to send an event to the main camera. And on the main camera, I wanted to maybe turn off the camera. That allows me to do that. So I could turn it off just like that without having to write any more code. Right. So when I switch screens, I could turn off cameras or fire an audio clip or start to play an audio clip. I mean, so we're fire off a particle effect. Super awesome. Super useful. So that is setting up the logic for the UI system. And in the next lecture, we are going to cover setting up the UI screen. Thanks so much.