显示FPS

Unity的日常...

设置帧率

首先把Edit->ProjectSetting->Quality面板中的
V Sync Count(垂直同步)设置为Don'y Sync
其次,在某一个组件的Awake函数中添加以下代码

void Awake() {
    Application.targetFrameRate = 60;
}

显示帧率

using UnityEngine;
using System.Collections;

public class ShowFPS : MonoBehaviour {

    public bool isShowFPS = true;
    private float updateInterval = 1f; //帧数刷新间隔
    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);
    }
}


你可能感兴趣的:(显示FPS)