using UnityEngine;
using System.Collections;
public class FPSManager : MonoBehaviour
{
public float updateInterval = 0.5F;
private double lastInterval;
private int frames = 0;
private float fps;
void Awake()
{
// Application.runInBackground = true;
// Screen.sleepTimeout = SleepTimeout.NeverSleep;
// Application.targetFrameRate = 30;
}
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;
}
}
}
如果想要自己控制帧率,需要打开Edit->Project Setting->Quality->Sync Count,设置为Don't Sync。即非垂直同步。关于垂直同步,参考我上一篇文章。
在Awake设置对应的帧率,targetFrameRate