How to Control Your Menu with Keyboard/Gamepad | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys really quick and simple one here we're gonna make a menu and you're going to be able to navigate through the menu with your keyboard or your Gamepad let's go so what's in my scene doesn't really matter because we're just going to make a menu from scratch and if you already have a menu and don't want to follow along with this I've broken this video into chapters so you can just skip past this one so step one let's make sure we have the input system installed mine is already in the project which is fine once you've installed it let's create an empty so we can create a UI section here and in there let's create another empty called menu and from there let's right click and create a new UI panel we'll use this as kind of an overlay that's partially transparent so we know that the game is paused and that we're in the menu so if this is your first UI element in the scene it's automatically going to create a canvas with a panel inside of that as well as an event system and since we're using the new input system let's go ahead and click replace with input system UI input module we'll take a look at this later call that panel pause overlay and let's set it to a nice dark color with a about 200 in the alpha and let's call the canvas main menu canvas so first off let's create a new text mesh Pro text element you'll need to import the essentials if you haven't used it in your project yet let's hit alt and shift so that we set the pivot and the position of the text to the top Center and bring it down by 150. a width of 370 and a height of 50 is fine and let's give it a nice big font size of 60. and actually let's go back to the canvas and set the canvas scalar to scale with screen size and set the reference resolution to 1920 by 1080. you can see by doing it this way the canvas will resize no matter how big the play area is and let's Center the text as well and we'll reset it to say paused and let's rename this to menu text okay now let's create a text mesh Pro button call it settings button set the width to 250 and the height to 80. and down here let's give it a nice red color in the selected so that we know when it's the selected button the center of the screen is fine and let's change the text to say settings now let's duplicate this button and call it resume button let's move it down by 110 on the Y and change the text to say resume okay now let's duplicate this whole canvas and rename it to settings canvas and let's deactivate the main menu we'll change the menu text to say settings let's duplicate this button and move it up here and we'll move it up by 110 on the Y call this top one Gamepad controls button and change the text to say Gamepad controls we'll call The Second One keyboard controls button and we'll change the text to set keyboard controls and change the last one to back button and change the text to back and let's deactivate this canvas as well let's set up controls really fast right click in your project and create a new input action let's call it player controls click edit asset and let's dock it create an action map called player or whatever you want you can use this to organize different control Maps if you want different ones for UI or in-game that sort of thing let's rename this action to menu open close and action type button is great now let's add a control scheme called keyboard and assign the keyboard to it and optional is fine and create one more called Gamepad also optional and assign the GamePad to it make sure you put this back to all control schemes so you can see see everything now for this first binding let's search for escape and assign it to the keyboard control scheme and let's add one more binding and search for the start on the GamePad you can just hit listen and press the button that you want to and assign it to the GamePad don't forget to save your asset now we want to be able to read inputs from this so let's create an empty called input manager and create a new script called input manager and put it on there and before we open it also add a player input script which comes with the input system add in your player controls and your default action map player now let's open up input manager make it a Singleton so that it's easy to call and we'll add one Bool called menu open close input we'll get a reference to our player input you'll need your input system namespace at the top and we'll set up an input action called menu open close action which we'll set up in the awake function then in update we can update that Bool by calling dot was pressed this Frame from that input action there we go we're ready to set up the menu manager which will actually control our menus as well as allow us to use a keyboard and Gamepad within them create a script called menu manager and add it to our menu object here so first let's get a reference to our main menu canvas and our settings menu canvas because we want to make those inactive in start we don't want the menu being open when we start the game now let's create a private pool called is paused and an update will check our input manager for the menu open and close input and if the game isn't already paused pause it otherwise we'll unpause it so in our pause function we want to set is paused to True we'll freeze time and let's open the main menu and to do that we simply want to activate our main menu canvas and deactivate the others now in our unpause function we'll set our is pause to false reset the time back to normal and close all the menus and to close all the menus we'll just deactivate all the menu canvases so let's test this assign your game objects here we can press the escape on our keyboard or the start button on our Gamepad and the menu opens and closes awesome really quick it's worth noting that freezing time doesn't truly pause your game and there are many ways to go about it but for me I'm just going to deactivate my player and player attack functions to completely ignore inputs from everywhere so I'll add those in my script and deactivate them in my pause function and I'm going to reactivate them in my unpause function but depending on your project you might have different needs but at least now my character truly won't receive any input while I'm in the menu but as you can see we currently have no way of navigating through these menus with our Gamepad or keyboard and we can click on them but we haven't actually assigned anything to the buttons so obviously nothing happens yet so let's fix that by assigning things to these buttons so I'm going to create a region for organization and create two very simple public methods in on settings press we'll call the open settings menu handle which we'll now create in there we'll just activate the settings menu and deactivate the main menu and back in our on resume press function we'll call on pause and one more I want to get the back button working as well so let's create an on settings back press function which we'll call the open main menu function which is the same one that we called in our pause function so let's go back to our inspector and assign on settings press to the settings on click event and on resume press to the resume buttons on click event and on settings back press to the setting menus back button on click event that was a mouthful let's do a quick test and there you go you can hit settings to go into the settings menu and we're not going to do anything with these buttons I'm doing a whole separate tutorial on key rebindings for those so we're not going to touch those buttons right now but we can go back and we can resume okay so that is the basics of how you can get a simple menu system working now let's actually make it so we can scroll through these menus with a Gamepad or keyboard navigating through the menus is actually really easy the only problem is that Unity doesn't know which button should be selected when you're mashing buttons on your Gamepad and by default it's not going to select anything and that's where this field comes in handy and yes we could go ahead and drag something in there but you generally don't want to do that unless this is like a whole separate scene for just your main menu or something like that so we're going to do it with code instead and all we have to do is pass in a game object so let's create two serialized game objects one for our main menu and one for our settings menu now in order to set the selected game object we'll need to add the unityengine.event systems namespace now in this function when we open our main menu we want to set our selected game object as our menu's first item and in this function where we open our settings menu let's assign the selected game object as the settings menu first and let's null it out when we close all the menus easy let's assign those objects in the inspector now as soon as we open the menu the resume is nice and red which is what we used as our selected color and you'll notice even though we haven't touched our controls or anything we can already use the analog stick on our Gamepad or the arrow keys on our keyboard to navigate through these options so that's great but where are these controls coming from it's all coming from our event system here there's a default input actions here with a whole UI section with controls for a Gamepad joystick and keyboard already set up for you you can change these or create your own and plug them into the event system if you want so the last question really is how does it know which buttons to navigate to based on your presses for example maybe from the settings option here you want to be able to get to the resume button by pressing up or down or maybe just down while all applicable UI elements will have these navigation options here it's set to automatic by default and these navigation settings are what control these arrows here automatic is pretty good but there might be cases where you want explicit which is where you tell it exactly what UI elements it can navigate to by dragging them in here and that is it thank you so much guys if you enjoyed this tutorial please give it a like and let me know if you have any questions down in the comments I want to give a very special thank you to our Hall of Fame patrons Kessler Darren perine throbbing wind Fontaine weight and couch as well as our Early Access patrons Ziona and Ken Wade if you choose to support us on patreon you can get early access to all our videos monthly Alpha builds and more
Info
Channel: Sasquatch B Studios
Views: 19,099
Rating: undefined out of 5
Keywords: unity, unity2d, tutorial, unity tutorial, sasquatch b, sasquatch b studios, unity beginner tutorial, unity input system, unity setting up a menu, unity UI, unity settings menu, unity main menu, unity menu naviagation, unity control your menu with a keyboard, unity control your menu with a gamepad, unity navigate in menu with controller, unity navigate in menu with keyboard, unity navigate in menu, unity navigate in menu with gamepad, unity menu manager
Id: e30i55Hp-to
Channel Id: undefined
Length: 9min 42sec (582 seconds)
Published: Thu Apr 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.