Quickstart: Create a Unity HoloLens app that uses Azure Spatial Anchors
In this quickstart, you'll create a Unity HoloLens app that uses Azure Spatial Anchors. Spatial Anchors is a cross-platform developer service that allows you to create mixed reality experiences with objects that persist their location across devices over time. When you're finished, you'll have a HoloLens app built with Unity that can save and recall a spatial anchor.
You'll learn how to:
- Create a Spatial Anchors account.
- Prepare Unity build settings.
- Configure the Spatial Anchors account identifier and account key.
- Export the HoloLens Visual Studio project.
- Deploy the app and run it on a HoloLens device.
To complete this quickstart:
- You need a Windows computer on which Unity 2019.1 or later and Visual Studio 2019 or later are installed. Your Visual Studio installation must include the Universal Windows Platform development workload and the Windows 10 SDK (10.0.18362.0 or newer) component. You must also install Git for Windows.
- You need a HoloLens device on which developer mode enabled. Windows 10 October 2018 Update (also known as RS5) must be installed on the device. To update to the latest release on HoloLens, open the Settings app, go to Update & Security, and then select Check for updates.
- On your app, you need to enable the SpatialPerception capability. This setting is in Build Settings > Player Settings > Publishing Settings > Capabilities.
- On your app, you need to enable Virtual Reality Supported with Windows Mixed Reality SDK. This setting is in Build Settings > Player Settings > XR Settings.
Create a Spatial Anchors resource
Go to the Azure portal.
In the left navigation pane in the Azure portal, select Create a resource.
Use the search box to search for Spatial Anchors.
Select Spatial Anchors. In the dialog box, select Create.
In the Spatial Anchors Account dialog box:
Enter a unique resource name, using regular alphanumeric characters.
Select the subscription that you want to attach the resource to.
Create a resource group by selecting Create new. Name it myResourceGroup and select OK. A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed. For example, you can choose to delete the entire resource group in one simple step later.
Select a location (region) in which to place the resource.
Select New to begin creating the resource.
After the resource is created, Azure Portal will show that your deployment is complete. Click Go to resource.
Then, you can view the resource properties. Copy the resource's Account ID value into a text editor because you'll need it later.
Under Settings, select Key. Copy the Primary key value into a text editor. This value is the
Account Key. You'll need it later.
Download and open the Unity sample project
Clone the samples repository by running the following command:
git clone https://github.com/Azure/azure-spatial-anchors-samples.git
In Unity, open the project in the
Unity folder. Unity might prompt you about a different Unity version between the project and the one you've installed on your machine. This warning is okay, as long as your version of Unity Editor is newer than the one the project was created with. In that case, just click Continue. If your Unity Editor version is older than the one the project needs, click Quit, and upgrade your Unity Editor.
Open Build Settings by selecting File > Build Settings.
In the Platform section, select Universal Windows Platform. Change the Target Device to HoloLens.
Select Switch Platform to change the platform to Universal Windows Platform. Unity might prompt you to install UWP support components if they're missing.
Close the Build Settings window.
Configure the account identifier and key
In the Project pane, go to
Assets/AzureSpatialAnchors.Examples/Scenes and open the
AzureSpatialAnchorsBasicDemo.unity scene file.
The next step is to configure the app to use your account identifier and account key. You copied them into a text editor when setting up the Spatial Anchors resource.
In the Project pane, navigate to
SpatialAnchorConfig. Then, in the Inspector pane, enter the
Account Key as the value for
Spatial Anchors Account Key and the
Account ID as the value for
Spatial Anchors Account Id.
Save the scene by selecting File > Save.
Export the HoloLens Visual Studio project
Open Build Settings by selecting File > Build Settings.
Under Scenes In Build, ensure all the scenes have a check mark next to them.
Select Build. In the dialog box, select a folder in which to export the HoloLens Visual Studio project.
When the export is complete, a folder containing the exported HoloLens project will appear.
Deploy the HoloLens application
In the folder, double-click HelloAR U3D.sln to open the project in Visual Studio.
Change the Solution Configuration to Release, change the Solution Platform to x86, and select Device from the deployment target options.
If using HoloLens 2, use ARM as the Solution Platform, instead of x86.
Turn on the HoloLens device, sign in, and connect the device to the PC by using a USB cable.
Select Debug > Start debugging to deploy your app and start debugging.
Follow the instructions in the app to place and recall an anchor.
In Visual Studio, stop the app by selecting either Stop Debugging or Shift+F5.
Clean up resources
In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, you can delete them by deleting the resource group.
From the left menu in the Azure portal, select Resource groups and then select myResourceGroup.
On the resource group page, make sure that the listed resources are the ones you want to delete.
Select Delete, type myResourceGroup in the text box, and then select Delete.
In this quickstart, you created a Spatial Anchors account. You then configured and deployed an app to save and recall spatial anchors. To learn more about how to improve the app so it can share spatial anchors with other devices, continue to the next tutorial.