PAUSE MENU in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right I admit it I might have gone a bit menu crazy lately so far we've created both a main menu and a settings menu so let's finish up by creating a pass menu as well when we're done it should look something like this also special thanks to Judy Minh our Minoru C and infinity PPR for their support on patreon you guys are awesome so let's get started series the game that we want to create a pause menu on top of I created this game for a Ludum dare ray and also made a video on it which you can check out in the top right corner so first let's begin by creating a canvas we'll go to the hierarchy right click go UI and select canvas let's make sure it's set to screen space overlay and let's select pixel perfect now if we go to the scene view we should be able to switch into 2d mode hit F to focus on our canvas and now here we can place our UI elements let's right-click on a canvas go UI and create a panel we want this to fill the entire screen so that's perfect but instead of making it white let's make it a transparent black if we switch into the game view we can see that this can act as a nice space for the rest of our UI we'll also change the source image from background to none and this will get rid of the edges around our game view now we can rename this panel to pause menu and we'll put all of our UI inside of this object so let's add some buttons to our post menu we'll right-click go UI select button let's switch back into the same you let's scale this while holding down alt to scale it from the center now I want to go ahead and disable our image here and instead focus on the text to do that we'll go under the button and select our text object let's bump up the font size quite a bit we also change the text to something like resue and also make the text completely white now if we have a look in our game you that already looks better but I also want to change the font style to bold whenever you're working with text on the UI I recommend picking up text mesh Pro it's a free asset on the unity asset story and it was recently acquired by unity it makes everything to do with text much much nicer we're gonna skip over it in this video but if you want to learn more about tech smash broz I have a separate video on the topic and also use it in the tutorial on creating a main menu however one thing that we can do with the default text is add a hard shadow to do that we'll hit add component will search for shadow we can keep the transparent black here but we do change the effect distance to something like four by minus four that definitely makes our text more visible we then select our button switch into the scene view and move it up a bit and let's now rename this to resume button if we go into the game view and hit play will of course see that our game plays in the background but we should now be able to click on a resume button however nothing seems to happen and we don't really get any visual feedback that anything is going on that's because we disabled our image component and by default unity shows what happens to a button under the transition but simply tending the color of our image so let's rename our image it's change the color to completely black and then under the normal color we'll set the Alpha to zero so by default we won't be able to see this however if we then highlight our button so if we hover over it will bump up the Alpha a bit and if we then press the button will pump up the Alpha even more so now if we hit play we should be able to hover over a button and it will visibly show that we're doing so and when we press it we can see that it turns even darker you'll also see that if we stop hovering over our button it's still selected that's because we need to change the navigation to none and that should fix it I want to duplicate our resume button move it down and this one is going to be our menu button so that's change the text to menu I also want to make it smaller so that's bump down the font size and let's then resize our button to fit it we can now take this button duplicate it move it down and this will be our quit button so let's again change the text to quit and I think that actually looks pretty nice let's rename our second button to menu button and our third button to quit button and when we now play the game we can select the different buttons now that we're satisfied with the UI of our menu it's time to add a script to control it to do that let's start by disabling our pass menu let's then go to our canvas and let's hit a component he will create a script called pause menu that's going to new script it's like c-sharp and hit create an ad and let's open it up in visual studio in here we want to delete our start method and instead let's create a variable that will keep track of whether or not our game is currently paused we'll make this a public static variable public because we wanted to be accessible from other scripts and Static because we don't want to reference this cific pass manuscript we just want to be able to very easily check from other scripts whether or not the game is currently passed so we'll write full here because the value can be either true or false let's name it something like game is passed by default this will be false then inside of our update method we can check whether or not we want to pass the game and you can of course use any key that you would like for this I'm just gonna use the Escape key so we'll write if input that get key down and again the key that we want to check for is Kiko dot escape if the game is currently passed so if game is passed is equal to true well then we've pressed escaped by the game was already passed and so we want to now resume it and if not well then we've pressed the Escape key while the game was not passed and so we want to go ahead and pass it now of course we need to create these two methods so we'll create a void resume as well as a void pause and what do we want to happen here well when we are pausing the game we want to bring up our parts menu we want to freeze time in our game and we want to change our game is past variable to true when we then resume it we want to do all those things but opposite so bring down our past menu set time back to normal and set game is past to false so let's do that first to control our UI and we need a reference to it so we'll create a public game object and we'll call it our past menu UI then when we pass the game we'll go pause menu UI dot said active and here we can enable and disable our game object we want to enable it so we'll set it to true we then want to freeze time we'll do that by writing time dot timescale and this is the speed at which time is passing you can use this to create slow motion effects or quite simply just set it to zero in order to completely freeze the game and finally we want to set game is passed to true and again we want to do the exact same thing but opposite when we resume it so pause menu UI dot said active and this time we want to disable it so we'll write false we want to set time dot timescale back to normal which is one so that speed passes at a normal rate and we'll set game is equal to force and now we should see that if we save this and head into unity we now have an empty slot for our past menu UI and here we'll drag in our past menu UI that we just created now when we play our game works as normal right until we press escape the game freezes our UI is brought up you can still hear the music playing in the background and we can now press on a different buttons of course they currently don't do anything but we'll fix that in a second if we then press escape our game returns to normal so we can now effectively pass our game and remember we can always use game is paused to find out whether or not our game is currently paused say we wanted to do this inside of my Audio Manager in here could actually go ahead and pitch down sounds that plays when the game is paused to do that I would simply write if then we access the pause menu dot and here we have it game is passed and I can now use this to decrease the pitch of my audio source just to give you an example of why this is useful but for now the main thing that we want to worry about is connecting of these buttons to do that we want to create a function for each one the resume button is easy we simply wanted to resume the game and we do that right here so we actually already have a function for that all we need to do is go in and mark it as public and we can now trigger it from our button we also need to create two more functions of course they'll still be public the first one will be called load menu and the second one quit game and we'll write this out in a second but for now let's just display something in the console debug that log loading menu and here debug deadlock quitting game let's save this going to unity and let's try and select one of our buttons this is the recent button if we go ahead and scroll down we can see that we have an on click event under our button we'll add an action to this so whenever this button is clicked this action will be executed our action sits on the canvas so we'll drag in our canvas it's under the pause menu script and it's called resume and we can do the same thing for a menu and quit button let's select our menu button add an on click event let's drag in our canvas go into past menu and that's like load menu and finally let's hook up a quit button add a new event drag in our canvas go a new path menu and select quit game and if we now wait to hit play pass the game select menu it's gonna say loading menu so that quit it's going to say quitting game and if we hit resume well our game resumes awesome so now we can make these two functions actually do something in order to load our menu we'll just need to go to the top here and use unity engine dot scene management and now we can access our scene manager in order to load a new scene and the scene that we want to load is the menu of course encourage you not to hard code it in like this and create a variable for it but I'm sure you can figure that out on your own then in terms of quitting the game we'll simply go application dot quit and whenever we call this nothing will happen inside the editor so let's just leave our debug that lock in here to let ourselves know what is going on if we now save this script go into unity we need to make sure that we have a scene called menu which we do and we also need to go to file build settings and make sure that we've added it to the scenes in build now if I play this hit escape and select menu you can see that it loads up by main menu but the game is still paused of course we don't want this so inside of our script we need to also adjust time to time scale back to the normal one now we can save this again and when we now try and go to the menu we can see that working as normal and we can actually drive around the menu yay finally you might want to add a bit of animation when you pass the game to do that we'll go under animation we'll create a new animation I'm gonna put it inside of my animation folder and here I'm gonna call it pause menu and all that I'm gonna do here is hit record go forward a little bit and simply go into color and change the Alpha to zero now you can see that as I scrub through time our alpha decreases of course we want to reverse this so I'll simply take the keyframes and switch them and now when we play we can see our background fades in it also currently loops to change this we'll go and find the animation mine was named pause menu and here we'll make sure to disable loop time and the last thing that we want to do is select our path menu and under the Adam we want to change our update mode from normal to unskilled time otherwise our animators will also be frozen by the fact that we change our time scale to zero when we select unskilled time our animator will just completely ignore our current time scale so that's why we do that for UI stuff we can then disable our paths menu maximize our game and our path menu should now be complete if I hit escape our game passes with a nice smooth transition from here I can quit the game which will of course work if you build it I can resume it and I can switch to the main menu awesome that's pretty much it for this video we're going to be taking a short break for Christmas Eve but we'll be back in exactly one week with a video on how I started making games it's a video that I'm really excited about so hope you guys will enjoy it on that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in November and especially thanks to Judah Minh Amanda rusi infinity PPR and soft tune cyber crime Derrick Eames Kirk Faizal Murphy James P Dan Evans Thomas Wally Superman the great John Bogart Kolka prowl Jason the Tito Alex rocket ski man Ellis James Rogers Robert pond Rob fair and Rasmus you guys Rock
Info
Channel: Brackeys
Views: 699,519
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, games, programming, coding, C#, pause, menu, pause menu, ui, ugui, freeze, timescale, animation, interface
Id: JivuXdrIHK0
Channel Id: undefined
Length: 12min 12sec (732 seconds)
Published: Wed Dec 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.