using System.Collections.Generic; using BoatAttack; using UnityEngine; using UnityEngine.Profiling; using UnityEngine.UI; public class PerfomanceStats : MonoBehaviour { private int _runNumber = 1; public int runCount = 4; public int runLength = 2000; // Frame time stats private List _stats; private PerfBasic Stats => _stats[_runNumber - 1]; private List samples = new List(); private int totalSamples = 250; private int curFrame = 0; // UI display public Text frametimeDisplay; private string debugInfo; private void Start() { _stats = new List(); _stats.Add(new PerfBasic(runLength)); CreateTextGUI(); } private void Update () { frametimeDisplay.text = ""; // sample frametime samples.Insert(0, Time.deltaTime); // add sample at the start if(samples.Count >= totalSamples) { samples.RemoveAt(totalSamples - 1); } UpdateFrametime(); var totalMem = Profiler.GetTotalAllocatedMemoryLong(); var gpuMem = Profiler.GetAllocatedMemoryForGraphicsDriver(); DrawText(((float)totalMem / 1000000).ToString("#0.00"), ((float)gpuMem / 1000000).ToString("#0.00")); curFrame++; Stats.RunTime += Time.unscaledDeltaTime; if (curFrame > runLength) ResetRun(); } private void DrawText(string memory, string gpuMemory) { var i = Stats.info; debugInfo = $"Unity:{i.UnityVersion} " + $"URP:{i.UrpVersion} " + $"Build:{i.BoatAttackVersion} " + $"Scene:{i.Scene} " + $"Quality:{i.Quality}\n" + ////////////////////////////////////////////////// $"DeviceInfo:{i.Platform} " + $"{i.API} " + $"{i.Os.Replace(" ", "")}\n" + //////////////////////////// $"CPU:{i.CPU} " + $"GPU:{i.GPU} " + $"Resolution:{i.Resolution}\n" + //////////////////////////////////////////// $"CurrentFrame:{curFrame} " + $"Mem:{memory}mb " + $"GPUMem:{gpuMemory}mb\n" + ///////////////////////////////////////////////// $"AvgFrametime:{Stats.AvgMs:#0.00}ms " + $"MinFrametime:{Stats.MinMs*1000:#0.00}ms(frame {Stats.MinMSFrame}) " + $"MaxFrametime:{Stats.MaxMs*1000:#0.00}ms(frame {Stats.MaxMSFrame})"; frametimeDisplay.text = $"{Application.productName} Benchmark\n{debugInfo}"; } private void ResetRun() { Debug.Log($"Run {_runNumber}: TotalRuntime:{Stats.RunTime:#0.00}s\n{debugInfo}"); Stats.RawSamples = samples.ToArray(); samples.Clear(); curFrame = 0; _stats.Add(new PerfBasic(runLength)); if (_runNumber < runCount) { _runNumber++; } else { Benchmark.EndBenchmark(); } } private void UpdateFrametime() { Stats.AvgMs = 0f; var sampleDivision = 1f / samples.Count; foreach (var t in samples) { Stats.AvgMs += t * sampleDivision; } if (Stats.MinMs > samples[0]) { Stats.MinMs = samples[0]; Stats.MinMSFrame = curFrame; } if (curFrame > 20 && Stats.MaxMs < samples[0]) { Stats.MaxMs = samples[0]; Stats.MaxMSFrame = curFrame; } Stats.AvgMs *= 1000; } private void CreateTextGUI() { var textGo = new GameObject("perfText", new []{typeof(Text)}); textGo.transform.SetParent(AppSettings.ConsoleCanvas.transform, true); frametimeDisplay = textGo.GetComponent(); var rectTransform = frametimeDisplay.rectTransform; rectTransform.anchorMin = rectTransform.sizeDelta = rectTransform.anchoredPosition = Vector2.zero; rectTransform.anchorMax = Vector2.one; frametimeDisplay.font = Resources.GetBuiltinResource("Arial.ttf"); } }