60 lines
1.2 KiB
C#
60 lines
1.2 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class FrameRateCounter : MonoBehaviour
|
|
{
|
|
public enum DisplayMode
|
|
{
|
|
FPS = 0,
|
|
MS = 1
|
|
}
|
|
|
|
[SerializeField]
|
|
private TextMeshProUGUI display;
|
|
|
|
[SerializeField]
|
|
private DisplayMode displayMode;
|
|
|
|
[SerializeField]
|
|
[Range(0.1f, 2f)]
|
|
private float sampleDuration = 1f;
|
|
|
|
private int frames;
|
|
|
|
private float duration;
|
|
|
|
private float bestDuration = float.MaxValue;
|
|
|
|
private float worstDuration;
|
|
|
|
private void Update()
|
|
{
|
|
float unscaledDeltaTime = Time.unscaledDeltaTime;
|
|
frames++;
|
|
duration += unscaledDeltaTime;
|
|
if (unscaledDeltaTime < bestDuration)
|
|
{
|
|
bestDuration = unscaledDeltaTime;
|
|
}
|
|
if (unscaledDeltaTime > worstDuration)
|
|
{
|
|
worstDuration = unscaledDeltaTime;
|
|
}
|
|
if (duration >= sampleDuration)
|
|
{
|
|
if (displayMode == DisplayMode.FPS)
|
|
{
|
|
display.SetText("FPS\n{0:0}\n{1:0}\n{2:0}", 1f / bestDuration, (float)frames / duration, 1f / worstDuration);
|
|
}
|
|
else
|
|
{
|
|
display.SetText("MS\n{0:1}\n{1:1}\n{2:1}", 1000f * bestDuration, 1000f * duration / (float)frames, 1000f * worstDuration);
|
|
}
|
|
frames = 0;
|
|
duration = 0f;
|
|
bestDuration = float.MaxValue;
|
|
worstDuration = 0f;
|
|
}
|
|
}
|
|
}
|