Adding voice commands
Another way to interact with your holograms is via voice commands. Hololens supports a variety of voice commands with the help of Cortana. Apart from the standard commands, you can program your own keywords to specify certain interactions with holographic objects.
Create a new Speech Manager
The first thing to do here is create a new empty object in the Hierarchy under Managers and name it SpeechManager.
Search for the SpeechInputSource script in Assets and drag it into this new object. For this example, we are going to change the size of the sphere by either a) Increasing size b) Decreasing size or c) Going back to default size.
In the Inspector window of the SpeechManager object, expand the Keywords menu under Speech Input Source. Let's add some keywords that our application can recognize. Click the + button and add 3 keywords: Increase size, Decrease size, Default size. I also added some options under Key Shortcut to allow us to test the speech recognition script that we will develop through Unity
Create a script to recognize keywords
This step involves creating a new C# script to perform certain tasks on the sphere once a voice command from the list created above is recognized.
Under Assets→Scripts, create a new C# script and name it SpeechAction. Open it in VS 2017. The SpeechInputSource script inherits from ISpeechHandler. Similar to what we did for Gaze, our new script will also inherit from ISpeechHandler and, in the header, we will add the command using HoloToolkit.Unity.InputModule to import this functionality.
The function that is inherited is OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData
Whenever a voice command is issued while gazing at the holographic object, this function is called. Within this function, we can parse the input and, based on what the user has said, we can call the appropriate sizing function on the object.
The code snippet to parse the input is shown in the screenshot below, but first let's discuss how to change the size of the object. The object has a transform component that has a Vector3 localScale parameter. This represents the size of the object in x, y, and z.
To change the size of the object, we have to simply manipulate this parameter. We can create two SerializeFields that hold scaling factors for increasing and decreasing the object. These values can be manipulated from the sphere's inspector window once this script is attached to it.
Pro Tip: You generally shouldn't need to use SerializeFields. For public variables, Unity automatically serializes them in the object's Inspector window. Using SerializeFields for private variables will also show them in the Inspector window.
In order to remember the default object size, we can save this parameter to a private variable in the void Start() function, which is called at the beginning only, during object creation.
The complete script is shown below. Remember to drag it to the sphere to attach it.
Testing the Script
Hit play in Unity to test the script. If you do not have a microphone configured to receive input in Unity (I don't) you can use the key shortcuts instead. In my project, 0 = Increase size, 1 = Decrease size, and 2 = Default size.
This simple example hopefully shows how voice commands can be used to interact with your holograms. Try something else like changing the color of the object instead, by assigning a new color to gameObject.GetComponent<Renderer>().material.color.
Leave a Reply.