Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdaptivePerformanceGeneralSettings.cs 7.1KB

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