CS2335
Master_v2
Master_v2
  • Introduction
  • Introduction
    • Introduction
      • Design
      • Game Design
    • Unity - Download
    • Visual Studio - IDE
    • Hero's Journey
  • Unity Basics
    • Unity Editor Windows
    • MonoBehavior - Base-Class
    • Unity Engine - Event Functions
  • Getting Started
    • UI-Elements
    • Animator Controller
      • Animation Steps
    • PlayerController Flow Chart
    • PlayerController Code
      • PlayerController - V1 - S20
      • PlayerController V2 S20
      • PlayerController V3 S20
  • Project 1 - Simple Game
    • Overview
    • Project 1 - Get Started
      • UML Class Diagram
    • Player GameObject
      • PlayerController.cs V2
      • PlayerController.cs V3
    • Create 2D Sprite Prefab: Rock
    • Sorting Layers
  • Project1 Code
    • PickUp PreFabs
    • Player GameObject
    • PlayerController - jump
    • GameData Version1
    • PlayerStats Version1
    • MiniGameManager
      • Logic Diagram
    • Simple Spawner
    • Utility Class
  • Project1 Enhancements
    • PickUp - SelfDestruct
    • Spawn from List of Prefabs
  • Project 2 - StateManager
    • Project 2 - Learning Objectives
    • Project 2 - Starter Assets
    • Project 2
      • State Machine Framework
        • Singleton Pattern
      • StateManager - Singleton Design Pattern
      • IStateBase, BeginState
      • Project 2 -Steps: Create new Scene and State
      • Project 2 - List of Steps
        • Project 2 - Starter Code
  • Project 2 -Dialog
    • Hide_Show_Panel Script
    • Configure TitlePanel, DecisionPanel
    • Simple Dialog Prefab
    • Conversation Scriptable Objects
    • DialogManager_ConvList
    • Image Transitions for Buttons
  • UI Components
    • Finding Game Objects
    • Game Objects: UI vs. 2D Sprite
    • UI Elements
      • Canvas: Screen-Space Render-Mode
      • UI-Buttons To Change Scene
      • Text Input
  • Project2 Resources
    • Visual Novel in Unity-Links
    • Scriptable Object Factory
      • ScriptableObjects
    • Dialog Prefab Packages
  • Project 3 - Overview
    • Branching Story Structures
    • Dictionary Data-Structure
      • Unity PlayerPrefs Dictionary
    • Dictionary: User-Choice Data
      • User-Choices - Example
        • Dictionary Value to Disable Options
    • Simplified Mini-Game
      • PlayerController_v2 Mods
        • PlayerController_v2_final
      • MiniGameManager_v2
  • Proj3: Inventory System
    • Inventory-System
      • Install and Configure
      • Diagrams, Resources
        • Item, Gem, Potion Classes
        • Inventory Class
      • InventoryDisplay, Slot UI
        • InventoryDisplay Class
        • Slot Class
        • Hazard Class
        • Layout Groups
      • Customization Steps
        • Configure Animation
        • AddItem Button
        • Concrete Class: Food
        • MiniGame Mods
          • PlayerController Mods
      • Code: InventorySystem
        • GameData, PickUp Mods
      • Resources: Data Structures
  • Proj3: Custom UnityEvents
    • Event Publishing Patterns
    • Custom Event Messaging
  • Proj3: Mini-Game
    • MiniGame-Overview-Proj3
    • LevelManager
      • LevelManager Logic Diagram
      • LevelManager FSM
      • LoadLevel, StartLevel Logic
      • Code Framework
    • Timer
  • Project 3 - Code Mods
    • Project 3 - Steps
    • Project 3 - Code
      • Code: Final Versions
        • PlayerController Mods
          • PlayerController_v2 Mods
        • GameData - Final
        • LevelManager
        • PlayerStats - Final
        • PickUp, Hazard, ScorePickUp
        • Spawner - Final
        • CameraFollow
        • ScreenFader
        • MiniGameState
        • Example: EndState
      • MiniGameWin Logic
  • Optional, Supplemental Content
    • Optional Content
      • Adding Audio
      • Screen Fading and Reloading
      • ScriptableObjects
      • Disable Debug Logging
      • Events and Actions
      • Saving Data - Serialization
      • Parallax Scrolling
      • Change Sprites
  • C# Language
    • C# Language
      • Variables
      • Enum
      • Encapsulation
        • C# Properties
        • Access Modifiers
      • Inheritance
      • Polymorphism
      • Interface
      • Switch-Case
      • List< T >
      • Queue< T >
      • Dictionary
      • Foreach
      • Static
      • Ternary Operator: ?
      • this
      • Delegates
    • Diagrams
      • State Machine Framework
      • UML Class Diagrams
      • Level Manager Logic Diagram
      • Flow-Chart: NumberGame
      • FSM: NumberGame
    • Glossary
    • References and Resources
    • Random Thoughts
