Working with the HoloLens sample in Unity

The Vuforia HoloLens Sample project provides pre-configured Unity scenes and project settings that you can use as a reference and starting point for your own AR apps that use Vuforia Engine. This document will show you how to author a Vuforia Engine scene for HoloLens, and how to customize event handling to implement custom app behaviors.

Also see: Developing Vuforia Engine Apps for HoloLens

Supported Versions

See: Vuforia Engine Supported Versions

Getting started with the Sample

Importing the sample

Get the Vuforia HoloLens Sample from the Unity Asset Store and import it into a new Unity project:

Vuforia Image

Building and executing the sample

The HoloLens sample comes with all project settings pre-configured that are required to build for HoloLens. When building a new HoloLens project from scratch, please see: Developing Vuforia Apps for HoloLens.

  1. Set your platform build target for Universal Windows Platform in File > Build Settings.
Vuforia Image
  1. Select the following platform build configuration settings
    SDK
     = Latest Installed
    UWP Build Type
     = D3D
  2. Define a unique Product Name, in Player Settings, to serve as the name of the app when installed on the HoloLens.
  3. Check Virtual Reality Supported and Vuforia Augmented Reality options, and set Virtual Reality SDKs to Windows Mixed Reality in "Player Settings > XR Settings".
  4. Select Build to generate a Visual Studio project
  5. Build the executable from Visual Studio and install it on your HoloLens.

See: https://developer.microsoft.com/en-us/windows/holographic/exporting_and_building_a_unity_visual_studio_solution

Visual Studio Build Configuration

Set your build target for x86.

Vuforia Image

Scene elements & their configuration

The HoloLens sample uses the ARCamera, ImageTarget, and ModelTarget prefabs. These can be found in the project's Vuforia folder along with the other assets and resources in this sample.

The sample's scene Hierarchy demonstrates how to set up a Vuforia Engine scene for HoloLens in Unity.

Vuforia Image

Scene Hierarchy

ARCamera GameObject

The ARCamera is the scene camera rig for Vuforia Engine apps in Unity. It defines the properties of both of its child scene cameras, as well as the device camera and rendering behavior of the scene.

Vuforia Image

Vuforia Engine Behaviour

World Center Mode defines which object in the scene hierarchy will serve as the world origin (0,0,0) of the scene's world space. When you select the CAMERA, the scene is rendered within the camera coordinate frame. This is the recommended World Center for Vuforia Engine when developing a HoloLens app.

The Open Vuforia Configuration button allows for quick access to the Vuforia Engine Configuration panel.

Voice Commands

The Voice Commands script on the ARCamera defines various voice commands available in the sample. Voice commands use the HoloLens Keyword Recognizer.

ImageTarget GameObjects

The ImageTarget GameObject encapsulates the ImageTargetBehavior and DefaultTrackableEventHandler. These are the primary script components that you will use to customize ImageTargets in a HoloLens application.
 

Vuforia Image

Image Target Behaviour

Database identifies the database that contains the Image Target that will be assigned to this Image Target Behaviour

Image Target defines which target in the database to assign to this Image Target Behaviour

Default Trackable Event Handler
The Default Trackable Event Handler component is responsible for handling callbacks to the Image Target Behaviour arising from changes in the state of the Image Target, such as when it has been detected and is then being tracked.

This script is used to enable and disable rendering and collision detection on digital content that is a child of the target. Extend this script's OnTrackingFound() and OnTrackingLost() methods to implement custom event handling for your app.

    protected virtual void OnTrackingFound()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);

        // Enable rendering:
        foreach (var component in rendererComponents)
            component.enabled = true;

        // Enable colliders:
        foreach (var component in colliderComponents)
            component.enabled = true;

        // Enable canvas':
        foreach (var component in canvasComponents)
            component.enabled = true;
    }


    protected virtual void OnTrackingLost()
    {
        var rendererComponents = GetComponentsInChildren<Renderer>(true);
        var colliderComponents = GetComponentsInChildren<Collider>(true);
        var canvasComponents = GetComponentsInChildren<Canvas>(true);

        // Disable rendering:
        foreach (var component in rendererComponents)
            component.enabled = false;

        // Disable colliders:
        foreach (var component in colliderComponents)
            component.enabled = false;

        // Disable canvas':
        foreach (var component in canvasComponents)
            component.enabled = false;
    }