How to build and run a HoloLens app in Unity using the Emulator


Here's how to set up a development environment to write HoloLens apps, even if you don't have a HoloLens. You will need a PC running the latest Windows 10, a game controller and a copy of Unity. The free Personal edition of Unity, will work fine.

It won't do any harm to have Visual Studio installed too as it makes a good C# editor (along other things 😉 ). You can also use Visual Studio Code for editing.

  • Install the latest version of Unity.

    IMPORTANT:  During installing, make sure to select Windows Store .NET Scripting Backend. If you omit this, (you will know you have if you find various menu options discussed later are missing) you will not be able to build UWP apps, and that means HoloLens apps. If you discover that you did forget to install it, you can re-launch the Unity Download Assistant (it will be in your Downloads folder) and check the box.

  • Create a new 3D project.
  • Add something to the scene to look at - a Cube for example. When you've done this, click File / Save Scene to save this view.
  • Select the Main Camera, and in the Inspector, change Background from SkyBox to Solid Color, and make the color (0,0,0). When drawing Holograms, you don't want any background blocking your view of the real world.

    hl1

  • Go File / Build Settings and select Windows Store for Platform. Then click Switch Platform to make it stick. Can't do this? See Step 1.

hl2

  • Click Add Open Scenes so there is something to build.
  • For Target Device, click the pull-down and select HoloLens.
  • Click Player Settings and in the view that opens on the right, expand Other Settings and check Virtual Reality Supported. Windows Holographic should be selected as the Virtual Reality SDK.

    hl3

  • Close the Build Settings window
  • Click on the menu option Window / Holographic Emulation. If you have followed the previous steps, you'll now have the option of selecting the Emulation Mode. Change it to Simulated in Editor.

    hl4

  • Click on the Unity Play button, and the app will start. Use the game controller to look around the scene. Admittedly, the scene isn't very exciting at this point, as it's one white cube in a vast empty universe.
  • Let's make things a little more interesting by adding some environmental details. HoloLens uses technique called Spatial Mapping to capture the user's surroundings. Select the Main Camera, and from the Component menu, go to AR and Spatial Mapping Renderer.

    hl5

  • View the Spatial Mapping Renderer's properties in the Inspector, and change Render State from Occlusion (which simply blocks you from seeing objects that are hidden) to Visualization (which draws the 'fake' spatial data).
  • Ensure the Window / Holographic Emulation window is open, select a room, and click Play again. You should be able to walk around the virtual room using the game controller. The room data will update at a similar rate to a real HoloLens, so there will be a tiny delay before the room appears.

hl6

Interacting with the Spatial Room Data

 

    1. The spatial data we added isn't just for looks either: we can make our virtual objects interact with it. First, select Main Camera again, and go to ComponentsAR and now add Spatial Mapping Collider.
    2. Next we'll create a ball object which we can throw around. We'll actually create a Unity prefab object which is a template from which multiple objects can be quickly created.
    3. Start by creating sphere 3D object, and change its scale to be (0.1, 0.1, 0.1).
    4. Select the sphere, and in the Inspector panel click on Add Component, and add Physics / Rigidbody. This provides the sphere with some physical properties, so it can bounce off things.
    5. Drag the sphere you created from the Scene hierarchy view on the left, to the Assets view at the bottom of the screen. This creates a Prefab object from the sphere. You can now click on the sphere in the hierarchy view and delete it from the scene. We'll create the spheres programmatically in a script instead, based on the prefab.
    6. In the Assets view, right click and select Create / C# Script. Double-click the script object, and it'll open in whatever the default editor happens to be.
    7. Edit the script to read like this:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallShooter : MonoBehaviour {
    public Rigidbody ball;
    void Start () {
}

// Update is called once per frame

void Update () {
        if (Input.GetButton("Fire1") )
        {
            Rigidbody clone = (Rigidbody) Instantiate(ball, transform.position, transform.rotation);
            clone.velocity = transform.forward * 10f;
            Destroy(clone.gameObject, 3);
        }
    }
}

 

  1. Save the script, and return to the Unity editor. There are just a few more steps we need to do to wrap this up: specifically, linking the sphere prefab object with the reference to the public Rigidbody ball object declared in the script.
  2. First, drag the script from Assets onto the Main Camera object so that it will actually be called. Once you do this, select the Main Camera to see script listed in the Inspector.
  3. From the Assets panel, drag the sphere object to the Inspector view and drop it where it says Ball.

    hl7

  4. Now when you click Play, press A on the game controller and you will find yourself throwing multiple balls into the scene. Notice how they bump and roll over the wireframe walls and furniture in the view.
    At this point a light bulb may be going on over your head: yes, when you are using a real HoloLens, the room data will be the room you are actually standing in, and yes, your game objects will bounce off the actual furniture in your actual room. Isn't that wild?
    hl8

Comments (0)

Skip to main content