Creating the controller script

In this section, we are going to create a script that will control the scene and allow our users to change the textures of the seats. Let's get started:

  1. In @MyAssets|Scripts, create a new C# script by right-clicking and selecting Create|New C# Script. Call it MainController and double-click on it to open it in Visual Studio:
Creating a new C# Script in the @MyAssets/Scripts folder
  1. If you installed Unity by following the steps in Chapter 2, Introduction to Unity for AR Development, you will already have Visual Studio installed and configured, and it will open the script with the default code, as shown in the following screenshot:

MainController script in Visual Studio
  1. If you installed Visual Studio prior to Unity or used another program such as MonoDevelop, you can configure Unity to open scripts with it by clicking on Edit|Preferences|External Tools|External Script Editor, as shown in the following screenshot:

Preferences window with Visual Studio assigned as the External Script Editor
  1. In the script, start by adding the following lines after the class declaration:
public class MainController : MonoBehaviour
{
public Material[] materials;
public Texture2D[] textures;
...

Here, we are declaring the materials array, which is where we will store the materials of both chairs. We will use it to change those materials' texture properties. The textures array will contain the actual images (red and blue) that we will apply to those materials. Both variables are public because we will initialize them from the Unity editor.

  1. Inside the Start() method, add the following loop:
foreach (Material material in materials)
{
material.mainTexture = textures[0];
}

With this loop, we are assigning each material inside the materials array to the first image of the textures array.

  1. After the Update() method, we are going to create a new method:
public void ChangeColor()
{
foreach (Material material in materials)
{
if (material.mainTexture == textures[0])
material.mainTexture = textures[1];
else
material.mainTexture = textures[0];
}
}

This method changes the texture (red or blue) of the materials of the chairs. It checks which texture is selected and assigns the other one to both chairs.

  1. Now, go back to the Unity editor and drag the script to the EasyAR_Setup GameObject so that the script will affect the current scene. 
Remember that a script will only be executed if it's attached to one GameObject (or more) of the scene. Otherwise, it will only exist in the Projects window but not in the actual scene.

Since this script doesn't make direct reference to the element it's attached to, it could go on any element that is active in the scene all the time (so it's always available). We have put it in EasyAR_Setup because it's a root element that fulfills this rule.
  1. Unfold the two variables of the script, that is, Materials and Textures, and, in Materials, set Size to 2 and select tela and Material #5. These are the materials that correspond to the fabric in each model:

Assigning the materials to the variables
  1. In Texturesset Size to 2 and select the red and the blue elements, that is, Acapulco 3011 and Acapulco PANTONE 2935 C:
 
Assigning the textures to the variables

The Controller script is ready. Now, we have to create the user interface and the button that will trigger the color change through the ChangeColor() method we have just created.

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

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