Hello guys.
Welcome to the video.
In this one I want to show you how to do navigation in the menu with the help of keyboard or gamepad.
To do that, we need to go to the main menu.
And over here we have event system and we need to click replace with the input system UI input module.
So this one basically will use the default input actions that were made by unity.
You can check them by clicking here and then opening them in here.
And you can see it has navigation submit cancel all of that.
So this will be used.
And that is good.
Now we need to talk about navigation itself.
Actually navigation is already working.
It is built in functionality in unity.
But navigation does not know where to start.
You can see it says first selected none.
And if you don't know where to start, you don't know where to go, right?
Because you don't have the first point.
So to make it work properly, we just need to go ahead to the main menu, take one of the buttons and
drag them here to the first.
Select it and it will work.
And the problem now is that our buttons are transparent and we can't really see when it is selected.
So we'll have to go ahead and.
Select all of the buttons and let me rename this to back button.
I'm going to take it and paste it in here and in here also for previous skin and for the next skin.
Just so I know it's a button and for back button over here.
Now I'm going to select all of the buttons.
Uh, we can we can hold Ctrl and just click them like that.
And on level selection.
There is a back button.
Okay, first of all, if you click visualize over here, you can see how buttons are connected.
And now we're going to make it not transparent just like that.
And then I want to go to the button component and find normal color and make it transparent just like
that.
Then we need to find the selected color.
And we're going to change it to something noticeable.
Maybe red is fine.
Okay.
Now when button is normal, it's going to be just like that.
And when it's selected, it's going to be mix of this dark blue and red color.
Okay.
Uh.
Very good.
Uh, let's go ahead and try this.
Just make sure you have this.
Just make sure you have first selected in the event system over here.
Let's go to play mode.
And you can see new game is selected and I can use arrow keys or w s to switch between these buttons.
Also, I can connect my gamepad.
Give me a second.
Yeah, and I can use gamepad as well.
Very nice.
And the problem now is that if we go to another UI, for example, new game, you can see none of the
buttons are selected and we cannot navigate further.
So that's what we need to fix in this video.
And that's basically most of the job we need to do in this video.
So how are we going to do that.
We're going to go to difficulty.
UI is going to be our example.
Let's go to UI difficulty.
Let's make serialized field private GameObject first selected.
Then I want to make on enable.
And inside of on enable we're going to do event system current set selected GameObject first selected.
Now when we enable UI difficulty we're going to assign new first selected.
Let's go and do that.
Uh, we're going to give it normal mode button as a first selected.
And now if I go to play mode.
I can click enter and go here.
And I have easy, normal and hard and I can switch between buttons.
Very nice.
Very, very, very nice, I like it.
Uh, one thing I want to add here before we go further is to enable navigation in cycles.
Uh, at the moment, if we navigate in the menu, I can go all the way up or all the way down, but
I cannot, uh, you know, click down now to switch to continue.
And that's what I want to do.
So this is how I fix it.
We go to continue button.
And over here we have navigation automatic which works perfectly fine.
But also we can go and choose none.
And then explicit and explicit allows us to do manual setup of the UI.
And for the select app I'm going to choose settings or select down.
I'm going to choose New Game button.
And now it will work in cycles as well.
For new game button it works pretty fine.
We don't need to change anything for settings button.
Again, we can choose explicit and drag New Game on up and Continue button on down.
Perfect.
Now, if you go to play mode and try again, you can switch between buttons like that.
Isn't it cool?
I love it, man.
Now let's go ahead.
Disable main menu.
Enable difficulty UI.
And over here I want to do the same.
So I'm going to choose explicit for.
Easy button for the down selection.
Going to be normal mode for up selection.
Going to be back button.
And same goes for back button.
Explicit.
And then, uh, a button, hard down button.
Easy mode.
All right.
Now let's see this.
And there is one more fix we need to apply to the UI so I can cycle between buttons.
Very good.
Now I can go back and you can see none of the buttons are selected.
That happens because well we don't have the functionality for it.
We need to fix it.
Let's go to main menu and you can see the problem is this one does not have any script.
We have scripts over here on all of the elements, but for main menu it is on canvas.
So we need to just make a script.
I'm going to do UI.
Uh, update first selected.
Just like that.
Why not?
Let's go and take these lines of code.
I'm going to copy it and I'm going to go to this UI.
First select it.
Also let's move it to UI folder.
Yes.
It's okay.
Move it.
So we're going to open this script paste it in here.
And I want to remove difficulty manager.
And now it's supposed to be good.
Let's go back.
You could also save last selected if you like, but I think that's too much.
And let's go, let's go, let's go and let's.
Uh, just, uh.
Drug.
Maybe new game to the first selected.
Okay, now let's go and try again.
Works very well.
We go to new game, works very well.
Go back, works very well.
Now there is another issue.
If you click the mouse over here.
None will be selected anymore.
And then you would have to, you know, restart the game because it cannot be controlled with the keyboard
anymore.
How could we fix it?
Actually, it's pretty simple.
We just need to go to event system.
Over here and we need to click deselect on the background.
False.
And now when you, uh, go to play mode, you can switch button with the keyboard.
And if you click it with your mouse it will not be deselected, which is pretty much okay I guess.
By default we have this enabled.
So you have button deselected and there is a way of switching it back.
If you're using the keyboard like it's going to select something again.
And we could do it that way.
There are two options.
One of them would be to select button every time you put your mouse over the object.
Another option is to just select the last selected when you do a navigation with a keyboard again.
So what do we choose?
If you like to disable selection when you use your mouse and then enable selection when you use navigation,
then you would have to make an event and subscribe to navigation of the UI and use it when you use navigation.
Again, actually, maybe let's try that quickly because I'm curious, supposed to work.
Um, so what do we need to do?
We have this default input actions so we can use them.
We're just going to go to UI main menu.
Let's make private default input actions.
Default input.
Then we need to make a method.
I'm going to do private void update selected.
And by the way, maybe there is a better way.
Maybe there is some button I don't know about.
Let me know.
Okay.
Anyway, we're going to check if event system current.
Current selected GameObject is equals to null.
Then we need to do event system current set selected GameObject and we need to know what to select.
I think we're just going to make a public GameObject.
No, no that would be.
Let's not do it.
Let's make it a private GameObject last selected.
And then let's make the.
Uh.
Public void.
Update.
Last selected.
Just like that.
And over here we're going to do last select it equals to.
Game object new last selected.
We're going to pass it here new last selected.
And then over here we're going to give it last selected.
Now there is a way to update this.
When object is selected you could basically go to a button um like any button.
An update last selected there.
Let's subscribe to event first.
Then we're going to do it with a script on each button.
Yeah.
So we're going to do default input equals to new default input actions.
By the way I just got this idea.
So I'm not sure it's going to work but I really hope it will.
Then we're going to need on enable default input enable.
Then default input um ui navigation uh performed plus equals context return me update selected.
Just like that.
Let's check it with, uh, on enable.
And then if it works I'm going to do on disable.
Okay.
Now we need to update last selected.
We just need to call this method every time one of the buttons is selected.
And we can go ahead.
Make a script.
UI update last selected.
Update last selected.
Over here we're going to put comma and type I select handler.
Let's implement the interface.
And then inside of this interface we're going to do a very simple thing.
We're going to need component of UI main menu.
So I'm going to do private UI main menu main menu awake main menu equals to get component in parent
because it's somewhere in parent.
And then on select we're going to do main menu update last.
Select it to this game object just like that.
Now we have to put this script on each button that can be selected.
Let me expose the variable just so I'm sure it is working.
Oh, we cannot do it this way.
We need to expose it on the main menu.
I'm so sorry.
Let's do serialize field.
Now in unity I'm going to select back button.
Hard mode.
Normal mode.
Easy mode.
Settings new game and continue.
And let's drag update last selected here.
Now this one is supposed to update this variable over here.
Every time we select the button let's go to play mode I'm going to use my keyboard.
We updating the value here.
This is good.
Now if I use my mouse it was not selected because of this checkbox over here.
Let me try again.
I'm going to click deselect on the background.
And let's go.
So nothing works.
I mean, sorry, everything works.
Now I'm going to click.
My mouse object was deselected.
I can use my mouse for the input.
Then I can click the keyboard again and I can select the button.
Cool I love it.
I guess this is the perfect option, so I'm not going to change it.
I think I like it.
Yes, I like it.
Definitely.
Now, this one was not selected for some reason.
Wai wai.
Um.
Oh, this one does not have update first selected.
Okay.
Should be fine now.
New game.
Uh.
Go back.
New game.
Go back.
Yes.
Okay.
I love it so much.
Um, you've seen that if you don't click your mouse.
But if you just move it like that.
We do not deselect the object.
So that may be an issue.
And if you like to fix it, you can do that simply by going to this, uh, update last selected.
Let's make pointer enter ie.
Pointer.
Enter.
Let's implement interface.
And then over here we're going to do main menu.
No, sorry.
Event system.
Current.
Uh, current.
Selected game object.
Or actually, let's do set.
Selected game object.
Null.
Just like that.
And this will fix it.
Now you can navigate with mouse and keyboard and this time for testing I'm going to use gamepad.
Let me enable my gamepad.
Enable switch on I mean why so long is my Bluetooth?
I think my Bluetooth switched off okay there is a gamepad.
Now I'm going to go to play mode and we can navigate in the UI.
Very nice.
Now I'm going to use my mouse.
We deselecting the object.
Now we can use the mouse to navigate and I can use gamepad again to switch the object.
Nice.
A it's not working here.
Why?
It's not working here now.
I'm using gamepad.
Let me see.
I guess because last selected is from the previous menu.
Yeah yeah yeah.
That's the.
Okay.
So.
Something to do here.
Let's go to UI difficulty.
And on enable, we're going to use main menu as well just in case you're going to make private main
menu.
Main menu.
Let's open a wake.
Let's do main menu equals to get component in parent of main menu.
And on enable I'm going to do main menu update last selected to first select it just in case.
Wow.
I'm doing like, full mode debug, man.
I kind of like it.
Let's go now.
Let's go.
So.
He's in gamepad.
Then I'm using mouse to go to new game and normal is selected.
Okay.
Go back new game now only mouse and I'm going to try to use navigation and it works.
Yes.
Boy.
So cool.
Well, this is just like a full scale game.
I kind of like it.
Uh, well, we still have to set up a couple of UIs like this one, two, three, and also in game UI.
But let's do that in the next video.
I want to break it into two parts.
Okay.
Let's go.