Made With Unity | 2D Space Shooter Part 11: Main Menu
Before making some code changes, again, lets go and add a simple Main Menu to our game, it will be a breeze.
CREATING A NEW SCENE
Go to the Scenes folder, Right click -> Create -> Scene. Named the new scene MainMenuScene
Since I want the Main Menu scene to have the same background image and settings like in the GameScene I will drag my Background game object, the one inside the [ — ENVIORMENT — ] game object, to the Prefabs folder.
MAIN MENU SCENE
Double click the MainMenuScene, select the MainCamera and change the Clear Flags to Solid Color and select the color black as the Background.
Drag the Background object from the Prefabs folder
MAIN MENU UI
Right click on the Hierarchy and create a UI image and name it mainmenu_image. Inside the Image component in the Source Image field I’m going to put a sprite image that it was designed to be use on the main menu.
Going to set the Canvas Scaler to have better control on the sprites.
Added some info to the Rect Transform of the mainmenu_image object
Were adding two buttons, Button — TextMeshPro, one is going to be our Play button and the second one our Quit button.
Inside the button game object there is a Text (TMP) object to change the letters on the button; go to each button and one of the type Play and the other one Quit.
MAIN MENU MANAGER SCRIPT
Create a new script inside the Managers folder, named it MainMenuManager and add the script component to the UI. On the MainMenuManager script be sure to add using UnityEngine.SceneManagement on your script.
Line 10 will load the GameScene. To know if the GameScene has the index of 1, we need to go to the Build Settings, add the MainMenu scene by pressing Add Open Scenes and put the GameScene has our second option.
On your GameManager script update the index where we ask permission to Restart the game
MAKING THE BUTTON TO WORK
Inside the play_button and quit_button there is a Button component that has a On Click() section.
On the play_button object press on the plus, add the UI object on the empty field and following the image below find the public method LoadGameScene()
For the quit_button follow the same steps but select the public method QuitGame()
🎮PART 12 🎮