InstructionModel class

The InstructionModel contains a list of instructions. We defined the InstructionStep class previously, and now we define the InstructionModel class with our list of instructions. The app will then grab instructions one at a time from the model as the user steps through the instructions. At first, we'll just hardcode some example data. Soon, we'll load the data from the external CSV file instead.

Create the script now:

  1. In Project Assets/ChangeTire/Scripts, right-click and create a new C# Script.
  2. Name it InstructionModel.
  3. Open it for editing.

Like InstructionSteps, the InstructionModel does not inherit from MonoBehaviour. For now, its only property is a list of steps:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
public class InstructionModel {
[SerializeField] private List<InstructionStep> steps = new List<InstructionStep>(); public void LoadData() { steps.Add(new InstructionStep(new List<string> { "0", "Hello World!", "Intro body text." })); steps.Add(new InstructionStep(new List<string> { "1", "This is the first step", "Body text of first step" })); steps.Add(new InstructionStep(new List<string> { "2", "This is the second step", "Body text of second step" })); } }

We've also written a public LoadData() function that builds a list of steps. Eventually, we'll actually load this data from an external file, but to get started we are just calling our InstructionStep constructor directly with a hardcoded list of strings. We define three steps, numbered 0 through 2.

Keeping with our object-oriented practices, the steps list is private and we provide a getter method, GetInstructionStep(), to obtain a specific InstructionStep from the list. It checks the requested index to ensure it is valid.

    public InstructionStep GetInstructionStep(int index) { 
        if (index < 0 || index >= steps.Count) 
            return null; 
        return steps[index]; 
    } 
 
    public int GetCount() { 
        return steps.Count; 
    } 
 

We also add a function, GetCount, to get the number of steps presently in the list.

If you want to expose the steps list in Inspector for debugging, there's no need to change it from private to public. Use [SerializeField] instead. Declaring a variable as public means you expect other code to modify its value, which should be used judiciously, if at all. [SerializeField] tells Unity to expose it in the Editor Inspector, but does not violate object oriented principles.

Save your file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.196.59