Nessuna descrizione
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.

BoostControl.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.AdaptivePerformance;
  6. using UnityEngine.UI;
  7. public class BoostControl : MonoBehaviour
  8. {
  9. IAdaptivePerformance ap;
  10. SampleFactory factory;
  11. public GameObject cpuLoader;
  12. public GameObject gpuLoader;
  13. public float timeOut = 3000;
  14. string state = "";
  15. public Text bottleneckStatus;
  16. bool testRunning = false;
  17. PerformanceBottleneck targetBottleneck = PerformanceBottleneck.CPU;
  18. BottleneckUI bottleneckUI;
  19. System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
  20. void Start()
  21. {
  22. factory = FindObjectOfType<SampleFactory>();
  23. bottleneckUI = FindObjectOfType<BottleneckUI>();
  24. factory.RunTest = false;
  25. ap = Holder.Instance;
  26. if (ap == null || !ap.Active)
  27. {
  28. state = "Adaptive Performance not active";
  29. Debug.Log("[AP Boost] Adaptive Performance not active");
  30. return;
  31. }
  32. else
  33. {
  34. watch.Start();
  35. state = "Waiting on Load";
  36. targetBottleneck = PerformanceBottleneck.TargetFrameRate;
  37. Debug.LogFormat("[AP Boost] Starting Test Timestamp : {0} s , Label : {1} , Objects : {2} \n", watch.ElapsedMilliseconds / 1000f, state, factory.internalObjs);
  38. }
  39. bottleneckStatus.text = state;
  40. ap.PerformanceStatus.PerformanceBoostChangeEvent += OnBoostModeEvent;
  41. }
  42. private void OnDestroy()
  43. {
  44. if (ap == null || !ap.Active)
  45. return;
  46. ap.PerformanceStatus.PerformanceBoostChangeEvent -= OnBoostModeEvent;
  47. }
  48. void OnBoostModeEvent(PerformanceBoostChangeEventArgs ev)
  49. {
  50. if (!ev.CpuBoost || !ev.GpuBoost)
  51. LogResult();
  52. }
  53. public void BoostCPU()
  54. {
  55. if (ap == null || !ap.Active)
  56. return;
  57. ap.DevicePerformanceControl.CpuPerformanceBoost = true;
  58. }
  59. public void BoostGPU()
  60. {
  61. if (ap == null || !ap.Active)
  62. return;
  63. ap.DevicePerformanceControl.GpuPerformanceBoost = true;
  64. }
  65. public void CPULoad()
  66. {
  67. StartTest(PerformanceBottleneck.CPU);
  68. }
  69. public void GPULoad()
  70. {
  71. StartTest(PerformanceBottleneck.GPU);
  72. }
  73. void StartTest(PerformanceBottleneck target)
  74. {
  75. LogResult();
  76. factory.FlushObjects();
  77. switch (target)
  78. {
  79. case PerformanceBottleneck.GPU:
  80. factory.RunTest = true;
  81. factory.prefab = gpuLoader;
  82. factory.spawnAmount = 50f;
  83. factory.LimitCount = 50;
  84. targetBottleneck = PerformanceBottleneck.GPU;
  85. testRunning = true;
  86. break;
  87. case PerformanceBottleneck.TargetFrameRate:
  88. factory.RunTest = false;
  89. state = "";
  90. targetBottleneck = PerformanceBottleneck.TargetFrameRate;
  91. testRunning = false;
  92. break;
  93. case PerformanceBottleneck.CPU:
  94. factory.RunTest = true;
  95. factory.prefab = cpuLoader;
  96. factory.spawnAmount = 1;
  97. factory.LimitCount = 2000;
  98. targetBottleneck = PerformanceBottleneck.CPU;
  99. testRunning = true;
  100. break;
  101. }
  102. state = $"Changed to {target} load";
  103. watch.Reset();
  104. watch.Start();
  105. LogResult();
  106. }
  107. void LogResult()
  108. {
  109. Debug.LogFormat("[AP Boost] Timestamp : {0} s , Label : {1} , Objects : {2} \n", watch.ElapsedMilliseconds / 1000f, state, factory.internalObjs);
  110. }
  111. void Update()
  112. {
  113. bottleneckStatus.text = $"{state}" + (testRunning ? $"{Environment.NewLine} Timeout in {timeOut-watch.ElapsedMilliseconds / 1000f:F2}s" : "");
  114. if (bottleneckUI != null)
  115. bottleneckUI.targetBottleneck = targetBottleneck;
  116. if (ap == null || !ap.Active)
  117. return;
  118. if (!testRunning)
  119. return;
  120. if (watch.ElapsedMilliseconds / 1000f > timeOut)
  121. StartTest(PerformanceBottleneck.TargetFrameRate);
  122. }
  123. }