Daily Progress: Main Menu Functionality!

Objective: Create the main menu functions and add the scene to the game.

In the last tutorial we created a main menu scene, and, in this tutorial, we are going to create its functions. To begin we create a script to handle the main menu functions. Attach the script to the canvas then open the script.

Empty the script and we need to add the Scene Manager name space.

We are going to use on click events to handle the buttons on the menu. Let’s begin with the start button.

We are going to use the Scene Manager. We want when the start button method is called the Scene Manager loads a different scene. we call the Scene Manager class, call its default method “load scene” and using a string use the name of the scene as a parameter.

Back in the editor, select the start button and in the hierarchy, we need to add the on click event.

The Object is the Canvas.

Select the No Function drop down > Your Main Menu script > the Start Button Method.

Now when the player presses the start button the main menu will load your Game scene. We can now create a method for the quit button.

When this is called, we want the application to close itself. This can be done by calling the application class and the quit method.

Set up the on click event for this the same way we set up the start button.

When the player clicks the Quit button, the game will close. If you want to implement the Menu button you can make another scene, and just like the main menu create a scene with UI images and sprites. You would then load that menu scene when pressed.

Now we need to add the scene to the build of the game. Open the build settings and in the top, you will see a list of your current scenes. If you are currently in your main menu, you can select Add Open Scenes.

The numbers on the right indicate what scene will be loaded first. Drag the main menu scene to the top so when you load your game it loads into the main menu.

Now you know how to create a functioning Main Menu from scratch! Don’t be afraid to experiment with your main menu and I’ll see you in the next tutorial!