Most of the HoloLens development guidance you’ll find out there, will tell you that when you build you HoloLens app from Unity you must select D3D and not XAML in the project build settings.
You might think (as I did for a while) that it’s very obvious why and that this makes the app run in 3D rather than a 2D app. But that’s not true, the 2D vs 3D aspect of a HoloLens app built with Unity lies in the Virtual Reality Enabled flag that you can find in the Player Settings window. You need to set that to Windows Holographic for your app to behave like a nice volumetric, 3D application.
Let’s take a look.
To find out I’ve generated the same app both as XAML and D3D and here’s what I found.
Looking at the folder structure, we notice that they are almost identical, but where the D3D version has only one relevant class -
App.cs, the XAML one also has a
App.Xaml and a
MainPage.Xaml with its code behind pretty much like any blank project you’d create.
csproj files are pretty much identical barring from the included files mentioned above. Nothing fancy there.
Digging in, let’s see how the D3D version is set up. The
App.cs class implements
IFrameworkView so it’s just a simple CoreWindow shell that gets passed into Unity via Unity’s AppCallbacks class. What it means is that all we’re doing in the UWP side of the application is we create the most basic Window possible and send it to Unity to manage its contents, lifecycle and fill it with holographic goodness.
On the XAML side, the code looks a lot more like a regular UWP app. With some very small exceptions, where an instance of the AppCallbacks class is created and initialized, the code runs normally and navigates to the MainPage.
The contents of the MainPage are the interesting bits. Its contains only a SwapChainPanel which is a basic DirectX rendering surface. This gets passed to the AppCallbacks instead of the CoreWindow in the D3D case so this is how Unity takes over and renders the same holographic goodness.
One of the most common reasons that you’d want this is if you need the HoloLens keyboard to allow users to enter some simple data. Because the keyboard is a XAML control it cannot be rendered in the 3D volumetric space. Also, because as we’ve seen, the D3D option initialized just a CoreWindow and passes it to Unity, there is no container for that XAML control to be shown. This is why we need to start from the XAML Application Window.
The code to actually show the keyboard is not complicated, and it’s properly explained here.
Not that I’m saying to always use the XAML project type, but don’t be afraid to use it if you need it. Other than a very small flicker at the start of your app, where you can see the main window for a brief frame, there are no other performance traps that I’ve noticed. Both apps D3D and XAML will run at the same framerate for the same app.
Feel free to leave any comments if you think I missed something.