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

AutoPerfUI.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using UnityEngine;
  2. using UnityEngine.AdaptivePerformance;
  3. using UnityEngine.UI;
  4. public class AutoPerfUI : MonoBehaviour
  5. {
  6. private IAdaptivePerformance ap;
  7. public PerformanceBottleneck targetBN = PerformanceBottleneck.TargetFrameRate;
  8. public Text avgCPUTime, avgGPUTime, avgFrameTime, cpuLevel, gpuLevel, tempLevel, tempTrend, stateText;
  9. public Image tempBackground;
  10. float avgCPUfloat, avgGPUfloat, avgFramefloat;
  11. public GameObject GPUBound, CPUBound, TargetFrameRateBound, UnknownBound;
  12. public GameObject NoWarning, ThrottlingImminent, Throttling;
  13. void Start()
  14. {
  15. ap = Holder.Instance;
  16. if (ap == null || !ap.Active)
  17. {
  18. Debug.Log("[AP APC] Adaptive Performance not active");
  19. enabled = false;
  20. return;
  21. }
  22. // The game starts in a menu and we want to save power
  23. ap.DevicePerformanceControl.CpuLevel = 3;
  24. ap.DevicePerformanceControl.GpuLevel = 3;
  25. ap.PerformanceStatus.PerformanceBottleneckChangeEvent += OnBottleneckChange;
  26. ap.ThermalStatus.ThermalEvent += OnThermalEvent;
  27. checkStatus();
  28. maxCpuPerformanceLevel = ap.DevicePerformanceControl.MaxCpuPerformanceLevel;
  29. maxGpuPerformanceLevel = ap.DevicePerformanceControl.MaxGpuPerformanceLevel;
  30. }
  31. private void OnDestroy()
  32. {
  33. if (ap == null)
  34. {
  35. return;
  36. }
  37. ap.PerformanceStatus.PerformanceBottleneckChangeEvent -= OnBottleneckChange;
  38. ap.ThermalStatus.ThermalEvent -= OnThermalEvent;
  39. }
  40. void checkStatus()
  41. {
  42. DisableAllBottlenecks();
  43. DisableAllThermalWarnings();
  44. switch (ap.PerformanceStatus.PerformanceMetrics.PerformanceBottleneck)
  45. {
  46. case PerformanceBottleneck.CPU:
  47. Activate(CPUBound);
  48. break;
  49. case PerformanceBottleneck.GPU:
  50. Activate(GPUBound);
  51. break;
  52. case PerformanceBottleneck.TargetFrameRate:
  53. Activate(TargetFrameRateBound);
  54. break;
  55. case PerformanceBottleneck.Unknown:
  56. Activate(UnknownBound);
  57. break;
  58. }
  59. switch (ap.ThermalStatus.ThermalMetrics.WarningLevel)
  60. {
  61. case WarningLevel.NoWarning:
  62. Activate(NoWarning);
  63. break;
  64. case WarningLevel.ThrottlingImminent:
  65. Activate(ThrottlingImminent);
  66. break;
  67. case WarningLevel.Throttling:
  68. Activate(Throttling);
  69. break;
  70. }
  71. }
  72. float tempLevelLast;
  73. float tempLevelFloat;
  74. int maxCpuPerformanceLevel;
  75. int maxGpuPerformanceLevel;
  76. void Update()
  77. {
  78. avgCPUfloat = ap.PerformanceStatus.FrameTiming.AverageCpuFrameTime;
  79. avgGPUfloat = ap.PerformanceStatus.FrameTiming.AverageGpuFrameTime;
  80. avgFramefloat = ap.PerformanceStatus.FrameTiming.AverageFrameTime;
  81. cpuLevel.text = ap.PerformanceStatus.PerformanceMetrics.CurrentCpuLevel + "/" + maxCpuPerformanceLevel;
  82. gpuLevel.text = ap.PerformanceStatus.PerformanceMetrics.CurrentGpuLevel + "/" + maxGpuPerformanceLevel;
  83. tempLevelFloat = ap.ThermalStatus.ThermalMetrics.TemperatureLevel;
  84. if (tempLevelFloat != tempLevelLast)
  85. CheckTempColour(tempLevelFloat);
  86. tempLevel.text = tempLevelFloat.ToString("F2");
  87. tempTrend.text = ap.ThermalStatus.ThermalMetrics.TemperatureTrend.ToString("F2");
  88. avgCPUTime.text = (avgCPUfloat * 1000).ToString("F2") + " ms";
  89. avgGPUTime.text = (avgGPUfloat * 1000).ToString("F2") + " ms";
  90. avgFrameTime.text = (avgFramefloat * 1000).ToString("F2") + " ms";
  91. tempLevelLast = ap.ThermalStatus.ThermalMetrics.TemperatureLevel;
  92. }
  93. void CheckTempColour(float tempLevel)
  94. {
  95. if (tempLevel < 0.5f)
  96. tempBackground.color = Color.green;
  97. else if (tempLevel < 0.8f)
  98. tempBackground.color = Color.yellow;
  99. else tempBackground.color = Color.red;
  100. }
  101. void OnBottleneckChange(PerformanceBottleneckChangeEventArgs ev)
  102. {
  103. DisableAllBottlenecks();
  104. switch (ev.PerformanceBottleneck)
  105. {
  106. case PerformanceBottleneck.CPU:
  107. Activate(CPUBound);
  108. break;
  109. case PerformanceBottleneck.GPU:
  110. Activate(GPUBound);
  111. break;
  112. case PerformanceBottleneck.TargetFrameRate:
  113. Activate(TargetFrameRateBound);
  114. break;
  115. case PerformanceBottleneck.Unknown:
  116. Activate(UnknownBound);
  117. break;
  118. }
  119. }
  120. void OnThermalEvent(ThermalMetrics ev)
  121. {
  122. DisableAllThermalWarnings();
  123. switch (ev.WarningLevel)
  124. {
  125. case WarningLevel.NoWarning:
  126. Activate(NoWarning);
  127. break;
  128. case WarningLevel.ThrottlingImminent:
  129. Activate(ThrottlingImminent);
  130. break;
  131. case WarningLevel.Throttling:
  132. Activate(Throttling);
  133. break;
  134. }
  135. }
  136. void DisableAllBottlenecks()
  137. {
  138. CPUBound.SetActive(false);
  139. GPUBound.SetActive(false);
  140. TargetFrameRateBound.SetActive(false);
  141. UnknownBound.SetActive(false);
  142. }
  143. void DisableAllThermalWarnings()
  144. {
  145. NoWarning.SetActive(false);
  146. ThrottlingImminent.SetActive(false);
  147. Throttling.SetActive(false);
  148. }
  149. void Activate(GameObject go)
  150. {
  151. go.SetActive(true);
  152. }
  153. }