Skip to content

Instantly share code, notes, and snippets.

@freemanlam
Created May 15, 2015 06:30
Show Gist options
  • Save freemanlam/825b0bee64eee8e1a491 to your computer and use it in GitHub Desktop.
Save freemanlam/825b0bee64eee8e1a491 to your computer and use it in GitHub Desktop.
Show FPS in Unity
using UnityEngine;
using System.Collections;
public class ShowFPS : MonoBehaviour
{
const float fpsMeasurePeriod = 0.5f;
private int m_FpsAccumulator = 0;
private float m_FpsNextPeriod = 0;
private int m_CurrentFps;
const string display = "{0} FPS";
void Start()
{
m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
}
void Update()
{
// measure average frames per second
m_FpsAccumulator++;
if (Time.realtimeSinceStartup > m_FpsNextPeriod)
{
m_CurrentFps = (int)(m_FpsAccumulator / fpsMeasurePeriod);
m_FpsAccumulator = 0;
m_FpsNextPeriod += fpsMeasurePeriod;
}
}
void OnGUI()
{
GUILayout.Label(string.Format(display, m_CurrentFps));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment