Game Programming with Unity and C# by Casey Hardman

Game Programming with Unity and C# by Casey Hardman

Author:Casey Hardman
Language: eng
Format: epub
ISBN: 9781484256565
Publisher: Apress


Scene Flow

Let’s examine a quick overview of how the loading of scenes is expected to flow for our game by the time we’re done with this.

The first scene in our build settings – the one that loads first – will be the main menu scene. We won’t have anything in this scene but a basic camera and an empty GameObject with a script that handles the main menu GUI.

This script will be called LevelSelectUI. It will call a little built-in method to instruct Unity that its GameObject should not be destroyed when a new scene is loaded. This way, the LevelSelectUI script can keep running after a level scene is opened.

Each level will have a scene all to itself. They’ll be numbered by their index in the Build Settings, and they’ll only show in the menu once we’ve added them to the build settings.

An instance of the Player prefab will be in each level scene, but the Player script will be disabled by default by unchecking the box beside its name in the Inspector, as we learned before. As well as this, the Camera GameObject inside the Player will be inactive – not just the Camera component, but the whole GameObject.

You can make this change through the Player prefab. First, make sure you’ve updated the Player prefab with any overrides that aren’t applied yet. Do this by selecting the Player in your Hierarchy, then clicking the Overrides dropdown in the header of the Inspector, and selecting “Apply All.”

Now open the Player prefab by double-clicking it in the Project window. Make the camera inactive by default, and disable the Player script by default. Now any level scenes you created (assuming you made one in the last chapter) will update to have the player at the initial state we desire.

Each level scene will have a Level View Camera on it, as we described in the previous chapter. This camera will be enabled by default, so when the LevelSelectUI loads the scene, we see the preview of the level, not the player’s view of the level. The player model will show in the level, but since the Player script is disabled, we won’t be able to move or act.

As long as we’re not in the main scene, our UI script will draw us a button we can press to play the game.

Once that button is pressed, we disable the Level View Camera, enable the Player script, and activate the player’s camera. Now the level is being played officially. We don’t need the UI anymore, so we destroy the GameObject holding the LevelSelectUI script.

When the player wins the level or uses the escape menu to quit (we’ll implement that in the next chapter), they’ll be brought back to the main scene again. Since we’ll be loading the main scene again, the same GameObject with the LevelSelectUI script on it will be there waiting for us again, so we can select our next level to play.

To set this up, we’ll start by cleaning any excess stuff out of the “main” scene we’ve had around since the start.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.