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:
- In Project Assets/ChangeTire/Scripts, right-click and create a new C# Script.
- Name it InstructionModel.
- 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.
Save your file.