In this post, we will go over how to implement and support airtap gestures for your application. The airtap gesture is the simplest select gesture performed on the Hololens. More on gestures can be found here.
Configuring for airtap in Unity
This is one of the easier gestures to implement in Unity because we have already incorporated the InputManager. InputManager includes GesturesInput which contains scripts to detect hand gestures.
If you hit play in Unity, you will notice two hands that show up in the Game mode. This indicates that your application is already configured to accept tap, hold, and manipulation gestures. The left hand is selected by holding the left Shift key and the right hand by the Spacebar. Once a hand is selected, click using the left mouse button.
Creating a script to listen to gestures
Once again, in Assets→Scripts, create a new script and name it GestureAction. Open it in VS17.
This script imports from IInputClickHandler and requires a function OnInputClicked(InputClickedEventData eventData).
For this example, we will randomize the color of our sphere anytime the user performs an airtap gesture. Each game object has a Color component, accessed using GetComponent<Renderer>.material.color.
A new Color can be created on initialization by calling its constructor. A color is defined with 4 floats; components r,g, and b define the RGB space and alpha defines the transparency. We will randomize RGB and use 1.0 for alpha.
The complete code is shown below.
Test the script
Attach this script to the sphere in Unity. Hit play and test it. Does the color change?
Probably not. Don't panic! This makes complete sense. The Gaze functionality we developed forces the sphere's color (in my case, to blue). This overwrites any actions performed by the Gesture script. In the Inspector window for the sphere, disable Gaze Action and try it again. It should work this time round. The color of the sphere should randomize every time you click. Try it with both the "left" and "right" hand.
Fix the Gaze Script (optional)
We clearly don't want the Gaze and Gesture script to conflict. Instead of changing the color on gaze, we can change the metallic level of the object. Without going into too much detail, here's the code to do that in the GazeScript script.