Tutorial: Share Azure Spatial Anchors across sessions and devices
In this tutorial, you'll learn how to use Azure Spatial Anchors to create anchors during one session and then locate them, on the same device or on a different one. These same anchors could also be located by multiple devices in the same place and at the same time.
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 app that can be deployed to two or more devices. Azure Spatial Anchors created by one instance can be shared to the others.
You'll learn how to:
- Deploy an ASP.NET Core Web App in Azure that can be used to share anchors, storing them in memory for a duration of time.
- Configure the AzureSpatialAnchorsLocalSharedDemo scene within the Unity Sample from our Quickstarts to take advantage of the Sharing Anchors Web App.
- Deploy and run to one or more devices.
To complete this tutorial, make sure you have:
- Read through the Azure Spatial Anchors overview.
- Completed one of the 5-minute Quickstarts.
- Basic knowledge on C# and Unity.
- Basic knowledge on ARCore if you want to use Android, or ARKit if you want to use iOS.
- A Windows computer on which Visual Studio 2017 or later is installed with the ASP.NET and web development workload.
- The .NET Core 2.2 SDK.
- One or more devices (iOS or Android) on which to deploy and run an app.
- If you're using Android, you need to have:
- If you're using iOS, you need to have:
- A macOS computer on which Xcode 10 or later, CocoaPods, and Unity 2019.1 or 2019.2 are installed.
- A developer-enabled ARKit-compatible iOS device.
- Git installed via Homebrew. Enter the following command on a single line in the Terminal:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"Then run
brew install git.
It's worth noticing that, although you'll be using Unity and an ASP.NET Core Web App in this Tutorial, it is only to show an example on how to share Azure Spatial Anchor identifiers across other devices. You can use other languages and back-end technologies to achieve the same goal. Also, the ASP.NET Core Web App used in this Tutorial has a dependency on .NET Core 2.2 SDK. It runs fine on regular Azure Web Apps (for Windows), but will currently not work on Azure Web Apps for Linux.
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.
Also copy the resource's Account Domain 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 the 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
Deploy your Sharing Anchors Service
Open Visual Studio, and open the project at the
Open the publish wizard
In Solution Explorer, right-click the SharingService project and select Publish.
The Publish Wizard starts. Select App Service > Publish to open the Create App Service dialog box.
Sign in to Azure
In the Create App Service dialog box, select Add an account and sign in to your Azure subscription. If you're already signed in, select the account you want from the drop-down list.
If you're already signed in, don't select Create yet.
Create a resource group
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.
Next to Resource Group, select New.
Name the resource group myResourceGroup and select OK.
Create an App Service plan
An App Service plan specifies the location, size, and features of the web server farm that hosts your app. You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan.
App Service plans define:
- Region (for example: North Europe, East US, or Southeast Asia)
- Instance size (small, medium, or large)
- Scale count (1 to 20 instances)
- SKU (Free, Shared, Basic, Standard, or Premium)
Next to Hosting Plan, select New.
In the Configure Hosting Plan dialog box, use these settings:
|App Service Plan||MySharingServicePlan||Name of the App Service plan.|
|Location||West US||The datacenter where the web app is hosted.|
|Size||Free||The pricing tier that determines hosting features.|
Create and publish the web app
In App Name, enter a unique app name (valid characters are
-), or accept the automatically generated unique name. The URL of the web app is
<app_name> is your app name.
Select Create to start creating the Azure resources.
After the wizard finishes, it publishes the ASP.NET Core web app to Azure and then opens the app in your default browser.
The app name you used in this section is used as the URL prefix in the format
https://<app_name>.azurewebsites.net. Take note of this URL because you'll need it.
Deploy the sample app
The Java android sample supports sharing across devices.
Open the file
SharedActivity.java from the samples folder in Android Studio. Enter the url you obtained in the previous step (from your ASP.NET web app Azure deployment) as the value for
SharingAnchorsServiceUrl in the
SharedActivity.java file. Replace the
index.html in the url with
api/anchors. It should look like this:
Deploy the app to your device. Once the app starts, in the Choose A Demo dialog, use the left or right arrows to select the LocalShare option, and tap Go!. Follow the instructions in the app. You can select Create & Share Anchor or Locate Shared Anchor.
Create & Share Anchor lets you create an anchor and save it to your sharing service. In return, you will get back an identifier for it that you can use to retrieve it from the sharing service. After this, you can then run the second scenario, Locate Shared Anchor, from either your device or a different one. Locate Shared Anchor allows you to locate previously shared anchors by entering the identifier mentioned earlier. After you pick your scenario, the app will guide you with further instructions around what to do. For example, you'll be asked to move your device around to collect environment information. Later on, you'll place an anchor in the world, wait for it to save, start a new session, and then locate it.
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.
In this tutorial, you've deployed an ASP.NET Core Web App in Azure, and then configured and deployed a Unity App. You created Spatial Anchors with the app, and shared them with other devices by using your ASP.NET Core Web App.
You can improve your ASP.NET Core Web App so that it uses Azure Cosmos DB to persist the storage of your shared Spatial Anchor identifiers. Adding Azure Cosmos DB support will allow your ASP.NET Core Web App to create an anchor today, and come back days later to be able to locate it again, by using the anchor identifier stored in your web app.