Skip to content

Instantly share code, notes, and snippets.

@grimmdev
Created August 24, 2015 08:13
Show Gist options
  • Save grimmdev/522b25d0f7e29d933be3 to your computer and use it in GitHub Desktop.
Save grimmdev/522b25d0f7e29d933be3 to your computer and use it in GitHub Desktop.
Crack at my own FPS Counter
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FPSCounter : MonoBehaviour
{
[SerializeField]
public Text m_label;
private Queue<float> m_queue = new Queue<float>(30);
private float m_timer = 1f;
[SerializeField]
private float m_refreshPeriod = 1f;
[SerializeField]
private float m_rollingWindowSize = 30f;
private void Update()
{
this.m_queue.Enqueue(Time.deltaTime);
if ((float)this.m_queue.Count > this.m_rollingWindowSize)
{
this.m_queue.Dequeue();
}
this.m_timer += Time.deltaTime;
if (this.m_timer >= this.m_refreshPeriod)
{
this.m_timer = 0f;
this.m_label.text = string.Format("{0:f0} fps", this.GetFps());
this.m_label.color = Color.white;
}
}
private float GetFps()
{
float num = 0f;
foreach (float num2 in this.m_queue)
{
num += num2;
}
return (float)this.m_queue.Count / num;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment