CS2335
Master 1.0.0
Master 1.0.0
  • Introduction
  • Introduction
    • Introduction
      • Game Design
    • Unity - Download
    • Visual Studio - IDE
    • Unity Manual, Scripting API
  • Unity Basics
    • Unity Editor Windows
    • Behavior Components
    • 2D Project, Scenes
      • Create 2D Sprite GameObject
      • Create UI-Text GameObject
    • MonoBehavior - Base-Class
    • Create a Custom C# Script
  • Game Coding Structures
    • Games Overview
    • Unity Engine - Event Functions
    • Finite State Machines
    • UML Class Diagram
  • Animation
    • Animator Controller
    • Animation Steps
      • Optional: Dead Animation
    • PlayerController.cs V0
  • Project 1 - Player
    • Player GameObject v1
      • C# Generics, Statics
    • Player GameObject - Jump
    • PlayerController.cs V2-Jump
    • PickUp PreFabs
      • Sorting Layers
    • PlayerController.cs V3-Collide
    • GameData Version1
    • GameData Version2
    • PlayerController V4-Score
  • Project 1 Details
    • Project1 GameObjects
    • PlayerStats Version1
      • UI-Canvas
    • Utility Class
    • Simple Spawner
    • MiniGameManager
      • Logic Diagrams
      • StartButton
      • ResultsPanel
  • Project1 Enhancements
    • PickUp - SelfDestruct
    • Spawn from List of Prefabs
  • Project 2 - StateManager
    • Project 2 - Learning Objectives
      • Inspiration
        • Branching Story Structures
        • Branching Structures
        • Hero's Journey
        • Visual Novel in Unity-Links
    • Project 2 - Starter Assets
    • State Machine Framework
    • StateManager - Singleton Design Pattern
    • Interface IStateBase
    • Create SceneXState.cs
    • OptionPanel Prefab
      • UI Images: Sprite Sheets
      • Button Image-Transitions
    • Project 2 - List of Steps
    • Project 2 - Starter Code
  • Project 2 -Dialogue
    • Hide_Show_Panel Script
    • Edit OptionPanel
    • Simple DialogPrefab
    • Conversation Entry
    • SimpleDialog.cs
    • ScriptableObjects
      • Scriptable Object Factory
    • Conversation Scriptable Objects
    • DialogManager_ConvList
      • DialogManager V2
      • Coroutines: Dynamic Text
      • DialogPrefab wImage
  • Overview: Branching Logic
    • DialogTrigger
    • Dictionary Data-Structure
      • Unity PlayerPrefs Dictionary
    • GameData Version3
    • Dictionary: choiceData
      • SaveChoice, ChoicePanel
        • choiceData - examples
          • Dictionary Value to Disable Options
    • Configure UI-Button Listeners
      • NPC Animation
      • NPC Activation
    • UI-Triggered Animations
    • Simple Inventory
    • EndState Conditions
    • Script ExecutionOrder
    • Custom UnityEvents
    • PlayerStats v2
      • ModifyPlayerData
      • BuyItem
    • Text Input
  • UI Components
    • Finding Game Objects
    • Game Objects: UI vs. 2D Sprite
    • UI Elements
      • Canvas: Screen-Space Render-Mode
      • UI-Buttons To Change Scene
  • Proj4: Inventory System
    • Inventory-System
      • GameData version4
      • 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
  • Custom Unity Events
    • Event Publishing Patterns
    • Custom Event Messaging
  • Proj4: Mini-Game
    • Simplified Mini-Game
      • PlayerController_v2 Mods
        • PlayerController_v2_final
      • MiniGameManager_v2
    • MiniGame-Overview-Proj4
    • LevelManager
      • LevelManager Logic Diagram
      • LevelManager FSM
      • LoadLevel, StartLevel Logic
      • Code Framework
    • Timer
  • Project 4 - Code Mods
    • Project 4 - Steps
    • Project 4 - 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
      • Particle Systems
      • Adding Audio
      • Screen Fading and Reloading
      • ScriptableObjects
      • Disable Debug Logging
      • Events and Actions
      • Saving Data - Serialization
      • Parallax Scrolling
      • Change Sprites
    • XR - Extended Reality
  • Computing Concepts
    • Programming Patterns
      • State - FSM
      • Singleton Pattern
    • C# Language
      • Variables
      • Delegates
      • Dictionary
      • Enum
      • Encapsulation
        • C# Properties
        • Access Modifiers
      • Generics < T >
      • Inheritance
      • Interface
      • List< T >
      • Polymorphism
      • Queue< T >
      • Switch-Case
      • Foreach
      • Static
      • Ternary Operator: ?
      • this
    • Diagrams
      • State Machine Framework
      • UML Class Diagrams
      • Level Manager Logic Diagram
      • Flow-Chart: NumberGame
      • FSM: NumberGame
    • Tools
    • Glossary
    • References and Resources
    • Random Thoughts
Powered by GitBook
On this page
  • Create New Scene, Add Scene to Build Settings
  • Create OptionPanel Prefab: UI-Panel,2 UI-Buttons
  • Add Scene in Unity Build Settings
  • Add GameScene Enum to StateManager.cs script:
  • Create a C# SceneXState.cs Script
  • SceneXState Class Definition Logic:
  • OptionPanel 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

Create SceneXState.cs

PreviousInterface IStateBaseNextOptionPanel Prefab

Last updated 4 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 Project2. This assumes you have downloaded the Project2 starter-assets unity-package and installed the Unity package.

For Project 2: 5 Scenes, 5 SceneXState.cs Classes.

  • You will need 5 Scenes with 5 SceneXState Class Definitions.

  • 2 Scenes were provided in the StarterAssets: BeginScene, EndScene

  • 1 Scene is the MiniGameScene from Project 1 - you will need to create a MGSceneState.cs file.

  • You will create 2 additional Scenes - you will need to create 2 SceneXState.cs files for these scenes.

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. Save the Scene.

UI-Panel,2 UI-Buttons

After creating the OptionPanel prefab, you will drag an instance of this prefab onto the Canvas GameObject, so that the OptionPanel is a child of the Canvas. Do for each Scene that you create. You may need to add a UI-Canvas to your scene, remember to configure before adding the OptionPanel prefab to the Canvas.

Add Scene 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
}

Create a C# SceneXState.cs Script

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.

SceneXState Class Definition Logic:

  • 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 SceneXState : 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 SceneXState ()
    {
        scene = GameScene.SomeScene;
        Debug.Log("In Constructor for SomeState");
    }

OptionPanel 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.

Drag the OptionPanel prefab onto the Canvas. Eliminate

Add Code to BeginState for the new Button's Function

  • Declare Object-Reference Variable for a Button Component

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

public void InitializeObjectRefs ()
    {
        btnOption1 = GameObject.Find ("ButtonOption1").GetComponent<Button> ();
        btnOption1.onClick.AddListener (LoadSceneX); //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

StarterAssets
Create OptionPanel Prefab:
Screen-Space Camera