Hello there.
So in this video, we actually going to make the menu work and we're going to begin by creating C-sharp
script.
I'm going to call it UI mane and let's take canvas and drag it over here.
All right.
Now, let's take a look at this.
This is the parent that contains menu items as it's children.
Right?
So this is the child of the canvas.
This is the child of the canvas.
And this is the child of the canvas.
And from the parent script, we can control those objects.
Let's go inside.
I'm going to close all but this.
Let's delete update for now and delete this green line and don't like it.
And in the start we're going to make for a loop that's going to act as long as this canvas has some
children in it.
So let's do.
Transform dot child count.
Right.
And inside, just for example, let's try to switch off all of the children.
Let's do this transform, get child with an eye.
So it's going to cycle from zero to the last one.
And do game object set active, false.
Let's see what it gives us.
Let's go back to Unity.
Let's switch on these and then press the play button.
Look, all of them were switched off because we cycled through them with the help of for loop, and
then we switched off one by one.
What if after this one we choose to switch on the one we like?
For example, let's do this serialized field private game object.
Let's say, settings.
All right.
And then in here, after for loop, we're going to say settings.
Not set active.
True.
Let's go and try that.
In the unit.
He lets select canvas and drag settings to the settings field.
Now let's go and try this.
Let's hit play.
And you can see everything was switched off and settings were switched on.
And basically, this is the entire code we're going to use to switch between menus.
Let's go and modify it by just a bit.
Let's select all of these code in the start press alt, enter and choose extract method.
I'm going to name it as switch menu two.
We can delete it from the start.
We don't need it for now.
And here instead of private, I want to make it public and in the parentheses I want to pass a game
object.
So let's do this game object UI menu.
The UI menu we want to switch on and instead of settings here we're going to do UI menu set active.
True.
We can delete the serialized field.
We don't need it now.
And now let's go to the unit and see how we can use this function.
I'm going to switch off all of the elements in.
Except the main menu.
I need main menu, and then I want to go inside the main menu, find my shop button, and it has a component
button.
You might see it before.
You might know how to use it, or you might just seen it in the previous video.
Me putting it on the game object.
What we need to do here and this shop button is already a working button.
It's clickable and you can add a function on click to this button.
Let's try and do this.
Let's go here and click plus.
Then we can drag an object here and then choose a function from that object.
We have a UI main component on it, and then we're going to go and find switch menu to function.
Now, as a parameter to pass, we need to give it a game object.
And because this is a show button, we can give it a shop UI game object.
Now let's go.
Not too far.
Find the shop UI.
Open it and find clothes button.
And here we can hit plus drag the canvas and then find the UI switch menu to.
And we're going to choose Main Menu.
Now let's go to play mode and see what it gives us.
I can go to shop and I can press close button and go back to Main menu.
Pretty cool, right?
Pretty simple.
Now, let's do this for other stuff.
Let's find settings button plus canvas.
Switch to find settings UI we prepared.
Where is it?
Here.
I'm going to draw the main menu on the top.
That's what I want.
What else we have here.
We have mute button, but we're going to take care of that later.
When we say turn up the volume and sounds and so on.
So let's just close this for now.
Top to start, we also going to set up in next video.
Let's go to settings find close button, set it up with a canvas.
UI switch tool and we're going to switch to Main menu.
We're not going to do the same for Main menu in the past UI.
It's going to have a bit different functionality.
All right.
So let's leave it for now.
And in the shop we took care of close button.
So basically this is done for now.
Let's go and overview the code just to avoid any confusion.
So we added a button and this button has on click event and on click we added a function from canvas
that is named a switch menu tool, which is exactly this function over here.
Then this function requires a game object inside it.
And we passing a game object by dragging this object to this field over here.
So then when this function called, we have a for loop that cycles through all of the children under
the canvas and switches them off.
And after that it takes the game object we chose.
We passed inside and sets it to active.
And by that we switch in between menus.
Very cool, right?
Yeah, I hope so.
Okay, let's go to the next video.