Unity的日常...
設置幀率
首先把Edit->ProjectSetting->Quality面板中的
V Sync Count(垂直同步)設置為Don'y Sync
其次,在某一個組件的Awake函數(shù)中添加以下代碼
void Awake() {
Application.targetFrameRate = 60;
}
顯示幀率
using UnityEngine;
using System.Collections;
public class ShowFPS : MonoBehaviour {
public bool isShowFPS = true;
private float updateInterval = 1f; //幀數(shù)刷新間隔
private double lastInterval;
private int frames = 0;
private float currFPS;
void Awake() {
Application.targetFrameRate = 60;
}
void Start () {
if(!isShowFPS) {
return;
}
lastInterval = Time.realtimeSinceStartup;
frames = 0;
}
// Update is called once per frame
void Update () {
if(!isShowFPS) {
return;
}
++frames;
float timeNow = Time.realtimeSinceStartup;
if (timeNow > lastInterval + updateInterval)
{
currFPS = (float)(frames / (timeNow - lastInterval));
frames = 0;
lastInterval = timeNow;
}
}
void OnGUI() {
if(!isShowFPS) {
return;
}
string str = "FPS:" + currFPS.ToString ("f2");
var style = new GUIStyle();
style.fontSize=24;
style.normal.textColor = new Color (1, 1, 1);
GUI.Label(new Rect(Screen.width - 130, Screen.height - 30, 100, 100), str, style);
}
}