You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.1 KiB

7 months ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FrameRateReporter : MonoBehaviour
{
// REFERENCES
public TMPro.TMP_Text uiText;
// INTERNALS
private float[] _samples = new float[20];
private int _sampleIndex = 0;
// LIFECYCLE
void Update()
{
// Get most recent unscaled delta time
this._samples[this._sampleIndex] = Time.unscaledDeltaTime;
// Update index (with wrap around)
this._sampleIndex = (this._sampleIndex + 1) % this._samples.Length;
// Calculate mininum and average frame rate
float maxDelta = float.MinValue;
float avgDelta = 0f;
for (int i = 0; i < this._samples.Length; i++)
{
avgDelta += this._samples[i];
maxDelta = (maxDelta < this._samples[i] ? this._samples[i] : maxDelta);
}
avgDelta /= this._samples.Length;
// Write framerates to UI
this.uiText.text = $"{string.Format("{0:0.0}", 1f / avgDelta)}\r\n{string.Format("{0:0.0}", 1f / maxDelta)}";
}
}