Class ARMarkerManager
A trackable manager that enables you to detect and track AR markers. Add this component to your XR Origin GameObject to enable marker tracking in your app.
Inheritance
Implements
Inherited Members
Namespace: UnityEngine.XR.ARFoundation
Assembly: Unity.XR.ARFoundation.dll
Syntax
[DefaultExecutionOrder(-2147483647)]
[DisallowMultipleComponent]
[RequireComponent(typeof(XROrigin))]
public class ARMarkerManager : ARTrackableManager<XRMarkerSubsystem, XRMarkerSubsystemDescriptor, XRMarkerSubsystem.Provider, XRMarker, ARMarker>, ITrackablesChanged<ARMarker>
Remarks
An AR Marker is a known visual pattern in the physical environment, such as a QR code or an ArUco tag, that a device can recognize and track. This manager detects these markers and creates GameObjects with ARMarker components to represent them.
Properties
gameObjectName
The name to assign to the GameObject instantiated for each ARMarker.
Declaration
protected override string gameObjectName { get; }
Property Value
Type | Description |
---|---|
string |
Overrides
markerPrefab
Get or set the prefab to instantiate for each detected AR marker. Can be null
.
Declaration
public GameObject markerPrefab { get; set; }
Property Value
Type | Description |
---|---|
GameObject |
Methods
GetMarker(TrackableId)
Gets the ARMarker with the given trackableId
, or null
if no such marker exists.
Declaration
public ARMarker GetMarker(TrackableId trackableId)
Parameters
Type | Name | Description |
---|---|---|
TrackableId | trackableId | The TrackableId of the ARMarker to retrieve. |
Returns
Type | Description |
---|---|
ARMarker | The ARMarker or |
GetPrefab()
Get the prefab to instantiate for each ARMarker.
Declaration
protected override GameObject GetPrefab()
Returns
Type | Description |
---|---|
GameObject | The prefab to instantiate for each ARMarker. |
Overrides
Raycast(Ray, TrackableType, Allocator)
Performs a raycast against all tracked markers.
Declaration
public NativeArray<XRRaycastHit> Raycast(Ray ray, TrackableType trackableTypeMask, Allocator allocator)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The ray, in session space, to cast against the markers. |
TrackableType | trackableTypeMask | A mask of trackable types to raycast against. If this mask does not include Marker, the method returns an empty array. |
Allocator | allocator | The allocator to use for the returned NativeArray_1 of hits. The caller is responsible for disposing this array. |
Returns
Type | Description |
---|---|
NativeArray<XRRaycastHit> | A NativeArray_1 containing the raycast hit data for each hit marker. The caller must dispose of this array for persistent allocators. |
Remarks
This method checks for intersections between the given ray
and the rectangular plane
of each active ARMarker. It is an implementation of the Raycast(Ray, TrackableType, Allocator) method.
TryGetBytesData(ARMarker)
Attempts to get the encoded data of a specific marker, decoded as a byte array.
Declaration
public Result<byte[]> TryGetBytesData(ARMarker marker)
Parameters
Type | Name | Description |
---|---|---|
ARMarker | marker | The ARMarker for which to retrieve the encoded data. |
Returns
Type | Description |
---|---|
Result<byte[]> | A |
Remarks
To determine if a marker's encoded data is available as binary data, you can check its dataBuffer property. If the bufferType is Uint8, this method should be used to retrieve the encoded data.
This method allocates a new managed byte[]
on each successful call. For performance critical applications
that need to avoid garbage collection, consider using (TryGetBytesData(ARMarker, Allocator)).
TryGetBytesData(ARMarker, Allocator)
Attempts to get the encoded data of a specific marker, decoded as a NativeArray_1.
Declaration
public Result<NativeArray<byte>> TryGetBytesData(ARMarker marker, Allocator allocator)
Parameters
Type | Name | Description |
---|---|---|
ARMarker | marker | The marker with the encoded data to retrieve. |
Allocator | allocator | The allocator to use for the returned NativeArray_1. |
Returns
Type | Description |
---|---|
Result<NativeArray<byte>> | A |
Remarks
This method is useful for performance sensitive applications as it avoids managed memory allocations. The caller is responsible for disposing the returned NativeArray_1 for persistent allocators.
TryGetStringData(ARMarker)
Attempts to get the encoded data of a specific marker, decoded as a string.
Declaration
public Result<string> TryGetStringData(ARMarker marker)
Parameters
Type | Name | Description |
---|---|---|
ARMarker | marker | The ARMarker for which to retrieve the encoded data. |
Returns
Type | Description |
---|---|
Result<string> | A |
Remarks
To determine if a marker's encoded data is available as a string, you can check its dataBuffer property. If the bufferType is String, this method should be used to retrieve the encoded data.
Alternatively, if you expect string data (for example, you are scanning QR codes with URLs), you can call this method directly and check the status to see if it was successful.