using System; using System.Collections; using System.Collections.Generic; using System.Reflection; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; using UnityEngine.Rendering; using UnityEngine.XR; namespace UnityEngine.XR.Management { /// /// XR Loader abstract subclass used as a base class for specific provider implementations. Class provides some /// helper logic that can be used to handle subsystem handling in a typesafe manner, reducing potential boilerplate /// code. /// public abstract class XRLoaderHelper : XRLoader { /// /// Map of loaded susbsystems. Used so we don't always have to fo to XRSubsystemManger and do a manual /// search to find the instance we loaded. /// protected Dictionary m_SubsystemInstanceMap = new Dictionary(); /// /// Gets the loaded subsystem of the specified type. Implementation dependent as only implemetnations /// know what they have loaded and how best to get it.. /// /// /// Type of the subsystem to get. /// /// The loaded subsystem or null if not found. public override T GetLoadedSubsystem() { Type subsystemType = typeof(T); ISubsystem subsystem; m_SubsystemInstanceMap.TryGetValue(subsystemType, out subsystem); return subsystem as T; } /// /// Start a subsystem instance of a given type. Subsystem assumed to already be loaded from /// a previous call to CreateSubsystem /// /// /// A subclass of protected void StartSubsystem() where T : class, ISubsystem { T subsystem = GetLoadedSubsystem(); if (subsystem != null) subsystem.Start(); } /// /// Stop a subsystem instance of a given type. Subsystem assumed to already be loaded from /// a previous call to CreateSubsystem /// /// /// A subclass of protected void StopSubsystem() where T : class, ISubsystem { T subsystem = GetLoadedSubsystem(); if (subsystem != null) subsystem.Stop(); } /// /// Destroy a subsystem instance of a given type. Subsystem assumed to already be loaded from /// a previous call to CreateSubsystem /// /// /// A subclass of protected void DestroySubsystem() where T : class, ISubsystem { T subsystem = GetLoadedSubsystem(); if (subsystem != null) subsystem.Destroy(); } /// /// Creates a subsystem given a list of descriptors and a specific subsystem id. /// /// You should make sure to destroy any subsystem that you created so that resources /// acquired by your subsystems are correctly cleaned up and released. This is especially important /// if you create them during initialization, but initialization fails. If that happens, /// you should clean up any subsystems created up to that point. /// /// /// The descriptor type being passed in. /// The subsystem type being requested /// List of TDescriptor instances to use for subsystem matching. /// The identifier key of the particualr subsystem implementation being requested. protected void CreateSubsystem(List descriptors, string id) where TDescriptor : ISubsystemDescriptor where TSubsystem : ISubsystem { if (descriptors == null) throw new ArgumentNullException("descriptors"); SubsystemManager.GetSubsystemDescriptors(descriptors); if (descriptors.Count > 0) { foreach (var descriptor in descriptors) { ISubsystem subsys = null; if (String.Compare(descriptor.id, id, true) == 0) { subsys = descriptor.Create(); } if (subsys != null) { m_SubsystemInstanceMap[typeof(TSubsystem)] = subsys; break; } } } } /// /// Creates a native, integrated subsystem given a list of descriptors and a specific subsystem id. /// DEPRECATED: Please use the geenric CreateSubsystem method. This method is soley retained for /// backwards compatibility and will be removed in a future release. /// /// /// The descriptor type being passed in. /// The subsystem type being requested /// List of TDescriptor instances to use for subsystem matching. /// The identifier key of the particualr subsystem implementation being requested. [Obsolete("This method is obsolete. Please use the geenric CreateSubsystem method.", false)] protected void CreateIntegratedSubsystem(List descriptors, string id) where TDescriptor : IntegratedSubsystemDescriptor where TSubsystem : IntegratedSubsystem { CreateSubsystem(descriptors, id); } /// /// Creates a managed, standalone subsystem given a list of descriptors and a specific subsystem id. /// DEPRECATED: Please use the geenric CreateSubsystem method. This method is soley retained for /// backwards compatibility and will be removed in a future release. /// /// /// The descriptor type being passed in. /// The subsystem type being requested /// List of TDescriptor instances to use for subsystem matching. /// The identifier key of the particualr subsystem implementation being requested. [Obsolete("This method is obsolete. Please use the generic CreateSubsystem method.", false)] protected void CreateStandaloneSubsystem(List descriptors, string id) where TDescriptor : SubsystemDescriptor where TSubsystem : Subsystem { CreateSubsystem(descriptors, id); } /// /// Override of to provide for clearing the instance map.true /// /// If you override in your subclass, you must call the base /// implementation to allow the instance map tp be cleaned up correctly. /// /// /// True if de-initialization was successful. public override bool Deinitialize() { m_SubsystemInstanceMap.Clear(); return base.Deinitialize(); } #if UNITY_EDITOR virtual public void WasAssignedToBuildTarget(BuildTargetGroup buildTargetGroup) { } virtual public void WasUnassignedFromBuildTarget(BuildTargetGroup buildTargetGroup) { } #endif } }