XRLoaderHelper.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. using UnityEngine;
  9. using UnityEngine.Rendering;
  10. using UnityEngine.XR;
  11. namespace UnityEngine.XR.Management
  12. {
  13. /// <summary>
  14. /// XR Loader abstract subclass used as a base class for specific provider implementations. Class provides some
  15. /// helper logic that can be used to handle subsystem handling in a typesafe manner, reducing potential boilerplate
  16. /// code.
  17. /// </summary>
  18. public abstract class XRLoaderHelper : XRLoader
  19. {
  20. /// <summary>
  21. /// Map of loaded susbsystems. Used so we don't always have to fo to XRSubsystemManger and do a manual
  22. /// search to find the instance we loaded.
  23. /// </summary>
  24. protected Dictionary<Type, ISubsystem> m_SubsystemInstanceMap = new Dictionary<Type, ISubsystem>();
  25. /// <summary>
  26. /// Gets the loaded subsystem of the specified type. Implementation dependent as only implemetnations
  27. /// know what they have loaded and how best to get it..
  28. /// </summary>
  29. ///
  30. /// <typeparam name="T">Type of the subsystem to get.</typeparam>
  31. ///
  32. /// <returns>The loaded subsystem or null if not found.</returns>
  33. public override T GetLoadedSubsystem<T>()
  34. {
  35. Type subsystemType = typeof(T);
  36. ISubsystem subsystem;
  37. m_SubsystemInstanceMap.TryGetValue(subsystemType, out subsystem);
  38. return subsystem as T;
  39. }
  40. /// <summary>
  41. /// Start a subsystem instance of a given type. Subsystem assumed to already be loaded from
  42. /// a previous call to CreateSubsystem
  43. /// </summary>
  44. ///
  45. /// <typeparam name="T">A subclass of <see cref="ISubsystem"/></typeparam>
  46. protected void StartSubsystem<T>() where T : class, ISubsystem
  47. {
  48. T subsystem = GetLoadedSubsystem<T>();
  49. if (subsystem != null)
  50. subsystem.Start();
  51. }
  52. /// <summary>
  53. /// Stop a subsystem instance of a given type. Subsystem assumed to already be loaded from
  54. /// a previous call to CreateSubsystem
  55. /// </summary>
  56. ///
  57. /// <typeparam name="T">A subclass of <see cref="ISubsystem"/></typeparam>
  58. protected void StopSubsystem<T>() where T : class, ISubsystem
  59. {
  60. T subsystem = GetLoadedSubsystem<T>();
  61. if (subsystem != null)
  62. subsystem.Stop();
  63. }
  64. /// <summary>
  65. /// Destroy a subsystem instance of a given type. Subsystem assumed to already be loaded from
  66. /// a previous call to CreateSubsystem
  67. /// </summary>
  68. ///
  69. /// <typeparam name="T">A subclass of <see cref="ISubsystem"/></typeparam>
  70. protected void DestroySubsystem<T>() where T : class, ISubsystem
  71. {
  72. T subsystem = GetLoadedSubsystem<T>();
  73. if (subsystem != null)
  74. subsystem.Destroy();
  75. }
  76. /// <summary>
  77. /// Creates a subsystem given a list of descriptors and a specific subsystem id.
  78. ///
  79. /// You should make sure to destroy any subsystem that you created so that resources
  80. /// acquired by your subsystems are correctly cleaned up and released. This is especially important
  81. /// if you create them during initialization, but initialization fails. If that happens,
  82. /// you should clean up any subsystems created up to that point.
  83. /// </summary>
  84. ///
  85. /// <typeparam name="TDescriptor">The descriptor type being passed in.</typeparam>
  86. /// <typeparam name="TSubsystem">The subsystem type being requested</typeparam>
  87. /// <param name="descriptors">List of TDescriptor instances to use for subsystem matching.</param>
  88. /// <param name="id">The identifier key of the particualr subsystem implementation being requested.</param>
  89. protected void CreateSubsystem<TDescriptor, TSubsystem>(List<TDescriptor> descriptors, string id)
  90. where TDescriptor : ISubsystemDescriptor
  91. where TSubsystem : ISubsystem
  92. {
  93. if (descriptors == null)
  94. throw new ArgumentNullException("descriptors");
  95. SubsystemManager.GetSubsystemDescriptors<TDescriptor>(descriptors);
  96. if (descriptors.Count > 0)
  97. {
  98. foreach (var descriptor in descriptors)
  99. {
  100. ISubsystem subsys = null;
  101. if (String.Compare(descriptor.id, id, true) == 0)
  102. {
  103. subsys = descriptor.Create();
  104. }
  105. if (subsys != null)
  106. {
  107. m_SubsystemInstanceMap[typeof(TSubsystem)] = subsys;
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// Creates a native, integrated subsystem given a list of descriptors and a specific subsystem id.
  115. /// DEPRECATED: Please use the geenric CreateSubsystem method. This method is soley retained for
  116. /// backwards compatibility and will be removed in a future release.
  117. /// </summary>
  118. ///
  119. /// <typeparam name="TDescriptor">The descriptor type being passed in.</typeparam>
  120. /// <typeparam name="TSubsystem">The subsystem type being requested</typeparam>
  121. /// <param name="descriptors">List of TDescriptor instances to use for subsystem matching.</param>
  122. /// <param name="id">The identifier key of the particualr subsystem implementation being requested.</param>
  123. [Obsolete("This method is obsolete. Please use the geenric CreateSubsystem method.", false)]
  124. protected void CreateIntegratedSubsystem<TDescriptor, TSubsystem>(List<TDescriptor> descriptors, string id)
  125. where TDescriptor : IntegratedSubsystemDescriptor
  126. where TSubsystem : IntegratedSubsystem
  127. {
  128. CreateSubsystem<TDescriptor, TSubsystem>(descriptors, id);
  129. }
  130. /// <summary>
  131. /// Creates a managed, standalone subsystem given a list of descriptors and a specific subsystem id.
  132. /// DEPRECATED: Please use the geenric CreateSubsystem method. This method is soley retained for
  133. /// backwards compatibility and will be removed in a future release.
  134. /// </summary>
  135. ///
  136. /// <typeparam name="TDescriptor">The descriptor type being passed in.</typeparam>
  137. /// <typeparam name="TSubsystem">The subsystem type being requested</typeparam>
  138. /// <param name="descriptors">List of TDescriptor instances to use for subsystem matching.</param>
  139. /// <param name="id">The identifier key of the particualr subsystem implementation being requested.</param>
  140. [Obsolete("This method is obsolete. Please use the generic CreateSubsystem method.", false)]
  141. protected void CreateStandaloneSubsystem<TDescriptor, TSubsystem>(List<TDescriptor> descriptors, string id)
  142. where TDescriptor : SubsystemDescriptor
  143. where TSubsystem : Subsystem
  144. {
  145. CreateSubsystem<TDescriptor, TSubsystem>(descriptors, id);
  146. }
  147. /// <summary>
  148. /// Override of <see cref="DeInitialize"/> to provide for clearing the instance map.true
  149. ///
  150. /// If you override <see cref="DeInitialize"/> in your subclass, you must call the base
  151. /// implementation to allow the instance map tp be cleaned up correctly.
  152. /// </summary>
  153. ///
  154. /// <returns>True if de-initialization was successful.</returns>
  155. public override bool Deinitialize()
  156. {
  157. m_SubsystemInstanceMap.Clear();
  158. return base.Deinitialize();
  159. }
  160. #if UNITY_EDITOR
  161. virtual public void WasAssignedToBuildTarget(BuildTargetGroup buildTargetGroup)
  162. {
  163. }
  164. virtual public void WasUnassignedFromBuildTarget(BuildTargetGroup buildTargetGroup)
  165. {
  166. }
  167. #endif
  168. }
  169. }