The Spatial Anchor Unity sample project demonstrates the capabilities of the Spatial Anchor system. This sample project also provides example code for handling and maintaining Spatial Anchors, which you can reuse in your projects.
Prerequisites
Before building the Spatial Anchor sample, you should read the section on Spatial Anchors, beginning with the Overview. You should also make sure you have Unity set up properly for Meta Quest builds. If you can create the Meta Quest Unity getting started project, Create your First VR App on Meta Quest Headset, you should be good to go.
If you prefer, set up the Meta Quest Developer Hub so you can control your on-Headset application.
In the Unity Project explorer, search for the SpatialAnchor scene. Drag it to your Hierarchy window or double click to open the scene. You can also find the scene in Assets > StarterSamples > Usage > SpatialAnchor or locate it by using the search field.
A dialog with request to import TMP is displayed. Import TMP as directed.
Remove any other scene from the Hierarchy Window.
Save your project and your scene.
From the File menu, choose Build Settings - the Build Settings window opens.
From the Run Device list, select your Meta Quest headset. If you don’t see the headset in the list, click Refresh.
Click Add Open Scenes to add your scene to the build. Deselect and remove any other scenes from the selection window.
Click Build and Run to launch the program onto your headset.
Using the Sample
When you first enter the scene, you see a control menu attached to the right controller.
To create and place anchors:
Move the thumbstick on the right Touch controller forward to highlight Enter Create Mode. Click the trigger on the front of the controller to
Enter Create Mode. (Use the trigger again when you exit the mode.)
Use the controller’s position and orientation to control where you want to place the anchors. Click A to create anchors.
Continue pressing the controller A button to place anchors.
When you have finished placing anchors, move the thumbstick on the right Touch controller forward to highlight the Exit Create Mode menu item. Then press the right trigger.
To save an anchor in persistent storage, or to destroy, or erase it:
Exit the create mode by pressing the front trigger while highlighting the Exit Create Mode menu item.
Press the A button to select the anchor.
A dropdown menu with options to Save, Destroy or Erase anchor is shown. Use the thumbstick to navigate up and down your options.
Press the front trigger to make your selection.
When you choose Save Anchor, a Save icon appears in the anchor’s title. The anchor is saved in persistent storage.
When you choose Destroy Anchor, the anchor is destroyed. If you do not save the anchor, it will not be available for later sessions. You can, however, retrieve the saved anchor during the current session by using Load Anchors.
When you choose Erase Anchor, the Save icon is removed from the anchor’s title, but the anchor remains for the rest of the session.
Note: When you choose Load Anchors, any anchors you saved in earlier sessions are shown, as well as any saved anchors in the current session that you have destroyed.
To load saved anchors from a previous session:
Make sure you are not in create mode. If you are, move the thumbstick on the right Touch controller forward to highlight the Exit Create Mode menu item. Then squeeze the right trigger.
Move the thumbstick on the right Touch Controller backward to highlight the Load Anchors menu item. Then squeeze the right trigger. Any anchors you saved in earlier sessions are shown.
To clear non-persisted anchors that were previously created and saved to Unity’s PlayerPrefs:
Make sure you are in Unity and not running the app in Play mode.
Navigate to Meta > Samples > Clear Anchor UUIDs. Doing so will clear all anchor UUIDs from the PlayerPrefs. Alternatively, you can use Edit > Clear All PlayerPrefs which will clear all PlayerPrefs, and not just anchor UUIDs.
Note: To learn about keeping track of anchors you create using their UUIDs by saving them to PlayerPrefs for easy reference in a future session, see Tips for using spatial anchors
Key Assets
OVR Passthrough Layer Script
The OVRPassthroughLayer.cs script is a component attached to the OVRCameraRig. Incorporating Passthrough enables you to fix anchors on locations in the user’s mixed reality experience. Please refer to OVRPassthroughLayer class reference for detailed information.
Manager Game Object
The Manager game object instantiates the Spatial Anchor Loader and the Anchor UI Manager scripts.
Anchor UI Manager Script
The AnchorUIManager.cs script controls how the you use the spatial anchor control menu.
Anchor Script
The Anchor.cs script controls the actions that happens when you select an item from anchor control menu (create, select, place, save, load, and delete).
Spatial Anchor Loader Script
The SpatialAnchorLoader.cs script demonstrates how to load previously created anchors from storage into the user’s mixed reality experience.
DemoAnchorPrefab
All spatial anchors created are based on the DemoAnchorPrefab prefab object. It defines the appearance of both the anchor and the anchor’s control menu.
Showcase Apps in GitHub
You can find more examples of using spatial anchors with Meta Quest in the oculus-samples GitHub repository. The Unity-Discover and Unity-SharedSpatialAnchors apps both highlight the implementation of spatial anchors.