Created
August 24, 2015 08:13
-
-
Save grimmdev/522b25d0f7e29d933be3 to your computer and use it in GitHub Desktop.
Crack at my own FPS Counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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