AR anchor manager
The anchor manager is a type of trackable manager.
The anchor manager creates GameObject
s for each anchor. An anchor is a particular point in space that you want the device to track. The device typically performs additional work to update the position and orientation of the anchor throughout its lifetime. Because anchors are generally resource-intensive objects, you should use them sparingly.
Anchor Prefab
While the ARAnchorManager has an "Anchor Prefab" field, this is not intended for content. When an anchor is created in some way other than AddComponent
, such as loading an ARWorldMap that contained anchors, AR Foundation creates a new GameObject to represent it.
If "Anchor Prefab" is null
, AR Foundation creates a GameObject with an ARAnchor
component on it. However, if you want the anchor to also include additional components, you can provide a Prefab for AR Foundation to instantiate for the anchor. In other words, the Prefab field extends the default behavior of anchors, but it is not the recommended way to place content in the world.
Adding and removing anchors
To add a new anchor, add the ARAnchor component to any GameObject using AddComponent<ARAnchor>. Anchors can also be created indirectly, for example by loading an ARWorldMap which includes saved anchors.
You should not change the transform of an anchor; its transform is updated automatically by ARFoundation.
When you add an anchor, it might take a frame or two before the anchor manager's anchorsChanged event reports it as added. During the time between being added and being reported as added, the anchor is in a "pending" state. You can query for this with the ARAnchor.pending property.
To remove an anchor, Destroy the ARAnchor
component (or its GameObject).
Note
When you Destroy
an ARAnchor
, you will not receive an anchorsChanged
event for it.
Anchoring content
A typical use case for anchors is to place virtual content in the physical world.
Examples:
To make existing content an anchor:
void AnchorContent(Vector3 position, GameObject content)
{
// Add an anchor to your content
content.AddComponent<ARAnchor>();
}
To instantiate a prefab as an anchor:
void AnchorContent(Vector3 position, GameObject prefab)
{
// Create an instance of the prefab
var instance = Instantiate(prefab, position, Quaternion.identity);
// Add an ARAnchor component if it doesn't have one already.
if (instance.GetComponent<ARAnchor>() == null)
{
instance.AddComponent<ARAnchor>();
}
}
Attaching anchors
You can also create anchors that are attached to a plane. The AttachAnchor method does this:
public ARAnchor AttachAnchor(ARPlane plane, Pose pose);
Attaching an anchor to a plane affects the anchor update semantics. This type of anchor only changes its position along the normal of the plane to which it is attached, thus maintaining a constant distance from the plane.
A typical use case for attaching an anchor to a plane is to place virtual content on the plane. Unity recommends creating the anchor with AttachAnchor
, and then parenting your content to the anchor.