Нема описа
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.AdaptivePerformance;
  4. using UnityEngine.UI;
  5. public class LifecycleControl : MonoBehaviour
  6. {
  7. private IAdaptivePerformance ap;
  8. public Toggle adaptivePerformanceInitialized;
  9. public Toggle adaptivePerformanceRunning;
  10. public Text provider;
  11. public Toggle providerInitialized;
  12. public Toggle providerRunning;
  13. public Text lastLifecycleEventReceived;
  14. public Text lastCommandAttempted;
  15. void Start()
  16. {
  17. Holder.LifecycleEventHandler += OnAdaptivePerformanceLifecycleEventHandler;
  18. StartCoroutine(TestTimeout());
  19. // if "Initialize Adaptive Performance on Startup" is unchecked, early exit will occur here
  20. // and Adaptive Performance must be initialized manually
  21. if (Holder.Instance == null)
  22. return;
  23. ap = Holder.Instance;
  24. UpdateAdaptivePerformanceStateInfo();
  25. // subscribe to any event handlers on new instance
  26. // example: ap.ThermalStatus.ThermalEvent += OnThermalEvent;
  27. }
  28. void OnDestroy()
  29. {
  30. Holder.LifecycleEventHandler -= OnAdaptivePerformanceLifecycleEventHandler;
  31. Holder.Deinitialize();
  32. }
  33. void OnAdaptivePerformanceLifecycleEventHandler(IAdaptivePerformance instance, LifecycleChangeType changeType)
  34. {
  35. if (changeType == LifecycleChangeType.Destroyed)
  36. {
  37. // unsubscribe from any event handlers on old instance
  38. // example: ap.ThermalStatus.ThermalEvent -= OnThermalEvent;
  39. ap = null;
  40. }
  41. else if(changeType == LifecycleChangeType.Created)
  42. {
  43. ap = instance;
  44. // reset any settings
  45. ap.DevelopmentSettings.Logging = true;
  46. ap.DevicePerformanceControl.AutomaticPerformanceControl = false;
  47. // subscribe to any event handlers on new instance
  48. // example: ap.ThermalStatus.ThermalEvent += OnThermalEvent;
  49. }
  50. UpdateLastLifecycleEventReceived(changeType);
  51. UpdateAdaptivePerformanceStateInfo();
  52. }
  53. IEnumerator TestTimeout()
  54. {
  55. while (true)
  56. {
  57. yield return new WaitForSeconds(300);
  58. #if UNITY_EDITOR
  59. UnityEditor.EditorApplication.isPlaying = false;
  60. #else
  61. Application.Quit();
  62. #endif
  63. }
  64. }
  65. private void UpdateAdaptivePerformanceStateInfo()
  66. {
  67. var settings = AdaptivePerformanceGeneralSettings.Instance;
  68. adaptivePerformanceInitialized.isOn = ap != null && ap.Initialized;
  69. adaptivePerformanceRunning.isOn = ap != null && ap.Active;
  70. var activeLoader = $"{settings.Manager.activeLoader}";
  71. if (string.IsNullOrWhiteSpace(activeLoader))
  72. activeLoader = "N/A";
  73. provider.text = activeLoader;
  74. providerInitialized.isOn = settings.IsProviderInitialized;
  75. providerRunning.isOn = settings.IsProviderStarted;
  76. }
  77. private void UpdateLastCommandAttemptInfo(string command)
  78. {
  79. lastCommandAttempted.text = $"Last Command: {command}";
  80. }
  81. private void UpdateLastLifecycleEventReceived(LifecycleChangeType changeType)
  82. {
  83. lastLifecycleEventReceived.text = $"Last Lifecycle Event: {changeType}";
  84. }
  85. public void InitializeAdaptivePerformance()
  86. {
  87. if (ap != null || Holder.Instance != null)
  88. {
  89. Debug.Log("Adaptive Performance is already initialized.");
  90. return;
  91. }
  92. Holder.Initialize();
  93. UpdateLastCommandAttemptInfo("Initialize");
  94. UpdateAdaptivePerformanceStateInfo();
  95. }
  96. public void DeinitializeAdaptivePerformance()
  97. {
  98. if (ap == null)
  99. {
  100. Debug.Log("You must initialize Adaptive Performance before attempting to deinitialize.");
  101. return;
  102. }
  103. Holder.Deinitialize();
  104. UpdateLastCommandAttemptInfo("Deinitialize");
  105. UpdateAdaptivePerformanceStateInfo();
  106. }
  107. public void StartAdaptivePerformance()
  108. {
  109. if (ap == null)
  110. {
  111. Debug.Log("You must initialize Adaptive Performance before attempting to start.");
  112. return;
  113. }
  114. if (ap.Active)
  115. {
  116. Debug.Log("Adaptive Performance is already running.");
  117. return;
  118. }
  119. ap.StartAdaptivePerformance();
  120. UpdateLastCommandAttemptInfo("Start");
  121. UpdateAdaptivePerformanceStateInfo();
  122. }
  123. public void StopAdaptivePerformance()
  124. {
  125. if (ap == null)
  126. {
  127. Debug.Log("You must start Adaptive Performance before attempting to stop.");
  128. return;
  129. }
  130. if (!ap.Active)
  131. {
  132. Debug.Log("Adaptive Performance is already stopped.");
  133. return;
  134. }
  135. ap.StopAdaptivePerformance();
  136. UpdateLastCommandAttemptInfo("Stop");
  137. UpdateAdaptivePerformanceStateInfo();
  138. }
  139. }