XRGeneralSettings.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. namespace UnityEngine.XR.Management
  8. {
  9. /// <summary>General settings container used to house the instance of the active settings as well as the manager
  10. /// instance used to load the loaders with.
  11. /// </summary>
  12. public class XRGeneralSettings : ScriptableObject
  13. {
  14. /// <summary>The key used to query to get the current loader settings.</summary>
  15. public static string k_SettingsKey = "com.unity.xr.management.loader_settings";
  16. internal static XRGeneralSettings s_RuntimeSettingsInstance = null;
  17. [SerializeField]
  18. internal XRManagerSettings m_LoaderManagerInstance = null;
  19. [SerializeField]
  20. [Tooltip("Toggling this on/off will enable/disable the automatic startup of XR at run time.")]
  21. internal bool m_InitManagerOnStart = true;
  22. /// <summary>The current active manager used to manage XR lifetime.</summary>
  23. public XRManagerSettings Manager
  24. {
  25. get { return m_LoaderManagerInstance; }
  26. set { m_LoaderManagerInstance = value; }
  27. }
  28. private XRManagerSettings m_XRManager = null;
  29. #pragma warning disable 414 // Suppress warning for needed variables.
  30. private bool m_ProviderIntialized = false;
  31. private bool m_ProviderStarted = false;
  32. #pragma warning restore 414
  33. /// <summary>The current settings instance.</summary>
  34. public static XRGeneralSettings Instance
  35. {
  36. get
  37. {
  38. return s_RuntimeSettingsInstance;
  39. }
  40. #if UNITY_EDITOR
  41. set
  42. {
  43. s_RuntimeSettingsInstance = value;
  44. }
  45. #endif
  46. }
  47. /// <summary>The current active manager used to manage XR lifetime.</summary>
  48. public XRManagerSettings AssignedSettings
  49. {
  50. get
  51. {
  52. return m_LoaderManagerInstance;
  53. }
  54. #if UNITY_EDITOR
  55. set
  56. {
  57. m_LoaderManagerInstance = value;
  58. }
  59. #endif
  60. }
  61. /// <summary>Used to set if the manager is activated and initialized on startup.</summary>
  62. public bool InitManagerOnStart
  63. {
  64. get
  65. {
  66. return m_InitManagerOnStart;
  67. }
  68. #if UNITY_EDITOR
  69. set
  70. {
  71. m_InitManagerOnStart = value;
  72. }
  73. #endif
  74. }
  75. #if !UNITY_EDITOR
  76. void Awake()
  77. {
  78. Debug.Log("XRGeneral Settings awakening...");
  79. s_RuntimeSettingsInstance = this;
  80. Application.quitting += Quit;
  81. DontDestroyOnLoad(s_RuntimeSettingsInstance);
  82. }
  83. #endif
  84. #if UNITY_EDITOR
  85. /// <summary>For internal use only.</summary>
  86. [System.Obsolete("Deprecating internal only API.")]
  87. public void InternalPauseStateChanged(PauseState state)
  88. {
  89. throw new NotImplementedException();
  90. }
  91. /// <summary>For internal use only.</summary>
  92. public void InternalPlayModeStateChanged(PlayModeStateChange state)
  93. {
  94. switch (state)
  95. {
  96. case PlayModeStateChange.ExitingPlayMode:
  97. Quit();
  98. break;
  99. case PlayModeStateChange.ExitingEditMode:
  100. case PlayModeStateChange.EnteredPlayMode:
  101. case PlayModeStateChange.EnteredEditMode:
  102. break;
  103. }
  104. }
  105. #endif
  106. static void Quit()
  107. {
  108. XRGeneralSettings instance = XRGeneralSettings.Instance;
  109. if (instance == null)
  110. return;
  111. instance.DeInitXRSDK();
  112. }
  113. void Start()
  114. {
  115. StartXRSDK();
  116. }
  117. void OnDestroy()
  118. {
  119. DeInitXRSDK();
  120. }
  121. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
  122. internal static void AttemptInitializeXRSDKOnLoad()
  123. {
  124. XRGeneralSettings instance = XRGeneralSettings.Instance;
  125. if (instance == null || !instance.InitManagerOnStart)
  126. return;
  127. instance.InitXRSDK();
  128. }
  129. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
  130. internal static void AttemptStartXRSDKOnBeforeSplashScreen()
  131. {
  132. XRGeneralSettings instance = XRGeneralSettings.Instance;
  133. if (instance == null || !instance.InitManagerOnStart)
  134. return;
  135. instance.StartXRSDK();
  136. }
  137. private void InitXRSDK()
  138. {
  139. if (XRGeneralSettings.Instance == null || XRGeneralSettings.Instance.m_LoaderManagerInstance == null || XRGeneralSettings.Instance.m_InitManagerOnStart == false)
  140. return;
  141. m_XRManager = XRGeneralSettings.Instance.m_LoaderManagerInstance;
  142. if (m_XRManager == null)
  143. {
  144. Debug.LogError("Assigned GameObject for XR Management loading is invalid. No XR Providers will be automatically loaded.");
  145. return;
  146. }
  147. m_XRManager.automaticLoading = false;
  148. m_XRManager.automaticRunning = false;
  149. m_XRManager.InitializeLoaderSync();
  150. m_ProviderIntialized = true;
  151. }
  152. private void StartXRSDK()
  153. {
  154. if (m_XRManager != null && m_XRManager.activeLoader != null)
  155. {
  156. m_XRManager.StartSubsystems();
  157. m_ProviderStarted = true;
  158. }
  159. }
  160. private void StopXRSDK()
  161. {
  162. if (m_XRManager != null && m_XRManager.activeLoader != null)
  163. {
  164. m_XRManager.StopSubsystems();
  165. m_ProviderStarted = false;
  166. }
  167. }
  168. private void DeInitXRSDK()
  169. {
  170. if (m_XRManager != null && m_XRManager.activeLoader != null)
  171. {
  172. m_XRManager.DeinitializeLoader();
  173. m_XRManager = null;
  174. m_ProviderIntialized = false;
  175. }
  176. }
  177. }
  178. }