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.
Adding and removing anchors
To add or remove anchors, call AddAnchor or RemoveAnchor on the ARAnchorManager component in script. In some scenarios, you can create anchors in other ways, such as loading an AR World Map on ARKit which includes saved anchors.
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 will be in a "pending" state. You can query for this with the ARAnchor.pending property.
Likewise, when you remove an anchor, it might take a frame before anchorsChanged
reports it as removed. If you remove a pending anchor before it's reported as added, you won't receive any events for it.
You should always remove anchors through the anchor manager. Don't Destroy an ARAnchor
unless its manager has also been destroyed.
Anchoring content
A typical use case for anchors is to place virtual content in the physical world. We recommend creating an ARAnchor and then parenting your content to that anchor.
While the ARAnchorManager has an "Anchor Prefab" field, this is not intended for content. When an anchor is created, either by you or by some other mechanism, such as loading an ARWorldMap that contained anchors, ARFoundation will create a new GameObject to represent it.
If "Anchor Prefab" is null
, then ARFoundation simply creates a GameObject with an ARAnchor
component on it. However, if you want every anchor to also include additional components, you can provide a prefab for ARFoundation to instantiate for each anchor. In other words, the purpose of the prefab field is to extend the default behavior of anchors; it is not the recommended way to place content in the world. Instead, you should parent your content to the anchor.
Examples:
To parent existing content to an anchor:
ARAnchorManager m_AnchorManager;
void AnchorContent(Vector3 position, Transform content)
{
// Create a new anchor.
var anchor = m_AnchorManager.AddAnchor(new Pose(position, Quaternion.identity));
// Parent 'content' to it.
content.parent = anchor.transform;
}
To instantiate a prefab with your content as a child of an anchor:
ARAnchorManager m_AnchorManager;
void AnchorContent(Vector3 position, GameObject prefab)
{
// Create a new anchor.
var anchor = m_AnchorManager.AddAnchor(new Pose(position, Quaternion.identity));
// Instantiate 'prefab' as a child of the new anchor.
Instantiate(prefab, anchor.transform);
}
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 will only change its position along the normal of the plane to which it is attached, thus maintaining a constant distance from the plane.