AR Anchor Manager component
The ARAnchorManager component is a type of trackable manager that tracks anchors. As a trackable manager, it creates GameObjects in your scene for each tracked anchor.
AR Anchor Manager component
Property | Description |
---|---|
trackables Changed | Invoked when trackables have changed (been added, updated, or removed). |
Anchor Prefab | If not null , this prefab is instantiated for each tracked anchor. If the prefab doesn't contain an AR Anchor component, ARAnchorManager will add one. |
Get started
Add an AR Anchor Manager component to your XR Origin GameObject to enable your app to create and track anchors. If your scene doesn't contain an XR Origin GameObject, first follow the Scene setup instructions.
If the device doesn't support anchors, the AR Anchor Manager component will disable itself during OnEnable
.
Create an anchor
While enabled, the AR Anchor Manager component allows you to create anchors in four different ways:
C# scripting — TryAddAnchorAsync (most widely supported)
On all platforms that support anchors, you can use TryAddAnchorAsync with C# async/await syntax as shown in the follwing code example:
async void CreateAnchorAsync() { // This is inefficient. You should re-use a saved reference instead. var manager = Object.FindAnyObjectByType<ARAnchorManager>(); // This is a "dummy" pose value. You should use a pose that is meaningful // to your app, such as from a raycast hit or another trackable. var pose = new Pose(Vector3.zero, Quaternion.identity); var result = await manager.TryAddAnchorAsync(pose); if (result.status.IsSuccess()) { var anchor = result.value; // Do something with the newly created anchor. } }
C# scripting — AttachAnchor
Some platforms support the ability to attach anchors to other trackables such as planes. The following code sample demonstrates how to check for support, then attach an anchor to a pose on a plane's surface with AttachAnchor.
void AttachAnchor(ARAnchorManager manager, ARPlane plane, Pose pose) { if (manager.descriptor.supportsTrackableAttachments) { var anchor = manager.AttachAnchor(plane, pose); // Do something with the newly created anchor. } }
C# scripting — TryLoadAnchorAsync
Some platforms support the ability to save anchors from one AR session and load them in subsequent AR sessions. Refer to Persistent anchors for more information.
Add an AR Anchor component to a GameObject
When an AR Anchor component is enabled at runtime, it will use the AR Anchor Manager in your scene to attempt to add itself as an anchor. If you choose to create anchors with the AR Anchor component, it is important to understand the limitations of this approach.
Important
When you enable an AR Anchor component at runtime, it makes a request to the AR Anchor Manager to add itself as an anchor. This request can fail, and if it fails, the AR Anchor component deactivates its GameObject and is not tracked.
You should use
ARAnchorManager.trackablesChanged
to verify that the anchor was successfully added before you parent any content to a newly enabled AR Anchor component.
Parent your content to the anchor
The most common use case for anchors is to place virtual content in the physical world. After you create an anchor, you can Instantiate a prefab as a child of that anchor, or call Transform.SetParent to reparent a GameObject in your scene to the anchor GameObject.
Anchor life cycle events
While enabled, the AR Anchor Manager component will get changes reported by the XRAnchorSubsystem every frame. If any anchors were added, updated, or removed, AR Anchor Manager will invoke its trackablesChanged event with the relevant information.
You can subscribe to trackablesChanged
using either the Inspector or C# scripting.
Use the Inspector
a. Create a public method on a
MonoBehaviour
orScriptableObject
with a single paramater of type ARTrackablesChangedEventArgs<ARAnchor>, as shown in the following example code:public void OnTrackablesChanged( ARTrackablesChangedEventArgs<ARAnchor> changes) { foreach (var anchor in changes.added) { // handle added anchors } foreach (var anchor in changes.updated) { // handle updated anchors } foreach (var anchor in changes.removed) { // handle removed anchors } }
b. Select your XR Origin GameObject, then click the Add (+) button on the AR Anchor Manager component's trackables Changed property.
c. Using the Object picker (⊙), select either a GameObject that contains an instance of your component or an instance of your ScriptableObject, whichever is applicable.
Subscribe to the trackablesChanged eventd. In the dropdown, select your class name and the name of your method. The method name appears in the Dynamic section of the methods list.
Use C# scripting
a. Create a public method with a single parameter of type ARTrackablesChangedEventArgs<ARAnchor> as shown in the preceding step 1a.
b. Use the following example code to subscribe to the
trackablesChanged
event:void SubscribeToAnchorsChanged() { // This is inefficient. You should re-use a saved reference instead. var manager = Object.FindAnyObjectByType<ARAnchorManager>(); manager.trackablesChanged.AddListener(OnTrackablesChanged); }
Remove an anchor
While enabled, the AR Anchor Manager component allows you to remove anchors in two ways:
C# scripting
You can use the TryRemoveAnchor API, passing the
ARAnchor
that you wish to remove. When you remove an anchor this way, the nextARAnchorManager.trackablesChanged
event reports theARAnchor
component in its removed list.Destroy the AR Anchor component
When you Destroy an AR Anchor component, it will use the AR Anchor Manager component in your scene to remove itself from the anchor subystem. When you remove an anchor this way, the next
ARAnchorManager.trackablesChanged
event contains a nullARAnchor
component in its removed list, because you destroyed the component before this event was invoked.
Visualize anchors in the scene
By default, the AR Anchor Manager doesn't render any geometry in the scene when anchors are detected. To enable anchor visualization, set a prefab as the AR Anchor Manager's Anchor Prefab.
The AR Foundation Samples GitHub repository contains a prefab that you can use to get started:
Prefab | Description |
---|---|
AR Anchor Debug Visualizer | Visualize anchors with a Transform gizmo, and optionally visualize additional information such as the anchor's trackableId, sessionId, trackingState, and whether the anchor is attached to a plane. |