Unity显示FPS
俩种情况
1.开发测试的时候
可以在Game视图中点击Stats查看
2.发布后看FPS
新建一个脚本FPSDisplay.cs,拖到相机上
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 = Color.white;
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);
}
}
// 都可以
using UnityEngine;
using System.Collections;
public class ShowFps : MonoBehaviour {
public float updateInterval = 0.5F;
private double lastInterval;
private int frames = 0;
private float fps;
void Start()
{
lastInterval = Time.realtimeSinceStartup;
frames = 0;
}
void OnGUI()
{
GUILayout.Label(" " + fps.ToString("f2"));
}
void Update()
{
++frames;
float timeNow = Time.realtimeSinceStartup;
if (timeNow > lastInterval + updateInterval)
{
fps = (float)(frames / (timeNow - lastInterval));
frames = 0;
lastInterval = timeNow;
}
}
}