UI in Unity: Creating Main Menu Pt. 2
In our previous article, we created the initial main menu with buttons and backgrounds from our UI elements.
In this article, we will create functionality for our Start, and End Game buttons. We make a MainMenuManager script and attach it to our canvas.
We will make two public functions (Start Game and Exit Game) that we can call into our buttons.
We also want to get a reference to the Scene Manager.
We will enter a reference to SceneManager and set our scene to LoadScene at an index, (0) is the default.
In the Exit Game function, we can simply access the Application.Quit() to exit the game when pressed.
- Application.Quit is only called in a Build Package.
At add our current scene to test, we can add to build settings (index 0).
Next, we will link our public methods to each button assigned.
To load our first scene, from the main menu scene, we can change the default index from 0 and increment to 1.
Next, we will add our scene to load in the build settings.
Now, when we press Start, our scene index of 1 will load.
So now that we have the Start and Exit functions working, in the next article, we can make an Options panel to spawn for our Options button and create a Game Manager script to go back and forth between scenes.