Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AdaptivePerformanceGeneralSettings.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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>The current settings instance.</summary>
  35. public static AdaptivePerformanceGeneralSettings Instance
  36. {
  37. get
  38. {
  39. return s_RuntimeSettingsInstance;
  40. }
  41. #if UNITY_EDITOR
  42. set
  43. {
  44. s_RuntimeSettingsInstance = value;
  45. }
  46. #endif
  47. }
  48. /// <summary>
  49. /// The current active manager used to manage the Adaptive Performance lifetime.
  50. /// </summary>
  51. public AdaptivePerformanceManagerSettings AssignedSettings
  52. {
  53. get
  54. {
  55. return m_LoaderManagerInstance;
  56. }
  57. #if UNITY_EDITOR
  58. set
  59. {
  60. m_LoaderManagerInstance = value;
  61. }
  62. #endif
  63. }
  64. /// <summary>
  65. /// Used to set if the manager is activated and initialized on startup.
  66. /// </summary>
  67. public bool InitManagerOnStart
  68. {
  69. get
  70. {
  71. return m_InitManagerOnStart;
  72. }
  73. #if UNITY_EDITOR
  74. set
  75. {
  76. m_InitManagerOnStart = value;
  77. }
  78. #endif
  79. }
  80. #if !UNITY_EDITOR
  81. void Awake()
  82. {
  83. s_RuntimeSettingsInstance = this;
  84. Application.quitting += Quit;
  85. DontDestroyOnLoad(s_RuntimeSettingsInstance);
  86. }
  87. #endif
  88. #if UNITY_EDITOR
  89. /// <summary>For internal use only.</summary>
  90. public void InternalPlayModeStateChanged(PlayModeStateChange state)
  91. {
  92. switch (state)
  93. {
  94. case PlayModeStateChange.ExitingPlayMode:
  95. Quit();
  96. break;
  97. case PlayModeStateChange.ExitingEditMode:
  98. case PlayModeStateChange.EnteredPlayMode:
  99. case PlayModeStateChange.EnteredEditMode:
  100. break;
  101. }
  102. }
  103. #endif
  104. static void Quit()
  105. {
  106. AdaptivePerformanceGeneralSettings instance = AdaptivePerformanceGeneralSettings.Instance;
  107. if (instance == null)
  108. return;
  109. instance.DeInitAdaptivePerformance();
  110. }
  111. void Start()
  112. {
  113. StartAdaptivePerformance();
  114. }
  115. void OnDestroy()
  116. {
  117. DeInitAdaptivePerformance();
  118. }
  119. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
  120. internal static void AttemptInitializeAdaptivePerformanceOnLoad()
  121. {
  122. AdaptivePerformanceGeneralSettings instance = AdaptivePerformanceGeneralSettings.Instance;
  123. if (instance == null || !instance.InitManagerOnStart)
  124. return;
  125. instance.InitAdaptivePerformance();
  126. }
  127. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
  128. internal static void AttemptStartAdaptivePerformanceOnBeforeSplashScreen()
  129. {
  130. AdaptivePerformanceGeneralSettings instance = AdaptivePerformanceGeneralSettings.Instance;
  131. if (instance == null || !instance.InitManagerOnStart)
  132. return;
  133. instance.StartAdaptivePerformance();
  134. }
  135. private void InitAdaptivePerformance()
  136. {
  137. if (AdaptivePerformanceGeneralSettings.Instance == null || AdaptivePerformanceGeneralSettings.Instance.m_LoaderManagerInstance == null || AdaptivePerformanceGeneralSettings.Instance.m_InitManagerOnStart == false)
  138. return;
  139. m_AdaptivePerformanceManager = AdaptivePerformanceGeneralSettings.Instance.m_LoaderManagerInstance;
  140. if (m_AdaptivePerformanceManager == null)
  141. {
  142. Debug.LogError("Assigned GameObject for Adaptive Performance Management loading is invalid. No Adaptive Performance Providers will be automatically loaded.");
  143. return;
  144. }
  145. m_AdaptivePerformanceManager.automaticLoading = false;
  146. m_AdaptivePerformanceManager.automaticRunning = false;
  147. m_AdaptivePerformanceManager.InitializeLoaderSync();
  148. m_ProviderIntialized = true;
  149. }
  150. private void StartAdaptivePerformance()
  151. {
  152. if (m_AdaptivePerformanceManager != null && m_AdaptivePerformanceManager.activeLoader != null)
  153. {
  154. m_AdaptivePerformanceManager.StartSubsystems();
  155. m_ProviderStarted = true;
  156. }
  157. }
  158. private void StopAdaptivePerformance()
  159. {
  160. if (m_AdaptivePerformanceManager != null && m_AdaptivePerformanceManager.activeLoader != null)
  161. {
  162. m_AdaptivePerformanceManager.StopSubsystems();
  163. m_ProviderStarted = false;
  164. }
  165. }
  166. private void DeInitAdaptivePerformance()
  167. {
  168. if (m_AdaptivePerformanceManager != null && m_AdaptivePerformanceManager.activeLoader != null)
  169. {
  170. m_AdaptivePerformanceManager.DeinitializeLoader();
  171. m_AdaptivePerformanceManager = null;
  172. m_ProviderIntialized = false;
  173. }
  174. }
  175. }
  176. }