using System; using Unity.Collections; namespace UnityEngine.XR.ARSubsystems { /// /// Base class for a raycast subsystem. /// /// /// This abstract class should be implemented by an XR provider and instantiated using the SubsystemManager /// to enumerate the available s. /// public abstract class XRRaycastSubsystem : XRSubsystem { /// /// Constructor. Do not invoke directly; use the SubsystemManager /// to enumerate the available s /// and call Create on the desired descriptor. /// public XRRaycastSubsystem() => m_Provider = CreateProvider(); /// /// Starts the subsystem. /// protected sealed override void OnStart() => m_Provider.Start(); /// /// Stops the subsystem. /// protected sealed override void OnStop() => m_Provider.Stop(); /// /// Destroys the subsystem. /// protected sealed override void OnDestroyed() => m_Provider.Destroy(); /// /// Casts against trackables specified with . /// /// A ray in session space. /// The types of trackables to test for ray intersections. /// The Allocator used to allocate the returned NativeArray. /// A NativeArray of all the resulting ray intersections. public NativeArray Raycast( Ray ray, TrackableType trackableTypeMask, Allocator allocator) { return m_Provider.Raycast(XRRaycastHit.defaultValue, ray, trackableTypeMask, allocator); } /// /// Casts a ray originating from against trackables specified with . /// /// A point on the screen in normalized screen coordinates (0, 0) - (1, 1) /// The types of trackables to test for ray intersections. /// The Allocator used to allocate the returned NativeArray. /// A NativeArray of all the resulting ray intersections. public NativeArray Raycast( Vector2 screenPoint, TrackableType trackableTypeMask, Allocator allocator) { return m_Provider.Raycast(XRRaycastHit.defaultValue, screenPoint, trackableTypeMask, allocator); } /// /// Should return an instance of . /// /// The interface to the implementation-specific provider. protected abstract Provider CreateProvider(); /// /// An interface to be implemented by providers of this subsystem. /// protected class Provider { /// /// Called when the subsystem is started. Will not be called again until . /// public virtual void Start() { } /// /// Called when the subsystem is stopped. Will not be called before . /// public virtual void Stop() { } /// /// Called when the subsystem is destroyed. will be called first if the subsystem is running. /// public virtual void Destroy() { } /// /// Performs a raycast from an arbitrary ray against the types /// specified by . Results /// should be sorted by distance from the ray origin. /// /// The default raycast hit that should be used as a template when populating the returned NativeArray. /// A ray in session space from which to raycast. /// The types to raycast against. /// The allocator with which to allocate the returned NativeArray. public virtual NativeArray Raycast( XRRaycastHit defaultRaycastHit, Ray ray, TrackableType trackableTypeMask, Allocator allocator) { throw new NotSupportedException("Raycasting using a Ray is not supported."); } /// /// Performs a raycast from the camera against the types /// specified by . Results /// should be sorted by distance from the ray origin. /// /// The default raycast hit that should be used as a template when populating the returned NativeArray. /// A point on the screen in normalized (0...1) coordinates /// The types to raycast against. /// The allocator with which to allocate the returned NativeArray. public virtual NativeArray Raycast( XRRaycastHit defaultRaycastHit, Vector2 screenPoint, TrackableType trackableTypeMask, Allocator allocator) { throw new NotSupportedException("Raycasting using a screen point is not supported."); } } Provider m_Provider; } }