Changing the color

So, the final lesson in this chapter will add interaction to change the color of our material. Instead of Gaze and Gesture though, we will use a voice command:

  1. With LeftRect selected, click on the Add Component button.
  2. Scroll down and click on the New Script option.
  3. Type ChangeRectColor in the new script dialog; then, click on Create and then Add.

Just like last time, instead of creating a second script for the right side, we will add the same script to RightRect using the following steps:

  1. Select RightRect.
  2. Click on the Add Component button.
  3. Click on Scripts.
  4. Select ChangeRectColor from the list.

This script will be slightly more complex than the previous one. From a scripting standpoint, materials are handled by the Renderer class. So, we get a reference to the renderer that is attached to this object. Then, we create a couple of bools.

We will use the OnFocusEvent.cs HTK class attached to an object and with two functions in our ChangeRectColor script, we can change the color of the object with OnGazeRectStart() when the user is looking at the object and OnGazeRectExit() when the user stops looking at the object. Then, we have two more public functions: VoiceSelect() and ResetColor(). All of these functions are public so that the OnFocusEvent and Speech Input Handler can use them; we will learn about this in a few minutes.

Here's a quick run through from the user's standpoint. The program starts and the user sees two boxes. If they look directly at either box, the box will turn green. If the user looks at one of the objects and gives the VoiceSelect() command, it will change color to blue. If the user looks at the box object and gives the ResetColor() command, the color will reset it to white.

The word select is used by the system as a vocal replacement or equivalent of Air tap. Saying select while looking at an object is exactly the same as Airtapping that object. In the case of this program, saying select causes the objects to start animating.

The following are a few other reserved words you should avoid using:

  • Place
  • Face me
  • Bigger/smaller

Now, let's double-click on the ChangeRectColor.cs in the Assets folder of the Project View so that Visual Studio opens up our new file.

Select and copy or type everything in the following code into ChangeRectColor.cs. I highly recommend that you type it; when we take the slow route in learning, we often start picking up things that we are not aware of:

using UnityEngine; 
using System.Collections; 

public class ChangeRectColor : MonoBehaviour { 

        Renderer rend; 
        bool selected; 
        bool gazed; 


       void Start () { 
        rend = GetComponent<Renderer>(); 
        bool selected = false; 
       } 

    public void OnGazeRectStart() 
    { 
        if (!selected) 
        {   rend.material.color = Color.green; 
            gazed = true; 
        }     
    } 
    public void OnGazeRectExit() 
    { 
        if (!selected) 
        {   rend.material.color = Color.white; 
            gazed = false; 
        } 
    } 
    public void VoiceColorChange()
    {
           rend.material.color = Color.blue;

    }
    public void ResetColor()
    {
          rend.material.color = Color.white;
          selected = false;
    }
    public void OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData)
    {
        var voiceCommand = eventData.RecognizedText.ToLower();

        switch (voiceCommand)
        {
           case "change color":
           {
              VoiceColorChange();
              break;
           }
          case "reset":
          {
              ResetColor();
              break;
          }
        default:
              break;
       }
   }

}

Okay, now that we have copied our code, save it by pressing Ctrl S.

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

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