123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using System;
- using System.Collections;
-
- using UnityEngine;
-
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
-
- namespace UnityEngine.AdaptivePerformance
- {
- /// <summary>
- /// General settings container used to house the instance of the active settings, as well as the manager
- /// instance used to load the loaders with.
- /// </summary>
- public class AdaptivePerformanceGeneralSettings : ScriptableObject
- {
- /// <summary>The key used to query to get the current loader settings.</summary>
- public static string k_SettingsKey = "com.unity.adaptiveperformance.loader_settings";
- internal static AdaptivePerformanceGeneralSettings s_RuntimeSettingsInstance = null;
-
- [SerializeField]
- internal AdaptivePerformanceManagerSettings m_LoaderManagerInstance = null;
-
- [SerializeField]
- [Tooltip("Enable this to automatically start up Adaptive Performance at runtime.")]
- internal bool m_InitManagerOnStart = true;
-
- /// <summary>The current active manager used to manage the Adaptive Performance lifetime.</summary>
- public AdaptivePerformanceManagerSettings Manager
- {
- get { return m_LoaderManagerInstance; }
- set { m_LoaderManagerInstance = value; }
- }
-
- private AdaptivePerformanceManagerSettings m_AdaptivePerformanceManager = null;
-
- #pragma warning disable 414 // Suppress warning for needed variables.
- private bool m_ProviderIntialized = false;
- private bool m_ProviderStarted = false;
- #pragma warning restore 414
-
- /// <summary>
- /// Indicates if provider loader has been initialized.
- /// </summary>
- public bool IsProviderInitialized
- {
- get { return m_ProviderIntialized; }
- }
-
- /// <summary>
- /// Indicates if provider loader and subsystem has been started.
- /// </summary>
- public bool IsProviderStarted
- {
- get { return m_ProviderStarted; }
- }
-
- /// <summary>The current settings instance.</summary>
- public static AdaptivePerformanceGeneralSettings Instance
- {
- get
- {
- return s_RuntimeSettingsInstance;
- }
- #if UNITY_EDITOR
- set
- {
- s_RuntimeSettingsInstance = value;
- }
- #endif
- }
-
- /// <summary>
- /// The current active manager used to manage the Adaptive Performance lifetime.
- /// </summary>
- public AdaptivePerformanceManagerSettings AssignedSettings
- {
- get
- {
- return m_LoaderManagerInstance;
- }
- #if UNITY_EDITOR
- set
- {
- m_LoaderManagerInstance = value;
- }
- #endif
- }
-
- /// <summary>
- /// Used to set if the manager is activated and initialized on startup.
- /// </summary>
- public bool InitManagerOnStart
- {
- get
- {
- return m_InitManagerOnStart;
- }
- #if UNITY_EDITOR
- set
- {
- m_InitManagerOnStart = value;
- }
- #endif
- }
-
-
- #if !UNITY_EDITOR
- void Awake()
- {
- s_RuntimeSettingsInstance = this;
- Application.quitting += Quit;
- DontDestroyOnLoad(s_RuntimeSettingsInstance);
- }
-
- #endif
-
- #if UNITY_EDITOR
- /// <summary>For internal use only.</summary>
- public void InternalPlayModeStateChanged(PlayModeStateChange state)
- {
- switch (state)
- {
- case PlayModeStateChange.ExitingPlayMode:
- Quit();
- break;
- case PlayModeStateChange.ExitingEditMode:
- case PlayModeStateChange.EnteredPlayMode:
- case PlayModeStateChange.EnteredEditMode:
- break;
- }
- }
-
- #endif
- static void Quit()
- {
- AdaptivePerformanceGeneralSettings instance = AdaptivePerformanceGeneralSettings.Instance;
- if (instance == null)
- return;
-
- instance.DeInitAdaptivePerformance();
- }
-
- void OnDestroy()
- {
- DeInitAdaptivePerformance();
- s_RuntimeSettingsInstance = null;
- }
-
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
- internal static void AttemptInitializeAdaptivePerformanceOnLoad()
- {
- AdaptivePerformanceGeneralSettings instance = AdaptivePerformanceGeneralSettings.Instance;
- if (instance == null || !instance.InitManagerOnStart)
- return;
-
- instance.InitAdaptivePerformance();
- }
-
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
- internal static void AttemptStartAdaptivePerformanceOnBeforeSplashScreen()
- {
- AdaptivePerformanceGeneralSettings instance = AdaptivePerformanceGeneralSettings.Instance;
- if (instance == null || !instance.InitManagerOnStart)
- return;
-
- instance.StartAdaptivePerformance();
- }
-
- internal void InitAdaptivePerformance()
- {
- if (m_ProviderIntialized)
- return;
-
- if (AdaptivePerformanceGeneralSettings.Instance == null)
- return;
-
- m_AdaptivePerformanceManager = AdaptivePerformanceGeneralSettings.Instance.m_LoaderManagerInstance;
- if (m_AdaptivePerformanceManager == null)
- {
- Debug.LogError("Assigned GameObject for Adaptive Performance Management loading is invalid. No Adaptive Performance Providers will be automatically loaded.");
- return;
- }
-
- m_AdaptivePerformanceManager.automaticLoading = false;
- m_AdaptivePerformanceManager.automaticRunning = false;
- m_AdaptivePerformanceManager.InitializeLoaderSync();
- m_ProviderIntialized = true;
- }
-
- internal void StartAdaptivePerformance()
- {
- if (!m_ProviderIntialized || m_ProviderStarted)
- return;
-
- if (m_AdaptivePerformanceManager == null || m_AdaptivePerformanceManager.activeLoader == null)
- return;
-
- m_AdaptivePerformanceManager.StartSubsystems();
- m_ProviderStarted = true;
- }
-
- internal void StopAdaptivePerformance()
- {
- if (!m_ProviderIntialized || !m_ProviderStarted)
- return;
-
- if (m_AdaptivePerformanceManager == null || m_AdaptivePerformanceManager.activeLoader == null)
- return;
-
- m_AdaptivePerformanceManager.StopSubsystems();
- m_ProviderStarted = false;
- }
-
- internal void DeInitAdaptivePerformance()
- {
- if (!m_ProviderIntialized)
- return;
-
- if (m_ProviderStarted)
- StopAdaptivePerformance();
-
- if (m_AdaptivePerformanceManager != null)
- {
- m_AdaptivePerformanceManager.DeinitializeLoader();
- m_AdaptivePerformanceManager = null;
- }
-
- m_ProviderIntialized = false;
- }
- }
- }
|