Loading Scenes in Unity
For today’s article, I’ll be going over how I use Unity’s Scene Management system to transition from the main menu screen into the actual game.
What is a Scene?
In Unity, a scene is special unity file that contains all the game-objects and its hierarchy. Internally, .unity files are YAML files that are used to help the Unity Game Engine render what we see in the editor. Scenes in Unity can be thought of as different levels in a game. In our case, the Main Menu and the Game are 2 different scenes where we need to start the game when the player selects the “New Game” button.
Adding Scenes
To add a scene, navigate to File -> Build Settings and select to add open scenes. For example, I added the Main Menu and the Game scenes to build. When a game gets built, it will add all the scenes listed below as part of the final game executable:
The Code
In the Unity Editor, I added in a UI Canvas and a button that will be used to transition the player from the Main Menu to the Game scene.
Next, I attached a MainMenu
script to the UI Canvas with the following code:
I select the Button game-object and locate the Button script attached to it where I wire up the LoadGame()
function onto the onClick
event handler. The onClick
function gets triggered by the Button
script whenever a mouse press is detected on the button.
Thanks for reading :)