using UnityEngine; using UnityEngine.UI; using UnityEngine.AdaptivePerformance; using System.Collections.Generic; public class IndexerVisualisation : MonoBehaviour { public Text Timer; public Text Bottleneck; public Text PerformanceAction; public Text ThermalAction; public Text GpuFrameTime; public Text CpuFrameTime; public Transform Content; public ScalerVisualisation ScalerVisualisationPrefab; public Toggle ShowDisabledScaler; List m_ScalerVisualisations; List m_AppliedScalers; List m_UnappliedScalers; List m_DisabledScalers; AdaptivePerformanceIndexer m_Indexer; private void Start() { var ap = Holder.Instance; if (ap == null || !ap.Active) { Debug.Log("[AP Indexer Visualisation] Adaptive Performance not active"); enabled = false; return; } m_Indexer = Holder.Instance.Indexer; if (m_Indexer == null) { Debug.Log("[AP Indexer Visualisation] No indexer available"); enabled = false; return; } m_ScalerVisualisations = new List(); m_AppliedScalers = new List(); m_UnappliedScalers = new List(); m_DisabledScalers = new List(); } private void Update() { if (!enabled) return; Apply(); } private void Apply() { Debug.Assert(Timer); Debug.Assert(Bottleneck); Debug.Assert(PerformanceAction); Debug.Assert(ThermalAction); Debug.Assert(GpuFrameTime); Debug.Assert(CpuFrameTime); Debug.Assert(Content); Debug.Assert(ScalerVisualisationPrefab); Timer.text = $" Timer: {Mathf.RoundToInt(m_Indexer.TimeUntilNextAction)}"; Bottleneck.text = $" Bottleneck: {Holder.Instance.PerformanceStatus.PerformanceMetrics.PerformanceBottleneck}"; var perfAction = m_Indexer.PerformanceAction; if (perfAction != StateAction.Decrease && perfAction != StateAction.FastDecrease) PerformanceAction.text = $" Perf action: {perfAction}"; else PerformanceAction.text = $" Perf action: {perfAction}"; var tempAction = m_Indexer.ThermalAction; if (tempAction != StateAction.Decrease && tempAction != StateAction.FastDecrease) ThermalAction.text = $" Thermal action: {tempAction}"; else ThermalAction.text = $" Thermal action: {tempAction}"; GpuFrameTime.text = $" GPU frame time: {Holder.Instance.PerformanceStatus.FrameTiming.AverageGpuFrameTime:0.####}s"; CpuFrameTime.text = $" CPU frame time: {Holder.Instance.PerformanceStatus.FrameTiming.AverageCpuFrameTime:0.####}s"; m_Indexer.GetAppliedScalers(ref m_AppliedScalers); foreach (var scaler in m_AppliedScalers) CreateScalerVisualisation(scaler); m_Indexer.GetUnappliedScalers(ref m_UnappliedScalers); foreach (var scaler in m_UnappliedScalers) CreateScalerVisualisation(scaler); m_Indexer.GetDisabledScalers(ref m_DisabledScalers); foreach (var scaler in m_DisabledScalers) CreateScalerVisualisation(scaler); } private void CreateScalerVisualisation(AdaptivePerformanceScaler scaler) { var scalerVisualisation = ContainsScaler(scaler); if (scalerVisualisation == null) { scalerVisualisation = Instantiate(ScalerVisualisationPrefab, Content); scalerVisualisation.Scaler = scaler; m_ScalerVisualisations.Add(scalerVisualisation); } if (scaler.Enabled || ShowDisabledScaler.isOn) scalerVisualisation.gameObject.SetActive(true); else scalerVisualisation.gameObject.SetActive(false); } private ScalerVisualisation ContainsScaler(AdaptivePerformanceScaler scaler) { foreach (var current in m_ScalerVisualisations) { if (current.Scaler == scaler) return current; } return null; } }