Loading Scenes in Unity
Day 20 — Aspiring Game Creator
2D Game Dev: Galaxy Shooter
Objective: Create a Main Menu scene which loads the game when New Game button is clicked.
Pseudo Code:
- Create a new scene.
- Add background and UI graphics.
- Add UI button.
- New UIManager class which will be attached to the Canvas game object.
- Create Timeline animation.
- Hide all main menu elements until animation is completed.
- MainMenu class will be responsible for loading the next level when New Game button is pressed.
Timeline is wildly easy to use, BUT as a rookie, it is also a very slow process.
Once Timeline is installed, create an empty game object and allow Timeline to create the Director Component.
Now it’s all simply ADDing animations & activations tracks.
Once an ANIMATION track is added the only way to see and move game object is to click the record button and tweak the object to create a keyframe. After a bit of basic animation is in place the ACTIVATION track needs to be added to enable (or disable) the game object during the animation track.
Once the Timeline animation has completed the New Game button waits for player input.
In order to load a scene in Unity the Scene Management library needs to be added:
In order to enable the New Game button to load the next scene a public method needs to be created.
To ‘hook’ the UI button up, the Canvas game object is added to the OnClick.
Then the public method from the MainMenu class will be available via the drop down (see below).
Once added, the MainMenu.LevelLoader wants the INT variable representing the scene number to load.
Though it’s not needed at this point, the asynchronous method is used for future proofing, in this case that means displaying a “loading’’ progress bar.
The asynchronous method will allow for ‘background’ loading and not burn all resources to load the next scene, but in the case of Galaxy Shooter it’s lightning fast.