123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System.Collections;
- using UnityEngine;
- using UnityEngine.AdaptivePerformance;
- using UnityEngine.UI;
-
- public class LifecycleControl : MonoBehaviour
- {
- private IAdaptivePerformance ap;
-
- public Toggle adaptivePerformanceInitialized;
- public Toggle adaptivePerformanceRunning;
-
- public Text provider;
- public Toggle providerInitialized;
- public Toggle providerRunning;
-
- public Text lastLifecycleEventReceived;
- public Text lastCommandAttempted;
-
- void Start()
- {
- Holder.LifecycleEventHandler += OnAdaptivePerformanceLifecycleEventHandler;
-
- StartCoroutine(TestTimeout());
-
- // if "Initialize Adaptive Performance on Startup" is unchecked, early exit will occur here
- // and Adaptive Performance must be initialized manually
- if (Holder.Instance == null)
- return;
-
- ap = Holder.Instance;
- UpdateAdaptivePerformanceStateInfo();
-
- // subscribe to any event handlers on new instance
- // example: ap.ThermalStatus.ThermalEvent += OnThermalEvent;
- }
-
- void OnDestroy()
- {
- Holder.LifecycleEventHandler -= OnAdaptivePerformanceLifecycleEventHandler;
- Holder.Deinitialize();
- }
-
- void OnAdaptivePerformanceLifecycleEventHandler(IAdaptivePerformance instance, LifecycleChangeType changeType)
- {
- if (changeType == LifecycleChangeType.Destroyed)
- {
- // unsubscribe from any event handlers on old instance
- // example: ap.ThermalStatus.ThermalEvent -= OnThermalEvent;
-
- ap = null;
- }
- else if(changeType == LifecycleChangeType.Created)
- {
- ap = instance;
-
- // reset any settings
- ap.DevelopmentSettings.Logging = true;
- ap.DevicePerformanceControl.AutomaticPerformanceControl = false;
-
- // subscribe to any event handlers on new instance
- // example: ap.ThermalStatus.ThermalEvent += OnThermalEvent;
- }
-
- UpdateLastLifecycleEventReceived(changeType);
- UpdateAdaptivePerformanceStateInfo();
- }
-
- IEnumerator TestTimeout()
- {
- while (true)
- {
- yield return new WaitForSeconds(300);
- #if UNITY_EDITOR
- UnityEditor.EditorApplication.isPlaying = false;
- #else
- Application.Quit();
- #endif
- }
- }
-
- private void UpdateAdaptivePerformanceStateInfo()
- {
- var settings = AdaptivePerformanceGeneralSettings.Instance;
-
- adaptivePerformanceInitialized.isOn = ap != null && ap.Initialized;
- adaptivePerformanceRunning.isOn = ap != null && ap.Active;
-
- var activeLoader = $"{settings.Manager.activeLoader}";
- if (string.IsNullOrWhiteSpace(activeLoader))
- activeLoader = "N/A";
-
- provider.text = activeLoader;
- providerInitialized.isOn = settings.IsProviderInitialized;
- providerRunning.isOn = settings.IsProviderStarted;
- }
-
- private void UpdateLastCommandAttemptInfo(string command)
- {
- lastCommandAttempted.text = $"Last Command: {command}";
- }
-
- private void UpdateLastLifecycleEventReceived(LifecycleChangeType changeType)
- {
- lastLifecycleEventReceived.text = $"Last Lifecycle Event: {changeType}";
- }
-
- public void InitializeAdaptivePerformance()
- {
- if (ap != null || Holder.Instance != null)
- {
- Debug.Log("Adaptive Performance is already initialized.");
- return;
- }
-
- Holder.Initialize();
-
- UpdateLastCommandAttemptInfo("Initialize");
- UpdateAdaptivePerformanceStateInfo();
- }
-
- public void DeinitializeAdaptivePerformance()
- {
- if (ap == null)
- {
- Debug.Log("You must initialize Adaptive Performance before attempting to deinitialize.");
- return;
- }
-
- Holder.Deinitialize();
-
- UpdateLastCommandAttemptInfo("Deinitialize");
- UpdateAdaptivePerformanceStateInfo();
- }
-
- public void StartAdaptivePerformance()
- {
- if (ap == null)
- {
- Debug.Log("You must initialize Adaptive Performance before attempting to start.");
- return;
- }
-
- if (ap.Active)
- {
- Debug.Log("Adaptive Performance is already running.");
- return;
- }
-
- ap.StartAdaptivePerformance();
-
- UpdateLastCommandAttemptInfo("Start");
- UpdateAdaptivePerformanceStateInfo();
- }
-
- public void StopAdaptivePerformance()
- {
- if (ap == null)
- {
- Debug.Log("You must start Adaptive Performance before attempting to stop.");
- return;
- }
-
- if (!ap.Active)
- {
- Debug.Log("Adaptive Performance is already stopped.");
- return;
- }
-
- ap.StopAdaptivePerformance();
-
- UpdateLastCommandAttemptInfo("Stop");
- UpdateAdaptivePerformanceStateInfo();
- }
- }
|