Powered by GitBook
On this page
  • Create New Scene, Add Scene to Build Settings
  • IMPORTANT: Add Scenes in Unity Build Settings
  • Add GameScene Enum to StateManager.cs script:
  • Configure the Canvas to use Screen-Space Camera
  • Create a SceneXState.cs Script file to correspond to the new Scene
  • Change Script Steps:
  • Add A Button in BeginScene to go to the new Scene.
  • Add Code to BeginState for the new Button's Function

Was this helpful?

  1. Project 2 - StateManager
  2. Project 2

Project 2 -Steps: Create new Scene and State

PreviousIStateBase, BeginStateNextProject 2 - List of Steps

Last updated 5 years ago

Was this helpful?

This section will provide details for how to create a SceneX and corresponding SceneXState.cs C# file, where these items will be added to a copy of Project1, rename to Project2. This assumes you have either downloaded the Proj2 starter-code unity-package, or the Proj2 starter-code assets folder. See:

Create New Scene, Add Scene to Build Settings

To create a new scene, right-click in the Unity Project / Assets Panel and select the Create > Scene option. Name this scene using a descriptive name according to your project's theme. Next, Add this scene to your project's build settings, following the instructions in the main Project2 page:

IMPORTANT: Add Scenes in Unity Build Settings

For this project to work, you must first go into your project's Build Settings. (File > Build Settings) You must add all scenes to the Scenes to Build Panel. Drag the scenes from your assets directly into the Scenes to Build Panel. Note the order, and SceneID's on the right side, this must match GameScene Enums in StateManager, BeginScene must be higher than EndScene in the order, it will have a 0 on the right side of the panel, while EndScene will has ID: 1.

Build Settings with 5 Scenes - shown below

Add GameScene Enum to StateManager.cs script:

/// <summary>
/// Game Scene. Matches Unity Scenes in Build Settings
/// </summary>
public enum GameScene //must match Build Settings Order
{
    BeginScene = 0,
    Scene2 = 1,   
    MiniGame = 2,
    Scene3 = 3,
    EndScene = 4
}

Configure the Canvas to use Screen-Space Camera

Create a SceneXState.cs Script file to correspond to the new Scene

Create a new C# Script in the scripts folder of your project's asset panel. Name the script so it's obvious this script corresponds to the scene you just created. Then, we'll need to modify the code for this script so that it is similar to the other SceneXState.cs files that were included in the starter code, such as BeginState.

Change Script Steps:

  • Add 'using' directives for UnityEngine.UI to the top of the script file:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;   //ADD THIS using directive
  • Remove: MonoBehaviour ( Base-Class )

  • Add: :IStateBase ( interface )

  • Remove: Unity Start( ) and Update( ) event functions

  • Add code for IStateBase Interface,

    • variable: private GameScene scene;

    • property: public GameScene Scene;

    • method: public void InitializeObjectRefs( ){ ... }

  • Add: Constructor for the Class - Sets scene value.

public class SomeState : IStateBase
{
    /// <summary>
    /// The scene enum associated with this state.
    /// </summary>
    private GameScene scene;

    /// <summary>
    /// Read Only Property gives access to the scene number - enum
    /// </summary>
    /// <value>The scene.</value>
    public GameScene Scene {
        get{ return scene; }
    }

//additional code must be added, see below    
} //end class
  • Add code for IStateBase, Method: InitializeObjectRefs( )

    /// <summary>
    /// Similar to Unity Start() 
    /// exectued once, after scene is loaded - called from StateManager
    /// Used to initialize object references - can be used to cache object references
    /// </summary>
    public void InitializeObjectRefs ()
    {
      //code will be added for initializing object refs for scene buttons
      Debug.Log("In InitializeObjectRefs for SomeState ");
    }
  • Add Class Constructor, and set the corresponding GameScene Enum.

 public SomeState ()
    {
        scene = GameScene.SomeScene;
        Debug.Log("In Constructor for SomeState");
    }

Add A Button in BeginScene to go to the new Scene.

Now you must add buttons to other scenes, and code to other state scripts so you can get to this new scene.

To add a Button to the BeginScene, select: GameObject > UI > Button. Name this button so that it corresponds to the task it will be used for. For example, we can call it: SomeSceneButton

Add Code to BeginState for the new Button's Function

  • Declare Object-Reference Variable for a Button Component

private Button someSceneBtn;
  • Initialize the object reference in: InitializeObjectRefs( )

public void InitializeObjectRefs ()
    {
        someSceneBtn = GameObject.Find ("SomeSceneButton").GetComponent<Button> ();
        someSceneBtn.onClick.AddListener (LoadSomeScene); //call custom method defined below
        Debug.Log ("In BeginState initializeObjRefs");
    }
  • Write Custom Method to change scene and state

/// <summary>
    /// Event handler - called when endBtn is clicked
    /// Loads the end scene.
    /// </summary>
    public void LoadSomeScene ()
    {  
        Debug.Log ("Leaving BeginScene going to SomeScene");
          StateManager.instanceRef.SwitchState (GameScene.SomeScene);  //create new state, pass to StateManager
    }
  • Use the Button to go to the new Scene, Add Images,2 Buttons to each of 5 scenes

See: Configure Canvas to use Screen-Space Camera
Starter Assets