Brak opisu
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.

IndexerVisualisation.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.AdaptivePerformance;
  4. using System.Collections.Generic;
  5. public class IndexerVisualisation : MonoBehaviour
  6. {
  7. public Text Timer;
  8. public Text Bottleneck;
  9. public Text PerformanceAction;
  10. public Text ThermalAction;
  11. public Text GpuFrameTime;
  12. public Text CpuFrameTime;
  13. public Transform Content;
  14. public ScalerVisualisation ScalerVisualisationPrefab;
  15. public Toggle ShowDisabledScaler;
  16. List<ScalerVisualisation> m_ScalerVisualisations;
  17. List<AdaptivePerformanceScaler> m_AppliedScalers;
  18. List<AdaptivePerformanceScaler> m_UnappliedScalers;
  19. List<AdaptivePerformanceScaler> m_DisabledScalers;
  20. AdaptivePerformanceIndexer m_Indexer;
  21. private void Start()
  22. {
  23. var ap = Holder.Instance;
  24. if (ap == null || !ap.Active)
  25. {
  26. Debug.Log("[AP Indexer Visualisation] Adaptive Performance not active");
  27. enabled = false;
  28. return;
  29. }
  30. m_Indexer = Holder.Instance.Indexer;
  31. if (m_Indexer == null)
  32. {
  33. Debug.Log("[AP Indexer Visualisation] No indexer available");
  34. enabled = false;
  35. return;
  36. }
  37. m_ScalerVisualisations = new List<ScalerVisualisation>();
  38. m_AppliedScalers = new List<AdaptivePerformanceScaler>();
  39. m_UnappliedScalers = new List<AdaptivePerformanceScaler>();
  40. m_DisabledScalers = new List<AdaptivePerformanceScaler>();
  41. }
  42. private void Update()
  43. {
  44. if (!enabled)
  45. return;
  46. Apply();
  47. }
  48. private void Apply()
  49. {
  50. Debug.Assert(Timer);
  51. Debug.Assert(Bottleneck);
  52. Debug.Assert(PerformanceAction);
  53. Debug.Assert(ThermalAction);
  54. Debug.Assert(GpuFrameTime);
  55. Debug.Assert(CpuFrameTime);
  56. Debug.Assert(Content);
  57. Debug.Assert(ScalerVisualisationPrefab);
  58. Timer.text = $" Timer: <b>{Mathf.RoundToInt(m_Indexer.TimeUntilNextAction)}</b>";
  59. Bottleneck.text =
  60. $" Bottleneck: <b>{Holder.Instance.PerformanceStatus.PerformanceMetrics.PerformanceBottleneck}</b>";
  61. var perfAction = m_Indexer.PerformanceAction;
  62. if (perfAction != StateAction.Decrease && perfAction != StateAction.FastDecrease)
  63. PerformanceAction.text = $" Perf action: <color=lime>{perfAction}</color>";
  64. else
  65. PerformanceAction.text = $" Perf action: <color=red>{perfAction}</color>";
  66. var tempAction = m_Indexer.ThermalAction;
  67. if (tempAction != StateAction.Decrease && tempAction != StateAction.FastDecrease)
  68. ThermalAction.text = $" Thermal action: <color=lime>{tempAction}</color>";
  69. else
  70. ThermalAction.text = $" Thermal action: <color=red>{tempAction}</color>";
  71. GpuFrameTime.text =
  72. $" GPU frame time: {Holder.Instance.PerformanceStatus.FrameTiming.AverageGpuFrameTime:0.####}s";
  73. CpuFrameTime.text =
  74. $" CPU frame time: {Holder.Instance.PerformanceStatus.FrameTiming.AverageCpuFrameTime:0.####}s";
  75. m_Indexer.GetAppliedScalers(ref m_AppliedScalers);
  76. foreach (var scaler in m_AppliedScalers)
  77. CreateScalerVisualisation(scaler);
  78. m_Indexer.GetUnappliedScalers(ref m_UnappliedScalers);
  79. foreach (var scaler in m_UnappliedScalers)
  80. CreateScalerVisualisation(scaler);
  81. m_Indexer.GetDisabledScalers(ref m_DisabledScalers);
  82. foreach (var scaler in m_DisabledScalers)
  83. CreateScalerVisualisation(scaler);
  84. }
  85. private void CreateScalerVisualisation(AdaptivePerformanceScaler scaler)
  86. {
  87. var scalerVisualisation = ContainsScaler(scaler);
  88. if (scalerVisualisation == null)
  89. {
  90. scalerVisualisation = Instantiate(ScalerVisualisationPrefab, Content);
  91. scalerVisualisation.Scaler = scaler;
  92. m_ScalerVisualisations.Add(scalerVisualisation);
  93. }
  94. if (scaler.Enabled || ShowDisabledScaler.isOn)
  95. scalerVisualisation.gameObject.SetActive(true);
  96. else
  97. scalerVisualisation.gameObject.SetActive(false);
  98. }
  99. private ScalerVisualisation ContainsScaler(AdaptivePerformanceScaler scaler)
  100. {
  101. foreach (var current in m_ScalerVisualisations)
  102. {
  103. if (current.Scaler == scaler)
  104. return current;
  105. }
  106. return null;
  107. }
  108. }