1、在edit->project setting->Quality(质量)设置里把帧数设定关闭之后才能在代码中修改游戏运行的帧数。
2.脚本中设置:
Application.targetFrameRate = 60 (设置为 -1 表示不限定帧率)
3.界面中显示
using UnityEngine;
using System.Collections;
public class ShowFps : MonoBehaviour
{
private float m_FPS = 0;
public float updateInterval = 0.5F;
private float accum = 0; // FPS accumulated over the interval
private int frames = 0; // Frames drawn over the interval
private float timeleft; // Left time for current interval
private GUIStyle titleStyle2 = new GUIStyle();
void Awake()
{
Application.targetFrameRate = -1;
titleStyle2.fontSize = 20;
titleStyle2.normal.textColor = Color.green;
}
// Use this for initialization
void Start()
{
timeleft = updateInterval;
}
// Update is called once per frame
void Update()
{
timeleft -= Time.deltaTime;
accum += Time.timeScale / Time.deltaTime;
++frames;
// Interval ended - update GUI text and start new interval
if (timeleft <= 0.0)
{
// display two fractional digits (f2 format)
float fps = accum / frames;
if (fps < 30)
titleStyle2.normal.textColor = Color.yellow;
else
if (fps < 10)
titleStyle2.normal.textColor = Color.red;
else
titleStyle2.normal.textColor = Color.green;
m_FPS = fps;
// DebugConsole.Log(format,level);
timeleft = updateInterval;
accum = 0.0F;
frames = 0;
}
}
void OnGUI()
{
string format = System.String.Format("{0:F2} FPS", m_FPS);
GUI.Label(new Rect(Screen.width / 2, 0, 100, 100), format, titleStyle2);
}
}