| | 1 | | using System.Collections; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | namespace DCL.FPSDisplay |
| | 6 | | { |
| | 7 | |
|
| | 8 | | public class FPSDisplay : MonoBehaviour |
| | 9 | | { |
| | 10 | | private const float REFRESH_SECONDS = 0.1f; |
| | 11 | |
|
| | 12 | | [SerializeField] private Text label; |
| | 13 | | [SerializeField] private PerformanceMetricsDataVariable performanceData; |
| | 14 | |
|
| | 15 | | private void OnEnable() |
| | 16 | | { |
| 10 | 17 | | if (label == null || performanceData == null) |
| 0 | 18 | | return; |
| | 19 | |
|
| 10 | 20 | | StartCoroutine(UpdateLabelLoop()); |
| 10 | 21 | | } |
| | 22 | |
|
| 20 | 23 | | private void OnDisable() { StopAllCoroutines(); } |
| | 24 | |
|
| | 25 | | private IEnumerator UpdateLabelLoop() |
| | 26 | | { |
| 1 | 27 | | while (true) |
| | 28 | | { |
| 11 | 29 | | UpdateLabel(); |
| | 30 | |
|
| 11 | 31 | | yield return new WaitForSeconds(REFRESH_SECONDS); |
| | 32 | | } |
| | 33 | | } |
| | 34 | |
|
| | 35 | | private void UpdateLabel() |
| | 36 | | { |
| 11 | 37 | | float dt = Time.unscaledDeltaTime; |
| | 38 | |
|
| 11 | 39 | | float fps = performanceData.Get().fpsCount; |
| | 40 | |
|
| 11 | 41 | | string fpsFormatted = fps.ToString("##"); |
| 11 | 42 | | string msFormatted = (dt * 1000).ToString("##"); |
| | 43 | |
|
| 11 | 44 | | string targetText = string.Empty; |
| | 45 | |
|
| | 46 | |
|
| 11 | 47 | | string NO_DECIMALS = "##"; |
| 11 | 48 | | string TWO_DECIMALS = "##.00"; |
| 11 | 49 | | targetText += $"Hiccups in the last 1000 frames: {performanceData.Get().hiccupCount}\n"; |
| 11 | 50 | | targetText += $"Hiccup loss: {(100.0f * performanceData.Get().hiccupSum / performanceData.Get().totalSeconds |
| 11 | 51 | | targetText += $"Bad Frames Percentile: {((performanceData.Get().hiccupCount) / 10.0f).ToString(NO_DECIMALS)} |
| 11 | 52 | | targetText += $"Current {msFormatted} ms (fps: {fpsFormatted})"; |
| | 53 | |
|
| 11 | 54 | | if (label.text != targetText) |
| | 55 | | { |
| 11 | 56 | | label.text = targetText; |
| | 57 | | } |
| | 58 | |
|
| 11 | 59 | | FPSColoring.DisplayColor(label, fps); |
| 11 | 60 | | } |
| | 61 | | } |
| | 62 | | } |