Skip to content

Commit

Permalink
Average out frame times for FPS display
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNathannator committed Jan 22, 2024
1 parent fc73ce6 commit 614dbb5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Assets/Script/Menu/Persistent/StatsManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Profiling;
Expand Down Expand Up @@ -42,6 +44,8 @@ public enum Stat

private int _screenRefreshRate;

private List<float> _frameTimes = new();

private float _nextUpdateTime;

protected override void SingletonAwake()
Expand Down Expand Up @@ -77,6 +81,8 @@ public bool IsShowing(Stat stat)

private void Update()
{
_frameTimes.Add(Time.unscaledDeltaTime);

// Wait for next update period
if (Time.unscaledTime < _nextUpdateTime) return;

Expand All @@ -92,7 +98,9 @@ private void UpdateFpsCounter()
if (!IsShowing(Stat.FPS)) return;

// Get FPS
int fps = (int) (1f / Time.unscaledDeltaTime);
// Averaged to smooth out brief lag frames
float fps = 1f / _frameTimes.Average();
_frameTimes.Clear();

// Color the circle sprite based on the FPS
if (fps < _screenRefreshRate / 2)
Expand All @@ -109,7 +117,7 @@ private void UpdateFpsCounter()
}

// Display the FPS
_fpsText.text = $"<b>FPS:</b> {fps}";
_fpsText.text = $"<b>FPS:</b> {fps:N1}";
}

private void UpdateMemoryStats()
Expand Down

0 comments on commit 614dbb5

Please sign in to comment.