顯示FPS

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);
    }
}


最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容