| | 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 | |
|
| 0 | 15 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| | 16 | | private int lastPlayerCount = 0; |
| | 17 | |
|
| | 18 | | private void OnEnable() |
| | 19 | | { |
| 0 | 20 | | if (label == null || performanceData == null) |
| 0 | 21 | | return; |
| | 22 | |
|
| 0 | 23 | | lastPlayerCount = otherPlayers.Count(); |
| 0 | 24 | | otherPlayers.OnAdded += OnOtherPlayersModified; |
| 0 | 25 | | otherPlayers.OnRemoved += OnOtherPlayersModified; |
| | 26 | |
|
| 0 | 27 | | StartCoroutine(UpdateLabelLoop()); |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | private void OnOtherPlayersModified(string playerName, Player player) |
| | 31 | | { |
| 0 | 32 | | lastPlayerCount = otherPlayers.Count(); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private void OnDisable() |
| | 36 | | { |
| 0 | 37 | | otherPlayers.OnAdded -= OnOtherPlayersModified; |
| 0 | 38 | | otherPlayers.OnRemoved -= OnOtherPlayersModified; |
| 0 | 39 | | StopAllCoroutines(); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | private IEnumerator UpdateLabelLoop() |
| | 43 | | { |
| 0 | 44 | | while (true) |
| | 45 | | { |
| 0 | 46 | | UpdateLabel(); |
| | 47 | |
|
| 0 | 48 | | yield return new WaitForSeconds(REFRESH_SECONDS); |
| | 49 | | } |
| | 50 | | } |
| | 51 | |
|
| | 52 | | private void UpdateLabel() |
| | 53 | | { |
| 0 | 54 | | float dt = Time.unscaledDeltaTime; |
| | 55 | |
|
| 0 | 56 | | float fps = performanceData.Get().fpsCount; |
| | 57 | |
|
| 0 | 58 | | string fpsFormatted = fps.ToString("##"); |
| 0 | 59 | | string msFormatted = (dt * 1000).ToString("##"); |
| | 60 | |
|
| 0 | 61 | | string targetText = string.Empty; |
| | 62 | |
|
| 0 | 63 | | string NO_DECIMALS = "##"; |
| 0 | 64 | | string TWO_DECIMALS = "##.00"; |
| 0 | 65 | | targetText += $"Nearby players: {lastPlayerCount}\n"; |
| 0 | 66 | | targetText += $"Hiccups in the last 1000 frames: {performanceData.Get().hiccupCount}\n"; |
| 0 | 67 | | targetText += $"Hiccup loss: {(100.0f * performanceData.Get().hiccupSum / performanceData.Get().totalSeconds |
| 0 | 68 | | targetText += $"Bad Frames Percentile: {((performanceData.Get().hiccupCount) / 10.0f).ToString(NO_DECIMALS)} |
| 0 | 69 | | targetText += $"Current {msFormatted} ms (fps: {fpsFormatted})"; |
| | 70 | |
|
| 0 | 71 | | if (label.text != targetText) |
| | 72 | | { |
| 0 | 73 | | label.text = targetText; |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | FPSColoring.DisplayColor(label, fps); |
| 0 | 77 | | } |
| | 78 | | } |
| | 79 | | } |