⚠ Preview docs — built from the develop branch and may be unstable. Go to the stable docs.

SpatialEntity.FindAnchor

static SpatialEntity FindAnchor(string name)

Gets the anchor you created with this name, in this session or an earlier one! This is the easy way to restore anchors across app restarts. Call this once and hold onto the result. Storage loads asynchronously, so the anchor starts out with Status Pending and no data, then fills in when it loads. Nothing loads while SpatialCapability.Anchor isn’t Spatial.Requested.

   
string name The name given to CreateAnchor.
RETURNS: SpatialEntity The anchor, already loaded or still loading. This is invalid if no anchor has this name, so check Valid.
static SpatialEntity FindAnchor(Guid guid)

Gets the anchor with this Guid, for apps that keep track of anchors by Guid themselves. Works like FindAnchor(string), and if storage doesn’t have this Guid, Status becomes Failed and the anchor shows up in Removed.

   
Guid guid A Guid from TryGetGuid, saved in an earlier session.
RETURNS: SpatialEntity The anchor, already loaded or still loading. This is invalid for an empty Guid, or a system without persistence support.

Examples

Anchors that survive an app restart

Give an anchor a name, and FindAnchor gets it back in a later session. Placing it again under the same name replaces the old one.

SpatialEntity anchor;

public void LoadAnchor()
{
	Spatial.Request(SpatialCapability.Anchor);
	anchor = SpatialEntity.FindAnchor("my_anchor");
}

// Call this from a button, a pinch, or wherever the user places content
public void PlaceAnchor(Pose pose)
	=> anchor = SpatialEntity.CreateAnchor(pose, "my_anchor");

public void StepAnchor()
{
	if (anchor.Tracked.IsActive())
		Mesh.Cube.Draw(Material.Default, anchor.Pose.ToMatrix(0.1f));
}

Keeping track of anchors by Guid

For apps that keep anchors in their own save data instead of by name. The Guid arrives a moment after Persist, so watch for it.

List<Guid> savedAnchors = new List<Guid>();

public void StepSavedAnchors()
{
	foreach (SpatialEntity anchor in SpatialEntity.With(SpatialComponent.Anchor))
	{
		if (anchor.HasChanged(SpatialComponent.Persistence) && anchor.TryGetGuid(out Guid guid))
			savedAnchors.Add(guid);
	}

	// Storage no longer having a Guid shows up as Failed
	foreach (SpatialEntity anchor in SpatialEntity.Removed)
	{
		if (anchor.Status == SpatialStatus.Failed && anchor.TryGetGuid(out Guid lost))
			savedAnchors.Remove(lost);
	}
}

// In a later session, each Guid loads back into its anchor
public List<SpatialEntity> RestoreAnchors()
{
	List<SpatialEntity> result = new List<SpatialEntity>();
	foreach (Guid guid in savedAnchors)
		result.Add(SpatialEntity.FindAnchor(guid));
	return result;
}




Found an issue with these docs, or have some additional questions? Create an Issue on Github!