Create Main Menu

Hal Brooks
3 min readNov 16, 2021

--

Objective: Create main menu for 2D Android game using Unity.

Main menu’s start button begins game, and returns to main menu on player death.

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.

Canvas parent for the main menu.

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.

Background image for Canvas

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.

Symbol and Title images for Canvas

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.

Quit & Start buttons within Canvas parent.

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.

MainMenu script on Canvas

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.

Damage method on Player provides

Dungeon escape now has a functioning main menu.

--

--

No responses yet