第四章游戏脚本
使用OnGUI显示FPS
public float updateInterval = 0.5f;
public int test;
private float accum = 0;
private int frames = 0;
private float timeleft;
private string stringFps;
void Start()
{
timeleft = updateInterval;
}
void Update()
{
timeleft -= Time.deltaTime;
accum += Time.timeScale / Time.deltaTime;
++frames;
if (timeleft <= 0.0f)
{
float fps = accum / frames;
string format = System.String.Format("{0:F2} FPS", fps);
stringFps = format;
timeleft = updateInterval;
accum = 0.0f;
frames = 0;
}
}
void OnGUI()
{
GUIStyle guistyle = GUIStyle.none;
guistyle.fontSize = 30;
guistyle.normal.textColor = Color.red;
guistyle.alignment = TextAnchor.UpperLeft;
Rect rt = new Rect(40, 0, 100, 100);
GUI.Label(rt, stringFps,guistyle);
}
下边的代码强制设置fps最高帧为30帧要使用int 而不能使用float类型的参数
Application.targetFrameRate=30;