Using Menus with A Controller/Keyboard in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this gains plus James and today we're gonna take a look at how to add controller and keyboard navigation to your game so you don't have to do any movement which are Mouse to interact with your menus you can make a game wholly dependent on just using a controller or just using a keyboard so I'm just going to show what I've got set up here a very simple little menu I'm gonna press play here since she does pause menu you can see the game is actually paused at the moment but we'll talk about why this is set up like this in a moment we've got a simple resume so I can resume and I can hit pause and we'll bring it back up again I've got a very simple options menu this is just to demonstrate going into another menu and this button that does nothing that's just allow us to be able to click on something to demonstrate some other stuff okay so let's take a look at what happens at the moment so by default when you set up a menu like this in unity if I go ahead and play my game here if I'm using a controller if I use my controller here now I can press different directions and nothing happens so if I go out of this pause menu if I'm playing my game running around or whatever and I pause it no matter what direction I press nothing happens with my menu so that's obviously not great at all now one thing we can do with very static menus in unity so this won't work for a pause screen but maybe for the main menu of your game what you can do is go to the event system the event system is always generated when you create a new canvas in unity so the event system object if you look over here in the event system component there's a space here for the first selected so this will be the first object that's selected when your scene starts so I could go here and I could get this resume button drop that in there and now when I press play to see or look the resume button is hired so now I could move up and down and that would be really good for example on a main menu that would work very well because you could just have your options for play the game or quit the game but here in a pause menu or any other kind of menu basically this really won't work very well so it for example if I go and resume them so if I press the resume button and if I hit escape oh our highlights disappeared but actually it's still there so I just pressed down there the first button remains highlighted in it doesn't show up as highlighted but it remains selected according to unity so then when you press the direction it does work but obviously that's not great you want it to look like it's highlighted another issue is if I go into the options menu now oh I can't navigate this options menu I'm moving the directions in the background here and if I move upwards to the very top and hit enter you can see I was still working in that first menu system so if I just to demonstrate that if I just pop just play it again here I've just paused for a second if I turn this options menu on and just drag it over to the side I'll actually just leave this up in the menu at the activators so if I resume here and if I turn on the options you can see rather than go into this new options I'm still highlighting in this menu so we've got a bunch of little issues which are a problem and another significant problem with this is if I have my pause when you're off by default and then press play now if I press the pause button oh that first item isn't even highlighters at all so obviously you can see that the using the event system first selected it can work in certain situations but not every situation and obviously we want a solution that will allow it to happen in every situation and the way to do that is to explicitly control this trucoat and it's really easy to do as well so I'm gonna go into the script I have set up for my menu so I just have one little script here if I open this up now that has is some code for pausing the game so you can see press keep putting on the keyboard here or if I press a button on the controller it'll pause or unpause the game it's going here and set that to be active or false and then we also have the options here for opening the options menu and for closing the options menu which is a button in the options screen itself okay so we have all these here but obviously we want to be able to control what happens in our menu and how we do that as I mentioned in the event system here is what sets what's first selected but this also keeps track of what's currently selected on your UI system in general so what we're gonna do is go back in here and up the very top we're gonna add using unity engine start event systems so we're able to access the event systems components of unity and then we'll tell the event system what object should be pressed but how do we know what objects should be pressed well we need reference to the objects that we want to have active in our menus so we're gonna want three different objects for the menu I can't really have setup so I'm going to create a public game object the first of all create is when we pause the game the first button that should be selected when we open the options menu the options oops spell options correctly options first button so the first button we prepare that should be selected when we open the options menu and then we finally have the options closed button so this will be what's selected when we close out of the options menu so let's go back in and just set them up first of all before we do anything else so save that jump back into unity and go to my script over here and we have the Eddie start so I'm gonna push the first pause button will be the resume because I'll be the top option on that menu the first option in the options menu if I just I just activated here for a second rather than using the eggs that options button lets say we'll go with the toggle up here so I'm gonna select the toggle I'm sure have my script available again select the toggle and click and drag that in there and then when we close out of the options menu we want to have the button that will open the options being selected so I'm gonna grab the options button here and put that in there okay let's deactivate these again and let's make it actually do something in our code so what we do is go back into your script and we're gonna deal first of all what's the first we'll deal with well when we pause the game what is the first button that comes up in my code here is where we activate the pause menu so I am just going to go down below here so this is turning the pause menu to be on and it's stopping time and then what we're gonna do is the first thing we need to do is remove any selected object from the event system currently it's a weird quirk of this thing that you can't just directly set the object you want to use you have to make sure it's emptied out first and then you set the object I don't really understand why unity does that but it's just one of those little quirks so I'm just gonna say we're gonna clear the selected object so how we do that is just go into the event system - current so the current event system that's in use in the game and we're going to assess the selected game objects so we're setting the selected game object that's active on our UI to be no so we're saying make sure there's no object selected then we're going to set a new selected object I just write in common so we know what's going on here and we're gonna say event system - current - set selected game objects and what obviously we want to set when we first pause the game we want to set the pause first button so let's save this jump back into unity and see this in action let that compile and now when I play you should see when I hit escape there we go we get our resume button coming up so now if I close out of it and open the back up again every time we open the menu even if I move it down here it returns to that first button that it should be when you pause the game okay so that's great so let's do the same then for our two other buttons so let's go back in here for when we open the option screen I'm just going to copy all of this little section that we just wrote when we open the option screen we want to do the options first button and then when we close the option screen we want to use the let me just highlight this options closed button okay well sort of save this and that's just verified that that all works because of course when you write any code you should always be jumping back and making sure that it works so I pause it if I go to my options menu there we go I get my toggle selected I've go to my exit options there we go we're back on the options screen here so perfect that works just the way we want to but there's one extra important thing that's really vital to do when you're working with controller based navigation and and keyboard based navigation either way so if I pause the game here if I go down to and move my mouse out of the way if I go down to options here and this is all fine I'm not happy I start on my toggle and if I hit down oh when I move to my slider but I hit down twice and now I've lost I'm where where are my buttons why am I not highlighting any of these objects anymore if I go over here and drag my options menu over to the side like so you'll see oh I'm back to selecting the menu over here I'm just click on this nothing again so I can if I can I can move left and right and I can navigate between these various objects and that obviously isn't great so what's happening here if I just pause this again and we go look at let's look at this toggle here for example you can see these arrows here or how unity is trying to work out where these objects are in relation to each other so this toggle here for example you can press down and we go to this slider or you could press left at the moment and we'll go to the resume button and because these objects previously were overlaid on top of each other so if I just stop and stack this again and go back into the options menu and pause again here you can see look at this look at this mess of arrows usually doesn't know where everything is because as far as unity is concerned all these objects they're visually hidden are still active just because we can't see them doesn't mean they go away so there's two ways we could fix this problem what we could do is I could deactivate all those other buttons like so and then reactivate them when we go after this menu but obviously that's not really great that doesn't feel good in general so we're not really gonna do that instead what we can do is very explicitly tell unity which object it should move when you move in a different direction or which button it should move to when you move into in a different direction so if I turn on my pause menu here and if I go to resume button which is the top button now it's automatically worked out these very simply because they're very basic they're just tree objects in a row but we need to be able to tell it that hey just ignore the whatever set and you want to use so what we want to do is go over here to button component and down here we have navigation here I'm gonna set this to none to remove all those ticks and then I'm gonna go into the bottom and select explicit like this and we're gonna say when you want to move down because the only way you can move from here is down go to the options button so now it knows okay if I press down go to the options button don't ever try to go to any other kind of button so they're the same thing for the options button here will go to none and then explicit I'm do the same so when we go up we go resume and down we go nothing same again so I just gonna loop through all of these here doing this is very simple to do you select on up like that so the same thing then for the options menu and this is much more important because we don't want to be able to go back to those objects that we can't even see so here we're gonna say none again and then explicit and then from our toggle when we move down we're gonna go to the slider so then we'll go through the slider for this slider will also go up to the toggle and down to the exit button and finally the exit button we have will go up to the slider and that's it very simple to do so now if I hide this options menu and hide the pause menu if I press play when I pause us I go down to my options and now I can navigate between all of these perfectly no matter if I press left or rice I'm gonna use the controller as well when I press any direction it moves these correctly obviously if you press left and right on a slider it'll move us up and down which is what you want but now we have full navigation with our menus working correctly so we're pretty happy with that that means that we don't have to worry about things getting deactivated we're able to navigate to our menus but I've ever happened to touch a mouse and it means your players won't get annoyed when they're playing your game and they have to switch from oh I'm playing with a controller but when I go into a menu I have to go and click everything that I want to do so there you go nice and simple to do but really important to make your games feel a lot better so thanks for watching this video I'll be back soon more tutorial goodness and in the meantime keep being awesome you
Info
Channel: gamesplusjames
Views: 75,837
Rating: undefined out of 5
Keywords: unity, unity tutorial, learn unity, unity games, unity school, unity beginners, learn to code, unity c#, c#
Id: SXBgBmUcTe0
Channel Id: undefined
Length: 14min 34sec (874 seconds)
Published: Thu Jan 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.