Create SceneXState.cs
Last updated
Last updated
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 StarterAssets Unity package.
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.
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.
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 Screen-Space Camera before adding the OptionPanel prefab to the Canvas.
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
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.
Add 'using' directives for UnityEngine.UI to the top of the script file:
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.
Add code for IStateBase, Method: InitializeObjectRefs( )
Add Class Constructor, and set the corresponding GameScene Enum.
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
Declare Object-Reference Variable for a Button Component
Initialize the object reference in: InitializeObjectRefs( )
Write Custom Method to change scene and state
Use the Button to go to the new Scene, Add Images,2 Buttons to each of 5 scenes