text
stringlengths
0
1.11k
Create a new spatial anchor
Save a spatial anchor to the user’s headset, or to the cloud
Update a spatial anchor
Load a spatial anchor stored in the user’s headset or stored in the cloud
Erase a spatial anchor from the headset or the cloud
Remove a spatial anchor from the current app (destroy it).
Share a spatial anchor with other users
Save spatial anchors UUIDs to PlayerPrefs
Load spatial anchors UUIDs from PlayerPrefs
The OVRSpatialAnchor() Unity component encapsulates a spatial anchor’s entire lifecycle, including creation, destruction, and saving to or erasing from the headset or cloud storage. Each spatial anchor has a unique identifier (UUID) that is assigned upon creation and remains constant throughout the life of the spatial anchor.
Prerequisites
Before working with Spatial Anchors, you should make sure you have Unity set up properly for Meta Quest builds. Also, if you can complete Create your First VR App on Meta Quest Headset, and can build the Spatial Anchors, sample, then you can build apps using Shared Spatial Anchors.
Create Spatial Anchors
To create a new spatial anchor in the Meta Quest runtime, use any Unity GameObject to create it using AddComponent().
Once it is created, the new OVRSpatialAnchor is assigned a unique identifier (UUID) (represented by a System.Guid in Unity), which you can use to load the spatial anchor after it has been saved locally or to the cloud. In the frame following its instantiation, the OVRSpatialAnchor() component uses its current transform to generate a new spatial anchor in the Meta Quest runtime. Because the underlying runtime representation of the spatial anchor is asynchronous, the UUID might not be valid immediately.
One way to work with this time delay is to use a Unity coroutine to ensure the spatial anchor exists before you try to use it.
This can be seen in the following excerpt.
public void CreateSpatialAnchor()
{
GameObject gs = new GameObject();
OVRSpatialAnchor workingAnchor = gs.AddComponent<OVRSpatialAnchor>();
StartCoroutine(anchorCreated(workingAnchor));
}
public IEnumerator anchorCreated(anchor)
{
// keep checking for a valid and localized spatial anchor state
while (!workingAnchor.Created && !workingAnchor.Localized)
{
yield return new WaitForEndOfFrame();
}
Guid anchorGuid = _workingAnchor.Uuid;
}
Save a Spatial Anchor with a Unity Coroutine
You can save spatial anchors objects locally (to the headset) for quick re-loading. Because the saving action is asynchronous, you can use a Unity coroutine as a delegate to control when your post-save actions begin. In this excerpt, we respond to a button press by creating a spatial anchor. The anchorCreated() coroutine ensures that we don’t try to save the newly created anchor before the new spatial it is valid.
public void OnAButtonPressed()
{
GameObject gs = new GameObject();
OVRSpatialAnchor workingAnchor = gs.AddComponent<OVRSpatialAnchor>();
StartCoroutine(anchorCreated(workingAnchor));
}
public IEnumerator anchorCreated(anchor)
{
// keep checking for a valid and localized spatial anchor state
while (!workingAnchor.Created && !workingAnchor.Localized)
{
yield return new WaitForEndOfFrame();
}
//when ready, save the spatial anchor using OVRSpatialAnchor.Save()
_anchor.Save((anchor, success) =>
{
if (!success) return;
// The save is successful. Now we can save the spatial anchor
// to a global List for convenience.
_allAnchors.Add(workingAnchor);
});
}
OVRSpatialAnchor.Save() defaults to using the OVRSpace.StorageLocation.Local enum, which is what we want here.
If you were planning to share the spatial anchor, then you must save it with OVRSpace.StorageLocation.Cloud before sharing. Saving to local (with optional parameter OVRSpace.StorageLocation.Local) is an optional optimization in this case. If you save the anchor locally, you can reload the next time the app is started instead of creating a new one.
Note that you only ever need to save using OVRSpace.StorageLocation.Cloud if you need to share the anchor in the future using shared spatial anchors. For all other use cases, saving without any parameters should work.
Update a Spatial Anchor
Once you have successfully created the spatial anchor in the Meta Quest runtime, the OVRSpatialAnchor() component updates its transform automatically. This automatic update is necessary because spatial anchors may drift slightly over time.
This excerpt from the Starter SamplesOVRSpatialAnchor.cs script shows how the Spatial Anchors sample does an explicit update:
if (TryGetPose(Space, out var pose))
{
transform.SetPositionAndRotation(pose.position, pose.orientation);
}
Find OVRSpatialAnchor.cs in .\Assets\Oculus\VR\Scripts.
Load a Spatial Anchor from the Headset or the Cloud
Anchors are loaded in three steps:
Load unbound spatial anchors using their UUID
Localize each spatial anchor
Bind each spatial anchor to OVRSpatialAnchor()
Each of these is described below.