Creating a Main Menu in Unity

When playing games, the Main Menu is an after thought. As the player, you really don’t think about it because every game has one. It is standard. You would be pretty surprised if you clicked to open a game and instantly the game started. This article will talk about how to create a Main Menu with an interactive button that starts a new game.

The Main Menu is going to be a completely NEW SCENE. We are going to be adding an UI Image Element and in the Image Element we will add the Title Image. With this addition a Canvas and EventSystem is created in the Hierarchy.

In the Canvas, we are going to be adding a Button. Adding the Button, comes with a button image and a text component. The text component allows you to customize what text in the Button will read, font color and style.

The Button Element has customizable features that not only allows the button to be interactive, which is what the EventSystem handles, but you can customize what happens when the button is clicked, highlighted, pressed by changing colors, sprite images and/or animations.

The button also has the capability to execute a function when it is clicked. This is key, because we can tell the program what scene or level we want to load when the New Game button is click. This calls for a MainMenu script to be created and attached to the Canvas.

In the MainMenu script will need access to the UnityEngine.SceneManagement library, so we can access the Scene Manager. We now just need to create a public method of LoadGame and we will load the scene of the first level.

We will need to add the Main_Menu scene to our Build. In Build Settings, click on the “Add Open Scenes” to add it and we will have to rearrange it, so the Main_Menu is Scene 0 and the Game Scene is 1. You can easily to do this by clicking and dragging the Scenes in the Scenes In Build box.

Let’s revisit the On Click feature of the Button UI Element. First, we need to add the GameObject that has the MainMenu script which is the Canvas (red arrow). Once the On Click has access to the MainMenu script via the Canvas, we can call any of the methods on the script as long as the method is public. Now, when the Button is clicked, it will executes the LoadGame function that loads the game scene.