Class XRAnchorSubsystem
This subsystem provides information regarding anchors. An anchor is a pose (position and rotation) in the physical environment that is tracked by an XR device. Anchors are updated as the device refines its understanding of the environment, allowing you to reliably place virtual content at a real-world pose.
Inheritance
Implements
Inherited Members
Namespace: UnityEngine.XR.ARSubsystems
Assembly: Unity.XR.ARSubsystems.dll
Syntax
public class XRAnchorSubsystem : TrackingSubsystem<XRAnchor, XRAnchorSubsystem, XRAnchorSubsystemDescriptor, XRAnchorSubsystem.Provider>, ISubsystem
Remarks
This is a base class with an abstract provider type to be implemented by provider plug-in packages. This class itself does not implement anchor tracking.
Constructors
XRAnchorSubsystem()
Do not invoke this constructor directly.
Declaration
public XRAnchorSubsystem()
Remarks
If you are implementing your own custom subsystem Lifecycle management, use the SubsystemManager to enumerate the available XRAnchorSubsystemDescriptors, then call XRAnchorSubsystemDescriptor.Register() on the desired descriptor.
Methods
GetChanges(Allocator)
Get the changes to anchors (added, updated, and removed) since the last call to this method.
Declaration
public override TrackableChanges<XRAnchor> GetChanges(Allocator allocator)
Parameters
| Type | Name | Description |
|---|---|---|
| Allocator | allocator | An allocator to use for the |
Returns
| Type | Description |
|---|---|
| TrackableChanges<XRAnchor> | Changes since the last call to this method. |
Overrides
TryAddAnchor(Pose, out XRAnchor)
Attempts to create a new anchor at the given pose.
Declaration
public bool TryAddAnchor(Pose pose, out XRAnchor anchor)
Parameters
| Type | Name | Description |
|---|---|---|
| Pose | pose | The pose, in session space, of the new anchor. |
| XRAnchor | anchor | The new anchor. Only valid if this method returns true. |
Returns
| Type | Description |
|---|---|
| bool |
See Also
TryAddAnchorAsync(Pose)
Attempts to create a new anchor at the given pose.
Declaration
public Awaitable<Result<XRAnchor>> TryAddAnchorAsync(Pose pose)
Parameters
| Type | Name | Description |
|---|---|---|
| Pose | pose | The pose, in session space, of the new anchor. |
Returns
| Type | Description |
|---|---|
| Awaitable<Result<XRAnchor>> | The result of the async operation. You are responsible to await this result. |
TryAttachAnchor(TrackableId, Pose, out XRAnchor)
Attempts to create a new anchor "attached" to the trackable with id trackableToAffix.
The behavior of the anchor depends on the type of trackable to which this anchor is attached.
Declaration
public bool TryAttachAnchor(TrackableId trackableToAffix, Pose pose, out XRAnchor anchor)
Parameters
| Type | Name | Description |
|---|---|---|
| TrackableId | trackableToAffix | The id of the trackable to which to attach. |
| Pose | pose | The pose, in session space, of the anchor to create. |
| XRAnchor | anchor | The new anchor. Only valid if this method returns true. |
Returns
| Type | Description |
|---|---|
| bool |
TryEraseAnchorAsync(SerializableGuid, CancellationToken)
Attempts to erase the persistent saved data associated with an anchor given its persistent anchor GUID.
Declaration
public Awaitable<XRResultStatus> TryEraseAnchorAsync(SerializableGuid savedAnchorGuid, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| SerializableGuid | savedAnchorGuid | A persistent anchor GUID created by TrySaveAnchorAsync(TrackableId, CancellationToken). |
| CancellationToken | cancellationToken | An optional |
Returns
| Type | Description |
|---|---|
| Awaitable<XRResultStatus> | The result of the async operation. You are responsible to await this result. |
See Also
TryEraseAnchorsAsync(NativeArray<SerializableGuid>, Allocator, CancellationToken)
Attempts to erase the persistent saved data associated with a batch of anchors given their persistent anchor GUIDs.
Declaration
public Awaitable<NativeArray<XREraseAnchorResult>> TryEraseAnchorsAsync(NativeArray<SerializableGuid> savedAnchorGuids, Allocator allocator, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| NativeArray<SerializableGuid> | savedAnchorGuids | The persistent anchor GUIDs created by TrySaveAnchorAsync(TrackableId, CancellationToken) or TrySaveAnchorsAsync(NativeArray<TrackableId>, Allocator, CancellationToken). |
| Allocator | allocator | The allocator of the returned |
| CancellationToken | cancellationToken | An optional |
Returns
| Type | Description |
|---|---|
| Awaitable<NativeArray<XREraseAnchorResult>> | The result of the async operation. You are responsible to await this result. |
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown if supportsEraseAnchor is false for this provider. |
TryGetSavedAnchorIdsAsync(Allocator, CancellationToken)
Attempts to get a NativeArray containing all saved persistent anchor GUIDs.
Declaration
public Awaitable<Result<NativeArray<SerializableGuid>>> TryGetSavedAnchorIdsAsync(Allocator allocator, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Allocator | allocator | The allocation strategy to use for the resulting |
| CancellationToken | cancellationToken | An optional |
Returns
| Type | Description |
|---|---|
| Awaitable<Result<NativeArray<SerializableGuid>>> | The result of the async operation, containing a |
See Also
TryLoadAnchorAsync(SerializableGuid, CancellationToken)
Attempts to load an anchor given its persistent anchor GUID.
Declaration
public Awaitable<Result<XRAnchor>> TryLoadAnchorAsync(SerializableGuid savedAnchorGuid, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| SerializableGuid | savedAnchorGuid | A persistent anchor GUID created by TrySaveAnchorAsync(TrackableId, CancellationToken). |
| CancellationToken | cancellationToken | An optional |
Returns
| Type | Description |
|---|---|
| Awaitable<Result<XRAnchor>> | The result of the async operation, containing the newly added anchor if the operation succeeded. You are responsible to await this result. |
See Also
TryLoadAnchorsAsync(NativeArray<SerializableGuid>, Allocator, Action<NativeArray<XRLoadAnchorResult>>, CancellationToken)
Attempts to load a batch of anchors given their persistent anchor GUIDs.
Declaration
public Awaitable<NativeArray<XRLoadAnchorResult>> TryLoadAnchorsAsync(NativeArray<SerializableGuid> savedAnchorGuidsToLoad, Allocator allocator, Action<NativeArray<XRLoadAnchorResult>> incrementalResultsCallback, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| NativeArray<SerializableGuid> | savedAnchorGuidsToLoad | The persistent anchor GUIDs to load that were created by TrySaveAnchorsAsync(NativeArray<TrackableId>, Allocator, CancellationToken) or TrySaveAnchorAsync(TrackableId, CancellationToken). |
| Allocator | allocator | The allocator used for the returned |
| Action<NativeArray<XRLoadAnchorResult>> | incrementalResultsCallback | A callback method that will be called when any requested
anchors are loaded. Pass a |
| CancellationToken | cancellationToken | An optional |
Returns
| Type | Description |
|---|---|
| Awaitable<NativeArray<XRLoadAnchorResult>> | The result of the async operation. You are responsible to await this result. |
Remarks
The order in which anchors are loaded may not match the enumeration order of
savedAnchorGuidsToLoad. To check if an anchor loaded successfully, check the
resultStatus.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown if supportsLoadAnchor is false for this provider. |
TryRemoveAnchor(TrackableId)
Attempts to remove an existing anchor with TrackableId anchorId.
Declaration
public bool TryRemoveAnchor(TrackableId anchorId)
Parameters
| Type | Name | Description |
|---|---|---|
| TrackableId | anchorId | The id of an existing anchor to remove. |
Returns
| Type | Description |
|---|---|
| bool |
TrySaveAnchorAsync(TrackableId, CancellationToken)
Attempts to persistently save the given anchor so that it can be loaded in a future AR session. Use the
SerializableGuid returned by this method as an input parameter to TryLoadAnchorAsync(SerializableGuid, CancellationToken) or
TryEraseAnchorAsync(SerializableGuid, CancellationToken).
Declaration
public Awaitable<Result<SerializableGuid>> TrySaveAnchorAsync(TrackableId anchorId, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TrackableId | anchorId | The TrackableId of the anchor to save. |
| CancellationToken | cancellationToken | An optional |
Returns
| Type | Description |
|---|---|
| Awaitable<Result<SerializableGuid>> | The result of the async operation, containing a new persistent anchor GUID if the operation succeeded. You are responsible to await this result. |
See Also
TrySaveAnchorsAsync(NativeArray<TrackableId>, Allocator, CancellationToken)
Attempts to persistently save the given anchors so that they can be loaded in a future AR session. Use the
SerializableGuids returned from this method as an input to TryLoadAnchorsAsync(NativeArray<SerializableGuid>, Allocator, Action<NativeArray<XRLoadAnchorResult>>, CancellationToken) or
TryEraseAnchorsAsync(NativeArray<SerializableGuid>, Allocator, CancellationToken).
Declaration
public Awaitable<NativeArray<XRSaveAnchorResult>> TrySaveAnchorsAsync(NativeArray<TrackableId> anchorIdsToSave, Allocator allocator, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| NativeArray<TrackableId> | anchorIdsToSave | The |
| Allocator | allocator | The allocator used for the returned |
| CancellationToken | cancellationToken | An optional |
Returns
| Type | Description |
|---|---|
| Awaitable<NativeArray<XRSaveAnchorResult>> | The result of the async operation. You are responsible to await this result. |
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown if supportsSaveAnchor is false for this provider. |