Hi everyone. Welcome back to Rehope Games. In this Unity Tutorial, I'm gonna show you how to create a Pause Menu. First, we will prepare the interface for the pause menu. The menu will have a Home button to return to the main menu, a Resume button to continue the game, and a restart button to restart the game. We will create a simple and easy-to-understand code. And I'm gonna show you how to easily add this
pause menu to all scenes. Alright, let's get started. First, let's take a look at the current state of the game. In the previous videos, we made the main menu and the Levels menu. Let's play Level 2. As you can see, when we reach the finish point, the next level loads. I will add a pause button here, so we can pause the game and return to the main menu. This is the main menu scene. I will add the pause menu to the scene where the levels are located. so I will open a level scene. I've created a pause panel and buttons for the Pause menu in Adobe Illustrator. And I've added these
visuals to my project. If you want, you can use your own buttons or simple buttons as well. First, let's create the
pause menu interface. Right click on the hierarchy panel and create a Canvas. I will make a small
adjustment on the canvas. Change the UI scale mode
as scale with screen size. And then set the reference resolution. That's it. First I'm going to add the pause panel to the canvas. Right click on the canvas and create an image. I will change the name to "Pause Panel". And then drag and drop the panel to the source image
of the pause panel. After that I will resize the panel according to the panel size. let's add the buttons onto the pause panel. right click on the pause panel and then create a button. Rename it to “Home”. And delete the text object of the button. Drag and drop the home image to the source image of the button. And of course resize the button according to the image. Duplicate the button. And rename its. Drag and drop the images to the source image of the buttons. There's an easy way
to do align buttons properly. Right click on the Pause panel and then create an
empty object named buttons. Then select all buttons and drag and drop
onto the buttons object. After that select the buttons object and add Grid Layout Group component. Select the rectangle tool and define the area of the buttons. I will set the cell size to 160. And I will set the
alignment to Middle center. From here, you can adjust the spacing between the buttons. As you can see, the buttons are aligned
properly and easily. I want the scene to dim a bit when the pause panel is opened. Right click on the canvas and create a panel. Change the color
of the panel to Black. Increase the alpha value. I will move the dark panel behind the pause panel. so move it up. I'm going to create an empty object named Pause Menu to be tidy. I will add a pause button here to open the pause menu. Right click on the canvas and create a button. Drag and drop the pause image to the Source image of the button. Resize the button. Set the position of the button. Decrease the alpha
value of the button. And I will reduce the size
of the button a little. I want the pause menu to open when I click this button. So I'm going to create a script. Select the pause menu object and create a script
named Pause Menu. Open the script. I will create separate public
functions for each button. I'm going to connect these functions to the buttons as an event function. I've created a game object
variable named pauseMenu. When the Pause button is clicked, I will activate the pause menu. When the Home button is clicked, I will load the main menu. I will add the scene management library to switch between scenes. You can use the scene's name or index to load a scene. When the resume button is clicked, I will close the pause menu. When the Restart button is clicked, I will reload the active scene. Save the script and go back to Unity. Drag and drop the pause menu object to the slot. I want the pause menu to be disabled by default. start the game. When I press the pause button, nothing happens. Because I forgot to connect the pause function to the pause button. Select the Pause button. Click on plus to add an event. Drag and drop the Pause Menu object here. Because it has the Pause Menu script. Select the Pause function. Start the game again. As you can see, when I pressed the pause button, the pause menu opened. But the game is still ongoing. I want the game to stop completely. I will freeze time when the pause menu is opened. Set the Time.timeScale to 0 when the pause button clicked. And when the Resume button is clicked, set the Time.timeScale to 1 again. When the Home and Restart
buttons are pressed as well. Save the script and go back to Unity. Start the game again. As you can see, when I press the pause button, time stops. Let's add the events
of the other buttons as well. Select the home button, Click on the plus, and drag and drop the
pause menu object here. And then select the home function. I will do the same process for the other buttons as well. Let's test it. As you can see, when I press the restart button, the game starts again. When I press the resume button, the game continues. And When I press the home button, the main menu opens. In Level 2, there is a pause menu and it works properly. But as you can see, when the player reaches level 3, there is no pause menu. Now let's see how
we can quickly add the pause menu to the other levels. I’m gonna make a prefab from the canvas so I can easily use it in other scenes. drag and drop the
eventSystem into the canvas. I will drag and drop the canvas object into the project panel to create the prefab. The prefab of the canvas is ready. Let's add the prefab to other levels. That's it. Now, when you play the game again, you can see the pause menu in the other levels as well. I hope this video has given you an idea on how to
create a pause menu. If you liked this video, Please don't forget
to hit the like button. Thanks for watching and see you in the
next Unity Tutorial.