Create Main Menu
Objective: Create main menu for 2D Android game using Unity.
After saving the current scene, create a new scene called Main_Menu from Unity file > new scene. Create a UI Canvas. Setup the Canvas as shown below by changing the canvas scaler UI scale mode to Scale With Screen Size with a reference resolution of 1920x1080. The MainMenu script will be described later.
Add a UI Image called Background to the Canvas. Use stretch stretch anchor preset and replace the source image with the BG sprite from GameDevHQ Filebase.
Duplicate the Background twice changing the copies names to Symbol and Title. Use middle center anchor preset and replace the source image with respectively Symbol and Title sprites. Adjust the width, height and position to that shown below. The color of Symbol was also adjusted to be orange and more transparent.
Create three new buttons named Quit, Menu and Start_Button. Replace the source image with the corresponding image from Filebase, Quit & Start buttons are shown below. Each button uses an OnClick() event to call either the StartButton or QuitButton method on MainMenu script. Drag the Canvas into the runtime only field then select the MainMenu script and the appropriate method for each button.
The MainMenu script on Canvas is shown below. The StartButton() method loads the Game scene and the QuitButton() method exits the application. Make sure both Main_Menu and Game scenes have been added to to the BuildSettings found within Unity’s File Menu.
If the player dies the Damage() method on the Player script to starts a coroutine, ReturnToMainMenu, which, after 3 seconds, loads the Main_Menu scene, allowing the player to restart or quit. The Player script needs using UnityEngine.SceneManagement library to have access to the SceneManager class to load the scene.
Dungeon escape now has a functioning main menu.