Loading Scenes in Unity
To Load a Scene in Unity you need to access the Scene Manager.
Both Methods take a build index or scene name and an optional parameter load scene mode or Load Scene Parameters. The Load Scene return void if using the load scene mode or the Scene that was loaded if using scene parameters. The Load Scene Async returns an Async operation (This can be used in a Corutine and you can get the is done).
The to modes you can use to load a scene is single (Deafult, closes all the scenes that are currently loaded and loads the new scene) or additive (adds the scene to the currently loaded scenes). Note that all scenes that are loaded additive are all rendered.
If you want to reload the current Active Scene you can use
Reload The Galaxy Shooter Game
I added a text object with directions on how to reload the scene to the game over text. I also Animated it in the Display Game over and Game Over Not Displayed Animations.
The Game Over Controller
I added A Coroutine that gets started in the Display Game Over Method. The Coroutine checks for the ‘R’ key being pressed if it is then it Reloads the current level.
The Build Settings
Now this works in the editor with reloading the current scene. If you want to load a different scene or reload the current scene in a released game You need to make sure that the scene is in the build settings.
You can add the current open scene by clicking the button. You can add other scenes by dragging them from the project window into the Build Settings Window. The scenes in Build contain the scene’s path and the build index.
The order in the List is the order that they build. You can change the order and also remove scenes. The first scene that is loaded in the Build will be index 0
The Main Menu
I created a new scene a saved it as Main Menu. I added an Image gave it a sprite and set it to native size. I changed the Canvas Scaler settings to Scale with the screen with a reference resolution of 800x450. I added a Text Ui for the credits. I Added a Button so I can start a new game. I Added a Behavior to the canvas called Main Menu.
The Main Menu Behavior
A simple behavior that has one Method Load Game. The Load Game Loads the scene at index 1.
I Added the Main Menu scene to the build settings at index 0 and made sure that the Game Scene was set to index 1.
I add a callback to the Button’s On Click Event to use the Load Game Method of the Main Menu Behavior on the Canvas Game Object. The Button has a Unity UI Event called On Click. This Event fires when you click on the button.