Putting together our Main Menu.
Most of the basic gameplay is going well, so now let's get the Main Menu set up for the game.
First, we need to create a new scene for the Main Menu. We go to File > New Scene, then click Create. Then Unity will create the scene and make it the active scene. We then click on File > Save As... and save our new scene as Main_Menu.
Now that we have the new scene, it’s time to start building it out. The first thing to do is add a UI Image and name it Title_Screen. Then add the MainMenu sprite as the source.
Now that the source image is added, we need to center it and enlarge it to fill the screen. Also, make sure to select Preserve Aspect as well so that it will scale properly.
Now on the Main Camera, change it from Skybox to Solid Color and change it to solid black.
Next, we can add the SpaceBG_Overlay background and stretch it to fill the black background.
Now we need to add a play button to the menu. You right-click on Canvas in the hierarchy and select UI > Button. Then we can name the button New_Game_Button. In the Text element of the button, we can put PLAY. Then let’s move the button over to the right and slightly up.
We need to create a script for the Main Menu, but to keep things tidy, I will put a folder inside the Scripts Folder and name it Main_Menu to keep the scripts related to the Main Menu.
To change scenes, both the Main Menu scene and the Game scene have to be added in build settings. The Main Menu scene needs to be added first for an index of 0, and the Game scene will have an index of 1, as shown below.
Now it’s time to open our Main Menu script. We can delete the Start() and Update() methods. All we need is to create a method to load scene method like below.
Once we save and go back into Unity, we go to our New_Game_Button, and in the Inspector in the section On Click(), we click “+” to add an event. Then drag the Canvas in the object field, and in the Function dropdown, we select MainMenu > LoadGame().
Lastly, we can add some credits to our Main Menu. We’ll go to Canvas > UI > Text. Then change the name to Credits_text, and in the Text field, we can place who it is created by. We need to change the color to white to be seen and adjust the font and move it just below the title.
Now, we have a working Main Menu with a play button and credits to show when the game starts.
I hope everyone enjoyed this article and maybe even learned something as well. Until next time I wish you luck on your coding journey.