Quickstart: Create a Unity Android app with Azure Spatial Anchors

This quickstart covers how to create a Unity Android app using Azure Spatial Anchors. Azure Spatial Anchors is a cross-platform developer service that allows you to create mixed reality experiences using objects that persist their location across devices over time. When you're finished, you'll have an ARCore Android 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 Android Studio project
  • Deploy and run on an Android device

If you don't have an Azure subscription, create a free account before you begin.


To complete this quickstart, make sure you have:

  • A Windows or macOS machine with Unity 2019.1 or 2019.2 including the Android Build Support and Android SDK & NDK Tools modules.
    • If running on Windows, you'll also need Git for Windows and Git LFS.
    • If running on macOS, get Git installed via HomeBrew. Enter the following command into a single line of the Terminal: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)". Then, run brew install git and brew install git-lfs.
  • A developer enabled and ARCore capable Android device.
    • Additional device drivers may be required for your computer to communicate with your Android device. See here for additional information and instructions.

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.

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.

    Create a resource

After the resource is created, Azure Portal will show that your deployment is complete. Click Go to resource.

Deployment complete

Then, you can view the resource properties. Copy the resource's Account ID value into a text editor because you'll need it later.

Resource properties

Also copy the resource's Account Domain value into a text editor because you'll need it later.

Account domain

Under Settings, select Key. Copy the Primary key value into a text editor. This value is the Account Key. You'll need it later.

Account key

Download and open the Unity sample project

Clone the samples repository by running the following commands:

git clone https://github.com/Azure/azure-spatial-anchors-samples.git
cd ./azure-spatial-anchors-samples

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.

Unity window

Open Build Settings by selecting File > Build Settings.

In the Platform section, select Android. Select Switch Platform to change the platform to Android. Unity might prompt you to install Android support components if they're missing.

Unity Build Settings window

Close the Build Settings window.

Configure account identifier and key

In the Project pane, navigate 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 Assets\AzureSpatialAnchors.SDK\Resources. Select 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.

Next, open up SpatialAnchorManager.cs. Find CreateSessionAsync() and add the following line, substituting in your account domain from earlier: session.Configuration.AccountId = "MyAccountDomain";. You can add this line directly before this comment // Configure authentication.

Save the scene by selecting File -> Save.

Export the Android 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 your device in Run Device and click Build And Run. You'll be asked to save an .apk file which you can pick any name for.

Follow the instructions in the app to place and recall an anchor.


Rendering issues

When running the app, if you don't see the camera as the background (for instance you instead see a blank, blue or other textures) then you likely need to re-import assets in Unity. Stop the app. From the top menu in Unity, choose Assets -> Reimport all. Then, run the app again.

Unity 2019.3

Due to breaking changes, Unity 2019.3 is not currently supported. Please use Unity 2019.1 or 2019.2.

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 Azure portal menu or Home page, select Resource groups, and on the Resource groups page, select myResourceGroup.

On the myResourceGroup page, make sure that the listed resources are the ones you want to delete.

Select Delete resource group, type myResourceGroup in the text box to confirm, and then select Delete.

Next steps

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.