2. Initializing your project and first application
In this first lesson, you'll learn about some of the capabilities the Mixed Reality Toolkit (MRTK) has to offer, start your first application for the HoloLens 2, and deploy it to the device.
- Optimize Unity for HoloLens development.
- Import assets and setup the scene.
- Visualization of the spatial mapping mesh, hand meshes, and the framerate counter.
Create new Unity project
Enter a project name (e.g. "MixedRealityBase").
Enter a location to save your project.
Make sure the project is set to 3D.
Click Create Project.
Configure the Unity project for Windows Mixed Reality
Open the Build Settings window by going to File > Build Settings.
Select the Universal Windows Platform and click the Switch Platform button to switch platforms. Applications running on HoloLens 2 are required to be Universal Windows Platform (UWP) compatible.
Enable virtual reality by clicking the Player Settings button in the Build Window, and enable the Virtual Reality Supported checkbox under XR Settings from the inspector panel, as shown in the image below. Note that you might need to drag the Build Settings window out of the way in order to see the inspector panel. The Virtual Reality Supported checkbox also applies to Mixed Reality and Augmented Reality headsets because it refers to the enabling of stereoscopic vision (rendering different images for each eye.)
Also under XR Settings, change the Stereo Rendering Mode to Single Pass Instanced. This rendering pipeline style is generally most performant for HoloLens 2. If interested in other performant configurations for your Unity environment, follow these instructions.
From the same inspector panel, ensure that the Spatial Perception checkbox in the capabilities section is enabled under Publishing Settings. Spatial Perception allows us to visualize the spatial mapping mesh on a mixed reality device, such as HoloLens 2. Publishing Settings are found in the inspector panel, above XR Settings and under Other Settings.
While not used in this section, some other common capabilities you might want to enable include the Microphone for voice commands, and InternetClient for connecting to services requiring a network connection.
Import the Mixed Reality Toolkit
Import the Mixed Reality Toolkit package that you downloaded in the previous step. Start by clicking on Assets > Import > Custom Package, select Microsoft.MixedReality.Toolkit.Unity.Foundation.2.1.0.unitypackage and open it to begin the importing process. Allow a few minutes for the importing process to complete.
In the next pop-up window, click Import to begin importing the selected package into the Unity project. Ensure all items are checked as shown in the image.
If you see a pop-up dialog box asking to apply the Mixed Reality Toolkit default settings, click Apply. MRTK analyzes your project for any missing recommended settings when imported for automated setup. Depending on your settings, the pop-up might look different than the image below.
Configure the Mixed Reality Toolkit
Add the Mixed Reality Toolkit to your current scene by selecting Mixed Reality Toolkit > Add to Scene and Configure.. from the menu bar. If you don't see this menu item after importing the mixed reality toolkit, restart Unity.
You may see a pop-up dialog box for selecting a profile for the Mixed Reality Toolkit. Choose the profile named DefaultHoloLens2ConfigurationProfile by double-clicking it.
Your scene will have several new items and modifications. Save your scene under a different name by clicking File > Save As..., and give your scene a name, such as BaseScene. Keep your scene organized by saving it to the Scenes folder in your project’s Assets folder.
Build your application to your device
If you closed the Build Settings window from the previous sections, open the Build Settings window again by going to File > Build Settings.
Ensure the scene you just created is in the Scenes in Build list by clicking on the Add Open Scenes button while your scene is open in Unity.
Press the Build button to begin the build process.
Create and name a new folder for your application. In the image below, a folder with the name App was created to contain the application. Click Select Folder to begin building to the newly created folder. After the build has completed, you can close the Build Settings window in Unity.
If the build fails, try building again or restarting Unity and building again. If you see an error, such as "Error: CS0246 = The type or namespace name “XX” could not be found (are you missing a using directive or an assembly reference?). If so, you might need to install Windows 10 SDK (10.0.18362.0)
After the build is completed, open the newly created folder containing your newly built application files. Double-click on the MixedRealityBase.sln solution, or the corresponding name, if you used an alternative name for your project, to open the solution file in Visual Studio.
Be sure to open the newly created folder (i.e. the App folder, if following the naming conventions from the previous steps), as there will be a similarly named .sln file outside of that folder, that is not to be confused with the .sln file inside the build folder. The folder structure should look similar to the image below.
If Visual Studio asks you to install new components, take a moment to ensure that all prerequisite components are installed as specified in the "Install the Tools" page
Connect your HoloLens 2 into your PC. While these instructions assume you will be deploying to a HoloLens 2 device, you might also choose to deploy to the HoloLens 2 emulator or choose to create an app package for sideloading
Before building to your device, the device must be in Developer Mode and paired with your development machine. Both of these steps can be completed by following these instructions
Configure Visual Studio for building to your HoloLens 2 by selecting the Release or Master configuration, the ARM architecture, and Device as target.
The final step is to build and deploy to your device by selecting Debug > Start without debugging. Selecting Start without Debugging causes the application to immediately start on your device upon a successful build, but without the debugger attached and information appearing in Visual Studio. This also means that you can disconnect your USB cable while your application is running on your HoloLens 2 without stopping the application.
You might also select Build > Deploy Solution to deploy to your device without having the application automatically start.
You have now deployed your first HoloLens 2 application. As you walk around, you should see a spatial mapping mesh covering all the surfaces that have been perceived by the HoloLens 2. Additionally, you should see indicators on your hands and fingers for hand tracking and a frame rate counter for keeping an eye on application performance. These are just a few of the foundational pieces, included out of the box, with the Mixed Reality Toolkit. In the lessons to come, you will start adding more content and interactivity to your scene so that you can fully explore the capabilities of HoloLens 2 and the Mixed Reality Toolkit.
In the app, you may notice the visual profiler. You will cover how to toggle the frame rate counter using a voice command in Lesson 5. It is generally recommended to keep the visual profiler visible at all times during development to understand when code changes may have impacted perf. HoloLens 2 application should continuously run at 60 FPS.