Unity 2018 Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

To create a button-navigable multi-scene game, follow these steps:

  1. Create a new Unity 2D project.
  2. Save the current (empty) scene, in a new folder, _Scenes, naming the scene page1. 
  3. Add a UI Text object positioned at the top center of the scene, containing large white text that says Main Menu (page 1).
  4. Add a UI Button to the scene positioned in the middle-center of the screen. In the Hierarchy, click on the show children triangle to display the Text child of this GameObject button. Select the Text GameObject, and in the Inspector for the Text property of the Text (Script) component, enter the text goto page 2:
  1. Create a second scene, named page2, with UI Text = Instructions (page 2), and a UI Button with the goto page 1 text. You can either repeat the preceding steps, or you can duplicate the page1 scene file, naming the duplicate page2, and edit the UI Text and UI Button Text appropriately
  2. Add both scenes to the Build, which is the set of scenes that will end up in the actual application built by Unity. To add the scene1 to the Build, open scene page1, then choose menu: File | Build Settings... then click on the Add Current button so that the page1 scene becomes the first scene on the list of Scenes in the Build. Now open scene page2 and repeat the process, so both scenes have been added to the Build.
We cannot tell Unity to load a scene that has not been added to the list of scenes in the build. This makes sense since when an application is built we should never try to open a scene that isn't included as part of that application.
  1. Ensure you have scene page1 open.
  2. Create a C# script class, SceneLoader, in a new folder, _Scripts, containing the following code, and add an instance as a scripted component to the Main Camera:
using UnityEngine; 
using UnityEngine.SceneManagement; 

public class SceneLoader : MonoBehaviour { 
    public void LoadOnClick(int sceneIndex) { 
        SceneManager.LoadScene(sceneIndex); 
    } 
} 
  1. Select the Button in the Hierarchy and click on the plus sign (+) button at the bottom of the Button (Script) component, in the Inspector view, to create a new OnClick event handler for this button (that is, an action to perform when the button is clicked).
  2. Drag the Main Camera from the Hierarchy over the Object slot immediately below the menu saying Runtime Only. This means that when the Button receives an OnClick event, we can call a public method from a scripted object inside the Main Camera.
  1. Select the LoadOnClick method from the SceneLoader drop-down list (initially showing No Function). Type 1 (the index of the scene we want to be loaded when this button is clicked) in the text box, below the method's drop-down menu. This integer, 1, will be passed to the method when the button receives an OnClick event message, as shown here:
  1. Save the current scene (page1).
  2. Open page2 and follow the same steps to make the page2 button load page1. That is, add an instance of the  SceneLoader script class to the main camera, then add an OnClick event action to the button, which calls LoadOnClick, and passes the integer 0, so scene page1 is loaded.
  3. Save scene page2.
  4. When you run the page1 scene, you will be presented with your Main Menu text and a button, which when clicked, makes the game load the page2 scene. On scene page2, you'll have a button to take you back to page1.