This tutorial will walk you through creating a basic holographic app built with Unity. This project can then serve as a starting template for any holographic app you might want to build in Unity.
Chapter 1 - Create a New Project
To build an app with Unity, you first need to create a project. A project is a folder that holds all assets you import from digital content creation tools such as Maya, Max Cinema 4D or Photoshop, all code you create with Visual Studio or your favorite code editor, and any number of content files that Unity creates as you compose scenes, animations and other Unity asset types in the editor. Unity projects do not rely on a centralized project file like Visual Studio's solution file (*.sln) - they are just based on folder structure.
Unity project script files are also not managed via a central project file (*.csproj in Visual Studio). Unity automatically discovers C# files in the project folder. To build and deploy UWP apps, Unity can export the project as a Visual Studio solution that will contain all necessary asset and code files.
- Start Unity
- Select New
- Enter a project name (e.g. "HelloHolograms")
- Enter a location to save your project
- Ensure the 3D toggle is selected
- Select Create project
Chapter 2 - Setup the Camera
The Unity Main Camera handles head tracking and stereoscopic rendering. There are a few changes to make to the Main Camera to use it with HoloLens.
First, it will be easier to lay out your app if you imagine the starting position of the user as (X: 0, Y: 0, Z: 0). Since the Main Camera is tracking movement of the user's head, the starting position of the user can be set by setting the starting position of the Main Camera.
- Select Main Camera in the Hierarchy panel
- In the Inspector panel, find the Transform component and change the Position from (X: 0, Y: 1, Z: -10) to (X: 0, Y: 0, Z: 0)
Second, the default Camera background needs to be changed. In holographic applications, the real world should appear behind everything the camera renders, not a Skybox texture.
- With the Main Camera still selected in the Hierarchy panel, find the Camera component in the Inspector panel and change the Clear Flags dropdown from Skybox to Solid Color.
- Select the Background color picker and change the RGBA values to (0, 0, 0, 0)
Finally, to prevent holograms from being rendered too close to the users eyes as a user approaches a hologram or a hologram approaches a user, the near clip plane can be set to the HoloLens recommended 0.85 meters.
- With the Main Camera still selected in the Hierarchy panel, find the Camera component in the Inspector panel and change the Near Clip Plane field from the default 0.3 to the HoloLens recommended 0.85.
Chapter 3 - Create a Hologram
Creating a hologram in your Unity project is just like creating any other object in Unity. Placing a hologram in front of the user is easy because Unity's coordinate system is mapped to the real world - where one meter in Unity is approximately one meter in the real world.
- In the top left corner of the Hierarchy panel, select the Create dropdown and choose 3D Object > Cube.
- Select the newly created Cube in the Hierarchy panel
- In the Inspector find the Transform component and change Position to (X: 0, Y: 0, Z: 2). This positions the cube 2 meters in front of the user's starting position.
- In the Transform component, change Rotation to (X: 45, Y: 45, Z: 45) and change Scale to (X: 0.25, Y: 0.25, Z: 0.25). This scales the cube to 0.25 meters.
- To save the scene changes, select File > Save Scene, name the scene, and select Save
Chapter 4 - Export from Unity to Visual Studio
The Unity work on HelloHolograms is now complete, so the project is ready to be exported to Visual Studio and then to your HoloLens.
Unity performance settings
Unity quality settings
Since maintaining high framerate on HoloLens is so important, we want the quality settings tuned for fastest performance. For more detailed performance information, Performance recommendations for Unity
- Select Edit > Project Settings > Quality
- Select the dropdown under the Windows Store logo and select Fastest. You'll know the setting is applied correctly when the box in the Windows Store column and Fastest row is green.
Unity build settings
We want to specify our Unity project to export as a Universal Windows Platform app.
- Select File > Build Settings...
- Select Windows Store in the Platform list.
- Set SDK to Universal 10
- Set Build Type to D3D.
Specify the export for Windows Holographic
We need to let Unity know that the app we are trying to export should create a holographic view instead of a 2D view. We do that by adding HoloLens as a virtual reality device.
- From the Build Settings... window, open Player Settings...
- Select the Settings for Windows Store tab
- Expand the Other Settings group
- In the Rendering section, check the Virtual Reality Supported checkbox to add a new Virtual Reality Devices list and confirm "Windows Holographic" is listed as a supported device.
Export the Visual Studio solution
Now your Unity project has all the settings applied to be properly exported as a Visual Studio solution for a HoloLens app.
- Return to the Build Settings window.
- Click Add Open Scenes to add the scene
- Check Unity C# Projects under "Windows Store" build settings.
- Click Build
- Click New Folder and name the folder "App".
- With the App folder selected, click the Select Folder button.
- When Unity is done building, a Windows File Explorer window will appear.
- Open the App folder in File Explorer.
- Open the generated Visual Studio solution (HelloHolograms.sln in this example)
Target the app to Windows.Holographic
By default, Universal Windows Platform apps exported from Unity will run on any Windows 10 device. For Holographic applications, we are taking advantage of some features that are only available on HoloLens. If you aren't building in fallback behavior for non-HoloLens devices, it's best to target the app to only run on Holographic devices.
- In Visual Studio, right click on Package.appxmanifest in the Solution Explorer and select View Code
- Find the line specifying TargetDeviceFamily and change Name="Windows.Universal" to Name="Windows.Holographic".
- On the same line, change MaxVersionTested="10.0.10240.0" to MaxVersionTested="10.0.10586.0"
- Save the Package.appxmanifest.
Chapter 5 - Build and Deploy
All that's left is to build the Unity exported project, deploy it, and try it out on the device.
- Using the top toolbar in Visual Studio, change the target from Debug to Release and from ARM to X86.
Now the instructions differ for deploying to a HoloLens versus the emulator. Follow the instructions that match your setup.
HoloLens over Wi-Fi
- Click on the arrow next to the Local Machine button, and change the deployment target to Remote Machine.
- Enter the IP address of your HoloLens device and change Authentication Mode to Universal (Unencrypted Protocol).
- Select Debug > Start without debugging. If this is the first time deploying to your device, you will need to pair it with Visual Studio.
HoloLens over USB
- Click on the arrow next to the Local Machine button, and change the deployment target to Device.
- Select Debug > Start without debugging
- Click on the arrow next to the Device button, and select HoloLens Emulator.
- Select Debug > Start without debugging
Try out your app
Now that your app is deployed, try moving all around the cube and observe that it stays in the world in front of you.