ref: http://wiki.unity3d.com/index.php?title=FramesPerSecond
using UnityEngine;
using System.Collections;
public class FPSDisplay : MonoBehaviour
{
float deltaTime = 0.0f;
void Update()
{
deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f;
}
void OnGUI()
{
int w = Screen.width, h = Screen.height;
GUIStyle style = new GUIStyle();
Rect rect = new Rect(0, 0, w, h * 2 / 100);
style.alignment = TextAnchor.UpperLeft;
style.fontSize = h * 2 / 100;
style.normal.textColor = new Color (0.0f, 0.0f, 0.5f, 1.0f);
float msec = deltaTime * 1000.0f;
float fps = 1.0f / deltaTime;
string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
GUI.Label(rect, text, style);
}
}
int frame;
float frequency = 1.0f;
private IEnumerator FPS()
{
int lastFrameCount = 0;
float lastTime = 0.0f;
WaitForSeconds waitFrequency = new WaitForSeconds(frequency);
float deltaTime = 0.0f;
int frameCount = 0;
while (true)
{
lastFrameCount = Time.frameCount;
lastTime = Time.realtimeSinceStartup;
yield return waitFrequency;
deltaTime = Time.realtimeSinceStartup - lastTime;
frameCount = Time.frameCount - lastFrameCount;
frame = (int)(frameCount / deltaTime);
}
}
private void OnGUI()
{
GUILayout.BeginVertical();
GUILayout.Label(string.Format("<color=red><size=50> FRAME: {0}</size></color>", frame));
GUILayout.EndVertical();
}