namespace NRKernal
{
using UnityEngine;
/// The FPS counter.
public class FPSCounter : MonoBehaviour
{
/// The frame range.
public int frameRange = 60;
/// Gets or sets the average FPS.
/// The average FPS.
public int AverageFPS { get; private set; }
/// Gets or sets the highest FPS.
/// The highest FPS.
public int HighestFPS { get; private set; }
/// Gets or sets the lowest FPS.
/// The lowest FPS.
public int LowestFPS { get; private set; }
/// Buffer for FPS data.
int[] fpsBuffer;
/// Zero-based index of the FPS buffer.
int fpsBufferIndex;
/// Updates this object.
void Update()
{
if (fpsBuffer == null || fpsBuffer.Length != frameRange)
{
InitializeBuffer();
}
UpdateBuffer();
CalculateFPS();
}
/// Initializes the buffer.
void InitializeBuffer()
{
if (frameRange <= 0)
{
frameRange = 1;
}
fpsBuffer = new int[frameRange];
fpsBufferIndex = 0;
}
/// Updates the buffer.
void UpdateBuffer()
{
fpsBuffer[fpsBufferIndex++] = (int)(1f / Time.unscaledDeltaTime);
if (fpsBufferIndex >= frameRange)
{
fpsBufferIndex = 0;
}
}
/// Calculates the FPS.
void CalculateFPS()
{
int sum = 0;
int highest = 0;
int lowest = int.MaxValue;
for (int i = 0; i < frameRange; i++)
{
int fps = fpsBuffer[i];
sum += fps;
if (fps > highest)
{
highest = fps;
}
if (fps < lowest)
{
lowest = fps;
}
}
AverageFPS = (int)((float)sum / frameRange);
HighestFPS = highest;
LowestFPS = lowest;
}
}
}