using System;
using Unity.Collections;
using UnityEngine.SubsystemsImplementation;
namespace UnityEngine.XR.ARSubsystems
{
///
/// Base class for an anchor subsystem.
///
///
/// An anchor is a pose in the physical environment that is tracked by an XR device.
/// As the device refines its understanding of the environment, anchors will be
/// updated, allowing you to keep virtual content connected to a real-world position and orientation.
/// This abstract class should be implemented by an XR provider and instantiated using the SubsystemManager
/// to enumerate the available s.
///
public class XRAnchorSubsystem
: TrackingSubsystem
{
///
/// Constructor. Do not invoke directly; use the SubsystemManager
/// to enumerate the available s
/// and call Create on the desired descriptor.
///
public XRAnchorSubsystem() { }
///
/// Get the changes to anchors (added, updated, and removed) since the last call
/// to .
///
/// An allocator to use for the NativeArrays in .
/// Changes since the last call to .
public override TrackableChanges GetChanges(Allocator allocator)
{
if (!running)
throw new InvalidOperationException("Can't call \"GetChanges\" without \"Start\"ing the anchor subsystem!");
var changes = provider.GetChanges(XRAnchor.defaultValue, allocator);
#if DEVELOPMENT_BUILD || UNITY_EDITOR
m_ValidationUtility.ValidateAndDisposeIfThrown(changes);
#endif
return changes;
}
///
/// Attempts to create a new anchor with the provide .
///
/// The pose, in session space, of the new anchor.
/// The new anchor. Only valid if this method returns true.
/// true if the new anchor was added, otherwise false.
public bool TryAddAnchor(Pose pose, out XRAnchor anchor)
{
return provider.TryAddAnchor(pose, out anchor);
}
///
/// Attempts to create a new anchor "attached" to the trackable with id .
/// The behavior of the anchor depends on the type of trackable to which this anchor is attached.
///
/// The id of the trackable to which to attach.
/// The pose, in session space, of the anchor to create.
/// The new anchor. Only valid if this method returns true.
/// true if the new anchor was added, otherwise false.
public bool TryAttachAnchor(TrackableId trackableToAffix, Pose pose, out XRAnchor anchor)
{
return provider.TryAttachAnchor(trackableToAffix, pose, out anchor);
}
///
/// Attempts to remove an existing anchor with .
///
/// The id of an existing anchor to remove.
/// true if the anchor was removed, otherwise false.
public bool TryRemoveAnchor(TrackableId anchorId)
{
return provider.TryRemoveAnchor(anchorId);
}
///
/// An abstract class to be implemented by providers of this subsystem.
///
public abstract class Provider : SubsystemProvider
{
///
/// Invoked to get the changes to anchors (added, updated, and removed) since the last call to
/// .
///
/// The default anchor. This should be used to initialize the returned
/// NativeArrays for backwards compatibility.
/// See .
///
/// An allocator to use for the NativeArrays in .
/// Changes since the last call to .
public abstract TrackableChanges GetChanges(XRAnchor defaultAnchor, Allocator allocator);
///
/// Should create a new anchor with the provided .
///
/// The pose, in session space, of the new anchor.
/// The new anchor. Must be valid only if this method returns true.
/// Should return true if the new anchor was added, otherwise false.
public virtual bool TryAddAnchor(Pose pose, out XRAnchor anchor)
{
anchor = default(XRAnchor);
return false;
}
///
/// Should create a new anchor "attached" to the trackable with id .
/// The behavior of the anchor depends on the type of trackable to which this anchor is attached and
/// might be implementation-defined.
///
/// The id of the trackable to which to attach.
/// The pose, in session space, of the anchor to create.
/// The new anchor. Must be valid only if this method returns true.
/// true if the new anchor was added, otherwise false.
public virtual bool TryAttachAnchor(
TrackableId trackableToAffix,
Pose pose,
out XRAnchor anchor)
{
anchor = default(XRAnchor);
return false;
}
///
/// Should remove an existing anchor with .
///
/// The id of an existing anchor to remove.
/// Should return true if the anchor was removed, otherwise false. If the anchor
/// does not exist, return false.
public virtual bool TryRemoveAnchor(TrackableId anchorId) => false;
}
#if DEVELOPMENT_BUILD || UNITY_EDITOR
ValidationUtility m_ValidationUtility =
new ValidationUtility();
#endif
}
}