This page includes a tutorial for setting up spatial anchors within Unity for a Meta Quest headset, using Unity, Meta XR All-in-One SDK, and Meta Building Blocks. For general information on spatial anchors, please reference this wiki page: Spatial Anchors. For general information on currently available Building Blocks, please reference this wiki page: Meta Building Blocks in Unity.
Meta Quest 3
Device setup for developing:
Additional set up tutorial: Set up Unity for XR development
Meta XR All-in-One SDK installed
Add as an asset to your Unity account
Once in Unity, navigate to Package Manager and install All-in-One SDK
Before setting up spatial anchors, it is important to understand how to set up passthrough in your scene, as this is essential to having spatial anchors be implemented. To set up passthrough, below are two tutorials to help walk through the necessary steps
Passthrough Tutorials with Building Blocks
https://developers.meta.com/horizon/documentation/unity/unity-passthrough-tutorial-with-blocks/
These tutorials include the use of the Passthrough Building Block which allows for a low/no code approach to implementation.
Passthrough Basic Tutorial
https://developers.meta.com/horizon/documentation/unity/unity-passthrough-tutorial/
This tutorial uses the OVRCameraRig directly to integrate Passthrough into the scene.
Once you have set up passthrough in your Unity scene, you are ready to start adding spatial anchors. You must have passthrough in your scene for spatial anchors to work.
With your Unity project open, go to Meta > Tools > Building Blocks to open the Building Blocks.
Add to your scene the following Building Blocks: Spatial Anchor Core, Controller Buttons Mapper, and Sample Spatial Anchor Controller.
Your scene will now be ready to use with spatial anchors but to customize the prefab being used (the visualization that will be anchored), go to [BuildingBlock] Sample Spatial Anchor Controller, then under the Inspector tab, go to Spatial Anchor Spawner Building Block (script). Update the Anchor Prefab and check if you would like it to follow your hand before anchoring.
You are now ready to run your project within your Meta Quest. To run:
Save your scene
Navigate to File > Build Profile
Go to the Android tab and this should be Active. If not active, select Switch Platform
Make sure your headset is connected by going to Run Device and your device is selected
Under Scene List, the scene you selected should have a check next to it
Select Build and Run
Put on your head set and try out your spatial anchors
Aim your right controller at your designated anchor spot and press A to set your anchor
When reentering your project, press B to reload your anchors
To remove all anchors, press down on your left controller joystick
Spatial Anchors Tutorial: https://developers.meta.com/horizon/documentation/unity/unity-spatial-anchors-basic-tutorial/
Spatial Anchors with Building Blocks Tutorial: https://www.youtube.com/watch?v=TrfesLoukI4
Now that you have spatial anchors set up in your project, you are ready to add more customizations. The below steps will walk you through how to anchor more than one asset in a project and how to update your controller mapping to help.
Upload your asset to Unity. One way to do so is to make a new folder under Assets in your project to hold all of the visualizations you wish to anchor. Then, drag and drop your asset into this folder.
Go to [BuildingBlock] Controller Buttons Mapper, which you should be able to find in your scene. (If not, go back over the above tutorial to make sure you have spatial anchors set up correctly. ) In the Inspector, scroll down to Button Click Actions.
By default, the prefab anchor will anchor in your scene by the press of A (A is also One within Unity).
Scroll to the bottom of the Button Click Actions and select the + to add a new action. Give your action a Title and select the Button that will create this action.
Under Callback(), select your object to be [BuildingBlock] Sample Spatial Anchor Controller. Select your action to be SpatialAnchorSpawnerBuildingBlock.AnchorPrefab, and select your asset to be the asset you wish to use.
Save your project and Build and Run using the same steps as the Spatial Anchor Tutorial above.
Your additional asset should now spawn when you select your designated button. It will anchor when you select A.
Note: These steps can be customized to meet your needs and this is an example of how you can further use spatial anchors in your space, beyond using only one asset.