Despite only taking the first half of the day to work on Unity projects, I managed to get a lot done! A quick overview of the sections completed today:
- Stealth Game and Cinematography
- Main Menu
- 2.5D Platformer
- Intro & Setup
- Player Movement
One thing I wanted to write up on was Scene Management and AsyncOperations. We had a taste of it in the 2D Space Shooter game when we had to set up the main menu. But the stealth game also has us transitioning from the main menu to a loading screen before we get to the main game screen.
Let’s walk our way through making a main menu screen with the options to Start and to Quit. To transition between scenes in-game in Unity, you first need to add all of your scenes to your Build in Build Settings (otherwise you won’t be able to get a reference to the other scenes). Once you do this, you can start setting up your menu by adding
in your main menu script. Think of this as your library card — it allows you to access the information Unity’s SceneManagement namespace provides. The most useful method we’ll use for this is the LoadScene method, which does exactly what it says on the tin. Make sure you pass in either the name of the scene you want to load through a string or the scene number that appears in the Build Settings.
The last step is to create the proper functionality on the buttons in the menu. These buttons should be children of the Canvas and your main menu script should be a component on the Canvas. When you select the buttons there should be an option in the Inspector to put in an On Click() function — add to the list, put the Canvas object where it asks you to select an object, and then select the method you want to call from the drop down menu on the right. It’s really easy!
When using a LoadLevelAsync coroutine, you can even track its progress until completion, which is extremely helpful when creating load screens!
Biggest takeaways from the day: AsyncOperations are intimidating at first, but with the right practices, can be invaluable to your code.