using System;
namespace UnityEngine.XR.ARSubsystems
{
///
/// This subsystem controls the lifecycle of an XR session. Some platforms,
/// particularly those that have non-XR modes, need to be able to turn the
/// session on and off to enter and exit XR mode(s) of operation.
///
public abstract class XRSessionSubsystem : XRSubsystem
{
///
/// Returns an implementation-defined pointer associated with the session.
///
public IntPtr nativePtr => m_Provider.nativePtr;
///
/// Returns a unique session identifier for this session.
///
public Guid sessionId => m_Provider.sessionId;
///
/// Asynchronously retrieves the . Used to determine whether
/// the current device supports XR and if the necessary software is installed.
///
///
/// This platform-agnostic method is typically implemented by a platform-specific package.
///
/// A which can be used to determine when the
/// availability has been determined and retrieve the result.
public Promise GetAvailabilityAsync() => m_Provider.GetAvailabilityAsync();
///
/// Asynchronously attempts to install XR software on the current device.
/// Throws if is false.
///
///
/// This platform-agnostic method is typically implemented by a platform-specific package.
///
/// A which can be used to determine when the
/// installation completes and retrieve the result.
public Promise InstallAsync()
{
if (!SubsystemDescriptor.supportsInstall)
throw new NotSupportedException("InstallAsync is not supported on this platform.");
return m_Provider.InstallAsync();
}
///
/// Do not call this directly. Call create on a valid instead.
///
public XRSessionSubsystem() => m_Provider = CreateProvider();
///
/// Starts or resumes the session.
///
protected sealed override void OnStart() => m_Provider.Resume();
///
/// Restarts a session. and pause and resume
/// a session, respectively. Restart resets the session state and clears
/// and any detected trackables.
///
public void Reset() => m_Provider.Reset();
///
/// Pauses the session.
///
protected sealed override void OnStop() => m_Provider.Pause();
///
/// Destroys the session.
///
protected sealed override void OnDestroyed() => m_Provider.Destroy();
///
/// Trigger the session's update loop.
///
/// Data needed by the session to perform its update.
public void Update(XRSessionUpdateParams updateParams) => m_Provider.Update(updateParams);
///
/// Should be invoked when the application is paused.
///
public void OnApplicationPause() => m_Provider.OnApplicationPause();
///
/// Should be invoked when the application is resumed.
///
public void OnApplicationResume() => m_Provider.OnApplicationResume();
///
/// Gets the for the session.
///
public TrackingState trackingState => m_Provider.trackingState;
///
/// Gets the for the session.
///
public NotTrackingReason notTrackingReason => m_Provider.notTrackingReason;
///
/// Whether the AR session update is synchronized with the Unity frame rate.
/// If true, should block until the next AR frame is available.
///
/// Thrown if is False.
public bool matchFrameRate
{
get => m_Provider.matchFrameRate;
set => m_Provider.matchFrameRate = value;
}
///
/// The native update rate of the AR Session.
///
/// Thrown if is False.
public int frameRate => m_Provider.frameRate;
///
/// Implement this to provide this class with an interface to
/// platform specific implementations.
///
/// An implementation specific provider.
protected abstract Provider CreateProvider();
///
/// The API this subsystem uses to interop with
/// different provider implementations.
///
protected class Provider
{
///
/// Invoked to start or resume a session. This is different from .
///
public virtual void Resume() { }
///
/// Invoked to pause a running session. This is different from .
///
public virtual void Pause() { }
///
/// Perform any per-frame update logic here.
///
/// Paramters about the current state that may be needed to inform the session.
public virtual void Update(XRSessionUpdateParams updateParams) { }
///
/// Stop the session and destroy all associated resources.
///
public virtual void Destroy() { }
///
/// Reset the session. The behavior should be equivalent to destroying and recreating the session.
///
public virtual void Reset() { }
///
/// Invoked when the application is paused.
///
public virtual void OnApplicationPause() { }
///
/// Invoked when the application is resumed.
///
public virtual void OnApplicationResume() { }
///
/// Get a pointer to an object associated with the session.
/// Callers should be able to manipulate the session in their own code using this.
///
public virtual IntPtr nativePtr => IntPtr.Zero;
///
/// Get the session's availability, such as whether the platform supports XR.
///
/// A that the caller can yield on until availability is determined.
public virtual Promise GetAvailabilityAsync()
{
return Promise.CreateResolvedPromise(SessionAvailability.None);
}
///
/// Attempt to update or install necessary XR software. Will only be called if
/// is true.
///
///
public virtual Promise InstallAsync()
{
return Promise.CreateResolvedPromise(SessionInstallationStatus.ErrorInstallNotSupported);
}
///
/// Get the for the session.
///
public virtual TrackingState trackingState => TrackingState.None;
///
/// Get the for the session.
///
public virtual NotTrackingReason notTrackingReason => NotTrackingReason.Unsupported;
///
/// Get a unique identifier for this session
///
public virtual Guid sessionId => Guid.Empty;
///
/// Whether the AR session update is synchronized with the Unity frame rate.
/// If true, should block until the next AR frame is available.
/// Must be implemented if
///
/// is True.
///
public virtual bool matchFrameRate
{
get => false;
set
{
if (value)
{
throw new NotSupportedException("Matching frame rate is not supported.");
}
}
}
///
/// The native update rate of the AR Session. Must be implemented if
///
/// is True.
///
public virtual int frameRate =>
throw new NotSupportedException("Querying the frame rate is not supported by this session subsystem.");
}
Provider m_Provider;
}
}