-
-
Save tk009999/2fffc948a126ddc36f2f0fc3a7100c66 to your computer and use it in GitHub Desktop.
Show FPS in Unity
This file contains 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 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