Objective: Create a new scene for our game to act as the Main Menu that can load in our Game scene.
Let's start by creating a new scene in the Unity editor. Go to the File menu and select New Scene. Don’t forget to save your old scene when creating a new one.
We will need to create a UI image and drag in the background images we want to fill our background to act as our title screen and set the camera's background to a solid black color.
We need to create a button that will act as our play button and send us to the game scene when clicked.
move the button to where you want it on the title screen and change the text to “New Game.”
With the background set to how you want it and the button created and ready to go. Create a Main Menu script and attach it to the Canvas:
Before we add anything to the Menu Script, we need to add our scenes to the build settings to get the scene switching to work:
Add both the Main Menu scene and the game scene to the build, but make sure the Main Menu scene is first in the list.
In the MainMenu script, we need to create a method that, when called, will change the scene from the menu scene to the game scene. To do this, we can use:
SceneManager.LoadScene will allow us to pass in the scene's ID to allow us to swap to that scene when called. Also, to access the SceneManager, we need to use the UnityEngine.SceneManagement directory.
Lastly, we have to add the Canvas with the MainMenu script to the On Click event of the button and call the LoadGame method in the script: