text
stringlengths 0
1.11k
|
|---|
Load Unbound Anchors
|
The first step is to load a collection of unbound spatial anchors using OVRSpatialAnchor.LoadUnboundAnchors(). An unbound spatial anchor is a spatial anchor you previously saved locally but haven’t bound to an OVRSpatialAnchor() component. The load operation is asynchronous and may take multiple frames to complete, so we advise using a delegate as with the Save a Spatial Anchor with a Unity Coroutine example above.
|
public static bool LoadUnboundAnchors(LoadOptions options, Action<UnboundAnchor[]> onComplete)
|
The LoadOptions parameter requires an explicit list of UUIDs to load. This operation is asynchronous, so as we note above you must supply an onComplete callback to handle the results.
|
This excerpt from the Starter Samples script SpatialAnchorLoader.cs shows loading a collection of unbound spatial anchors. In this implementation, the call to LoadUnboundAnchors() is done as a lambda expression:
|
private void Load(OVRSpatialAnchor.LoadOptions options) => OVRSpatialAnchor.LoadUnboundAnchors(options, anchors =>
|
{
|
if (anchors == null)
|
{
|
Log("Query failed.");
|
return;
|
}
|
...
|
}
|
Find SpatialAnchorLoader.cs in .\Assets\StarterSamples\Usage\SpatialAnchor\Scripts.
|
Localize each anchor
|
Localizing a spatial anchor causes the system to determine the spatial anchor’s pose in the world.
|
Note The term localize is in the context of Simultaneous Localization and Mapping (SLAM).
|
In the second step, use OVRSpatialAnchor.UnboundAnchor.Localize() to localize each spatial anchor.
|
public void Localize(Action<UnboundAnchor, bool> onComplete = null, double timeout = 0)
|
If you have content associated with the spatial anchor, you should make sure that you have localized the spatial anchor before instantiating its associated content. (You may skip this step if you do not need the spatial anchor’s pose immediately.)
|
This stage is also asynchronous, so we advise using a coroutine delegate as with the Save a Spatial Anchor with a Unity Coroutine example above.
|
Some spatial anchors may already be localized. You can check whether this is the case using the OVRSpatialAnchor.UnboundAnchor.Localized property:
|
This excerpt from the Starter Samples script SpatialAnchorLoader.cs shows localizing a collection of unbound spatial anchors:
|
foreach (var anchor in anchors)
|
{
|
if (anchor.Localized) {
|
_onLoadAnchor(anchor, true);
|
} else if (!anchor.Localizing) {
|
anchor.Localize(_onLoadAnchor);
|
}
|
}
|
Find SpatialAnchorLoader.cs in .\Assets\StarterSamples\Usage\SpatialAnchor\Scripts.
|
The Localize() method above may fail if the spatial anchor is in a part of the environment that is not perceived or is poorly mapped, such as if the spatial anchor is behind the user. In that case, you can try to localize the spatial anchor again. You might also consider guiding the user to look around their environment.
|
Bind Each Spatial Anchor to OVRSpatialAnchor
|
In the third step, you bind a spatial anchor to its intended game object’s OVRSpatialAnchor() component. Unbound spatial anchors should be bound to an OVRSpatialAnchor() component to manage their lifecycle and to provide access to other features such as Save(), Erase(), and Destroy().
|
You should bind an unbound OVRSpatialAnchor() immediately upon instantiation with OVRSpatialAnchor.UnboundAnchor.BindTo().
|
This excerpt from the Starter Samples script SpatialAnchorLoader.cs shows testing and binding of the localized spatial anchor.
|
var pose = unboundAnchor.Pose;
|
var spatialAnchor = Instantiate(_anchorPrefab, pose.position, pose.rotation);
|
unboundAnchor.BindTo(spatialAnchor);
|
if (spatialAnchor.TryGetComponent<Anchor>(out var anchor))
|
{
|
// We just loaded it, so we know it exists in persistent storage.
|
anchor.ShowSaveIcon = true;
|
}
|
The call to Instantiate is to the UnityEngine.Object.Instantiate() method.
|
Note An OVRSpatialAnchor() object that is not bound to an existing spatial anchor will create a new spatial anchor after the first frame of its existence.
|
Erase Spatial Anchors from the Headset or the Cloud
|
Use the OVRSpatialAnchor.Erase() method to erase a spatial anchor from local or cloud storage. As with Save(), the OVRSpace.StorageLocation enum governs the storage to erase the spatial anchor from.
|
The Erase() operation is asynchronous. As with the other asynchronous methods listed in this topic, we advise using a delegate as with the Save a Spatial Anchor with a Unity Coroutine example above. The delegate takes two arguments:
|
OVRSpatialAnchor(): The spatial anchor you are erasing
|
bool: true if the operation succeeded; otherwise, false
|
For example, this excerpt from the OnEraseButtonPressed() action in Anchor.cs governs the erasure of a spatial anchor in the Starter SamplesSpatial Anchors sample:
|
if (!_spatialAnchor) return;
|
_spatialAnchor.Erase((anchor, success) =>
|
{
|
if (success)
|
{
|
_saveIcon.SetActive(false);
|
}
|
});
|
Find Anchor.cs in .\Assets\StarterSamples\Usage\SpatialAnchor\Scripts.
|
Destroy Spatial Anchors
|
When you destroy an OVRSpatialAnchor component, it removes the spatial anchor from the Meta Quest runtime. Note that destroying a spatial anchor only destroys the current runtime instance. It does not affect spatial anchors in local or cloud storage. Destroying a spatial anchor means the Meta Quest runtime is no longer tracking it. However, the spatial anchor continues to remain where you stored it (local, cloud, or both). Therefore if a spatial anchor is saved, you can reload the destroyed spatial anchor object if you have its UUID.
|
This excerpt from OnEraseButtonPressed() action in the Anchor.cs script destroys a spatial anchor.
|
public void OnHideButtonPressed()
|
{
|
Destroy(gameObject);
|
}
|
Find Anchor.cs in .\Assets\StarterSamples\Usage\SpatialAnchor\Scripts.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.