I have recently started to develop for Microsoft Hololens and there is a really useful toolkit called Holotoolkit. In this post I will show how to integrate this toolkit to Unity and how to use the input module. I assume that you have learned the basics of how to deploy an app for Hololens at Holograms 100 course.

The steps for unity integration:

Make sure you have an Unity version 5.5.0 or above Download the Unity Package from the repository. In Unity install the Unity package by: Assets -> Import Package -> Custom Package… and then select the package that you have downloaded. Remove the Main Camera from the scene and add the prefabs: HoloToolkit/Input/Prefabs→ HoloLensCamera.prefab HoloToolkit/Input/Prefabs/Cursor → DefaultCursor.prefab Create an empty game object in the scene called ‘Managers’ and add the prefab under this game object: HoloToolkit/Input/Prefabs → InputManager.prefab

Input events

Now your game is ready to handle all the hololens gesture events such as: click and navigation.

Click event occurs, when the user does the tap gesture or presses the clicker.

Navigation event occurs, when the user does the pinch gesture and moves her hand. Navigation event gives the information of relative movement of the hand according to the gestures starting point.

There are more events available that you can use in your game but in this post I will focus only on this two events. For further information please check this page out.

Handling the events

Microsoft have a course called Holograms 211 to teach how to handle gesture events, but this has not been updated yet for the latest Holotoolkit. But it is a good resource to learn more about the available input gestures.

We will create two cubes to learn how to handle the aforementioned events. Create two cubes in the scene and attach one of them ClickExample.cs component and the other NavigationExample.cs. These scripts are available below.

ClickExample.cs:

This script changes the color of the cube, when it is selected. It is a good test to understand, if everything works fine.

using UnityEngine; using HoloToolkit.Unity.InputModule; public class ClickExample : MonoBehaviour, IInputClickHandler{ private Color[] colors = new Color[]{Color.red, Color.cyan}; private int i = 0; private Renderer rend; void Start () { rend = GetComponent<Renderer>(); } //here we learn that this game object is clicked public void OnInputClicked(InputEventData eventData) { //here we change the color of the cube rend.material.color = colors[i]; //index is calculated with mode operation so that //it stays in the size of the array i = (i+1) % colors.Length; } }

This script is implementing the IInputClickHandler interface. When the gaze cursor (in our case DefaultCursor.prefab) is intersected with a game object’s collider and the user clicks it, then the InputManager looks for this event handler on this game object.

NavigationExample.cs

This script rotates the cube on its y-axis, when the user pinches their hand and moves their hand to left or right.

//This code is adopted from Holograms 211 course https://developer.microsoft.com/en-us/windows/mixed-reality/holograms_211 //Originally part of this code is in GestureAction.cs inside this course. using UnityEngine; using HoloToolkit.Unity.InputModule; public class NavigationExample : MonoBehaviour, INavigationHandler { [Tooltip("Rotation max speed controls amount of rotation.")] public float RotationSensitivity = 10.0f; private float rotationFactor; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void OnNavigationStarted(NavigationEventData eventData) { Debug.Log("Navigation is started"); } public void OnNavigationUpdated(NavigationEventData eventData) { rotationFactor = eventData.CumulativeDelta.x * RotationSensitivity; //transform.Rotate along the Y axis using rotationFactor. transform.Rotate(new Vector3(0, -1 * rotationFactor, 0)); } public void OnNavigationCompleted(NavigationEventData eventData) { Debug.Log("Navigation is completed"); } public void OnNavigationCanceled(NavigationEventData eventData) { Debug.Log("Navigation is canceled"); } }

This script is implementing INavigationHandler interface. Same as previous script, when the user does the navigation input, InputManager looks for these methods at the focused object. CumulativeDelta gives the relative position of the hand according to gesture start point.

Troubleshooting