Skip to content

Instantly share code, notes, and snippets.

@tk009999
Forked from freemanlam/ShowFPS.cs
Created November 25, 2021 06:20
Show Gist options
  • Save tk009999/2fffc948a126ddc36f2f0fc3a7100c66 to your computer and use it in GitHub Desktop.
Save tk009999/2fffc948a126ddc36f2f0fc3a7100c66 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