InstructionStep class

We'll get started by creating a new C# script named InstructionStep, which will basically be a data structure or container for a row of data from our spreadsheet (in CSV format), including fields the for title, body text, image, and video.

  1. In the Project Assets/HowToChangeATire/Scripts folder, right-click and create a new C# Script and name it InstructionStep.
  2. Open it for editing.

When Unity creates a new script it uses a default template for a typical object class derived from MonoBehaviour. We want this to be just a simple object and do not want it to be a MonoBehaviour (and do not need the Start/Update functions).

File: InstructionStep.cs 
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
public class InstructionStep { 
    public string Name; 
    public string Title; 
    public string BodyText; 
} 

Now we will define a constructor for this class. A constructor is a public method with the same name as the class itself, which initializes the property values of a new instance of the class. This constructor will receive the list of value strings as its argument and assign the value to the corresponding properties. Add the following code to your class:

    private const int NameColumn = 0;
private const int TitleColumn = 1; private const int BodyColumn = 2; public InstructionStep(List<string> values) { foreach (string item in values) { if (values.IndexOf(item) == NameColumn) { Name = item; } if (values.IndexOf(item) == TitleColumn) { Title = item; } if (values.IndexOf(item) == BodyColumn) { BodyText = item; } } }

The constructor will expect a list of string values where the first element is the step number (index 0), the second is the title (index 1), and the third is the body text (index 2). Not coincidentally, this is also how the columns are set up in our CSV data and spreadsheet. There may be additional elements of the array, but we'll ignore them for now.

That's it, for now. Save the file. Later we'll add fields for image and video file names.

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

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