Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 2021 Cookbook

You're reading from   Unity 2021 Cookbook Over 140 recipes to take your Unity game development skills to the next level

Arrow left icon
Product type Paperback
Published in Sep 2021
Publisher Packt
ISBN-13 9781839217616
Length 816 pages
Edition 4th Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Shaun Ferns Shaun Ferns
Author Profile Icon Shaun Ferns
Shaun Ferns
Matt Smith Matt Smith
Author Profile Icon Matt Smith
Matt Smith
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

1. Displaying Data with Core UI Elements 2. Responding to User Events for Interactive UIs FREE CHAPTER 3. Inventory and Advanced UIs 4. Playing and Manipulating Sounds 5. Creating 3D Objects, Terrains, Textures, and Materials 6. 2D Animation and Physics 7. Characters, Game Kits, and Starter Assets 8. Web Server Communication and Online Version Control 9. Controlling and Choosing Positions 10. Navigation Meshes and Agents 11. Cameras and Rendering Pipelines 12. Shader Graphs and Video Players 13. Advanced Topics - Gizmos, Automated Testing, and More 14. Particle Systems and Other Visual Effects 15. Virtual and Augmented Reality (VR/AR)

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 called _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).
  1. Add a UI Button to the scene positioned in the middle-center of the screen. In the Hierarchy window, click on the show children triangle to display the Text child of this GameObject button. Select the Text GameObject and, in the Inspector window for the Text property of the Text (Script) component, enter the text goto page 2:
Figure 2.4 – UI Button Text child
  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 then 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 scene1 to the build, open the page1 scene and go to File | Build Settings.... Then, click on the Add Open Scenes button so that the page1 scene becomes the first scene in the list of Scenes in the build. Now open page2 and repeat this process so that 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 the page1 scene open.
  2. Create a C# script class called SceneLoader, in a new folder called _Scripts that contains the following code. Then, add an instance of the SceneLoader as a scripted component to Main Camera:
using UnityEngine; 
using UnityEngine.SceneManagement; 

public class SceneLoader : MonoBehaviour { 
    public void LoadOnClick(int sceneIndex) { 
        SceneManager.LoadScene(sceneIndex); 
    } 
} 
  1. Select Button in the Hierarchy window and click on the plus (+) button at the bottom of the Button (Script) component, in the Inspector window, to create a new OnClick event handler for this button (that is, an action to perform when the button is clicked).
  2. Drag Main Camera from the Hierarchy window over the Object slot immediately below the menu that says Runtime Only. This means that when the button receives an OnClick event, we can call a public method from a scripted object inside 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:
Figure 2.5 – Button (Script) settings
  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 Main Camera and then add an OnClick event action to the button that calls LoadOnClick and passes an integer of 0 so that page1 is loaded.
  3. Save page2.
  4. When you run the page1 scene, you will be presented with your Main Menu text and a button that, when clicked, makes the game load the page2 scene. On page2, you'll have a button to take you back to page1.
You have been reading a chapter from
Unity 2021 Cookbook - Fourth Edition
Published in: Sep 2021
Publisher: Packt
ISBN-13: 9781839217616
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image