其他分享
首页 > 其他分享> > Unity显示FPS帧率

Unity显示FPS帧率

作者:互联网

直接上代码:

using UnityEngine;
using UnityEngine.UI;

public class FPSControl : MonoBehaviour
{
    [SerializeField, Tooltip("拖入显示FPS的TEXT文本组件")]
    public Text fpsText;
    [SerializeField, Tooltip("true:显示FPS;false:隐藏FPS")]
    public bool isShowFPS = true;
    [SerializeField, Tooltip("FPS刷新周期T")]
    public float updateTimeT = 0.5f;

    private int framesCount = 0;
    private float accumTime = 0.0f;
    private float leftTime = 0.0f;

    void Update()
    {
        if (fpsText != null && isShowFPS)
        {
            ShowFps();
        }
    }

    private void ShowFps()
    {
        leftTime -= Time.unscaledDeltaTime;     //无缩放的delta time
        accumTime += Time.unscaledDeltaTime;
        ++framesCount;

        if (leftTime <= 0)
        {
            float fps = framesCount / accumTime; //帧数/帧数对应的实际时间
            fpsText.text = string.Format("FPS: {0:f0}", fps);

            accumTime = 0.0f;                     //初始化
            framesCount = 0;
            leftTime = updateTimeT;
        }
    }
}

标签:显示,float,private,framesCount,Unity,FPS,public,accumTime
来源: https://blog.csdn.net/kuilaurence/article/details/120564249