Here, we will take our first steps into Visual Studio's code editing capabilities. As previously explained, this will be a very simple script with some, but with very little, explanation.
Now, double-click on the grayed-out RectAnim script to open Visual Studio:
Once it is loaded, your screen should look comparable to this:
You should see the RectAnim.cs filename highlighted in the left-upper corner. When a developer writes code, this is where we do it. Currently, you will see a couple of namespace declarations, the class name, and a few functions.
The Start() function is called when an object is first instantiated; it is one of the first things that happens. So, it is the setup for a scripted object in Unity.
The Update() function is called for every frame. When you need something to change on a regular basis, it is normally done through Update.
Now, replace all the code in RectAnim.cs with the following code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using HoloToolkit.Unity.InputModule; public class RectAnim : MonoBehaviour, IInputClickHandler { Animator anim; // Use this for initialization void Start () { anim = GetComponent<Animator>(); } // Update is called once per frame void Update () { if (!anim.GetBool("idle")) { anim.Play("RectMove"); } else if (anim.GetBool("idle")) { anim.Play("RectIdle"); } } public void OnInputClicked(InputClickedEventData eventData) { if (anim.GetBool("idle")) { anim.SetBool("idle", false); } else { anim.SetBool("idle", true); } } public void OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData) { var voiceCommand = eventData.RecognizedText.ToLower(); switch (voiceCommand) { case "change color": { break; } case "reset": { break; } default: break; } } }
The OnInputClicked(InputClickedEventData eventData) function is part of the HoloToolKit Input function. When an object is being looked at via Gaze and the user does the air-tap gesture, it will send the OnInputClicked() message to the object and run that code.
If we press the Play button:
We can see what appears to be a white bar against a black background:
Now, if we click on the two halves of the bar, we will see them move up and down:
If you see the blocks moving, congratulations on learning the basics of Unity animation.