using System; using Unity.Collections; using UnityEngine.SubsystemsImplementation; namespace UnityEngine.XR.ARSubsystems { /// /// Base class for object tracking subsystems. /// /// /// This subsystem allows real objects to be recognized in the environment. /// You must first specify a library of "reference objects" to search for. /// These reference objects are typically in a format specific to a particular /// implementation; see the documentation for the implementing subsystem for /// further instructions. /// public class XRObjectTrackingSubsystem : TrackingSubsystem { /// /// For AR implementors: implement this class to provide support for object tracking. /// public abstract class Provider : SubsystemProvider { /// /// Get the changes to s (added, updated, and removed) /// since the last call to this method. This is typically invoked once per frame. /// /// A 'template' . /// might have fields added to it in the future; this template allows you to fill /// the arrays of added, updated, and removed with default values before copying in /// data from your own memory buffer. /// The allocator to use for the added, updated, and removed arrays. /// A new containing the changes since the last /// call to this method, allocated with . public abstract TrackableChanges GetChanges(XRTrackedObject template, Allocator allocator); /// /// The library that contains the reference objects for which to scan. /// If this is not null, the provider should begin scanning for the /// objects in the library. If null, the provider should stop /// scanning for objects. /// public virtual XRReferenceObjectLibrary library { set {} } } /// /// Constructs an object tracking subsystem. Do not invoke directly; call Create on the instead. /// public XRObjectTrackingSubsystem() { } /// /// Starts scanning for the reference objects in . /// /// Thrown if is null. protected sealed override void OnStart() { if (m_Library == null) throw new InvalidOperationException("Cannot start object tracking without an object library."); provider.library = m_Library; base.OnStart(); } /// /// The library of reference objects for which to scan. This must be set to non-null /// before calling . /// /// Thrown if you set the library to null while the subsystem is running. public XRReferenceObjectLibrary library { get => m_Library; set { if (m_Library == value) return; if (running && value == null) throw new ArgumentNullException("Cannot set library to null while subsystem is running."); m_Library = value; // If we are running, then we want to switch the current library if (running) provider.library = m_Library; } } /// /// Stops scanning for objects. /// protected override sealed void OnStop() { provider.library = null; base.OnStop(); } /// /// Get changes to s (added, updated, and removed) since /// the last call to this method. The caller owns the memory allocated with . /// /// The allocator to use for the returned arrays of changes. /// A new allocated with . /// The caller owns the memory and is responsible for calling Dispose on it. public sealed override TrackableChanges GetChanges(Allocator allocator) { var changes = provider.GetChanges(XRTrackedObject.defaultValue, allocator); #if DEVELOPMENT_BUILD || UNITY_EDITOR m_ValidationUtility.ValidateAndDisposeIfThrown(changes); #endif return changes; } /// /// Registers an implementation of the . /// /// The concrete type of the subsystem being registered. /// A unique string identifying the subsystem implementation. /// Describes the capabilities of the implementation. /// Thrown if is null. [Obsolete("XRObjectTrackingSubsystem.Register(string id, XRObjectTrackingSubsystemDescriptor.Capabilities capabilities) has been deprecated in AR Foundation version 6.0. Instead use XRObjectTrackingSubsystemDescriptor.Register(XRObjectTrackingSubsystemDescriptor.Cinfo cinfo)", false)] public static void Register(string id, XRObjectTrackingSubsystemDescriptor.Capabilities capabilities) where T : XRObjectTrackingSubsystem.Provider { if (id == null) throw new ArgumentNullException(nameof(id)); XRObjectTrackingSubsystemDescriptor.Register( new XRObjectTrackingSubsystemDescriptor.Cinfo() { id = id, providerType = typeof(T), subsystemTypeOverride = null }); } /// /// Registers a new implementation of the . /// Allows overriding a derived type of XRObjectTrackingSubsystem. /// /// The concrete type of the provider being registered. /// The concrete type of the subsystem being registered. /// A unique string identifying the subsystem implementation. /// Describes the capabilities of the implementation. /// Thrown if is null. [Obsolete("XRObjectTrackingSubsystem.Register(string id, XRObjectTrackingSubsystemDescriptor.Capabilities capabilities) has been deprecated in AR Foundation version 6.0. Instead use XRObjectTrackingSubsystemDescriptor.Register(XRObjectTrackingSubsystemDescriptor.Cinfo cinfo)", false)] public static void Register(string id, XRObjectTrackingSubsystemDescriptor.Capabilities capabilities) where TProvider : XRObjectTrackingSubsystem.Provider where TSubsystemOverride : XRObjectTrackingSubsystem { if (id == null) throw new ArgumentNullException(nameof(id)); XRObjectTrackingSubsystemDescriptor.Register( new XRObjectTrackingSubsystemDescriptor.Cinfo() { id = id, providerType = typeof(TProvider), subsystemTypeOverride = typeof(TSubsystemOverride) }); } XRReferenceObjectLibrary m_Library; #if DEVELOPMENT_BUILD || UNITY_EDITOR ValidationUtility m_ValidationUtility = new ValidationUtility(); #endif } }