Unity创建FPS监视

效果

本文提供了两种FPS呈现方式
左上角的白色GUI展示
和我们的TextMeshPro展示
因为本程序使用的版本为2021版本所以用的是TextMeshPro
下面的脚本会使用2021版本的TextMeshpro版本和其他UGUI的Text版本两种版本
在这里插入图片描述

红色部分FPS呈现

第一种脚本TextMeshPro

创建一个FPS脚本制作以下脚本

using TMPro;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 打印FPS
/// </summary>
public class FPS : MonoBehaviour
{
    
    
    float _updateInterval = 1f;//设定更新帧率的时间间隔为1秒  
    float _accum = .0f;//累积时间  
    int _frames = 0;//在_updateInterval时间内运行了多少帧  
    float _timeLeft;
    string fpsFormat;
    public TextMeshProUGUI text;

    void Start()
    {
    
    
        _timeLeft = _updateInterval;
    }
    void Update()
    {
    
    
        _timeLeft -= Time.deltaTime;
        //Time.timeScale可以控制Update 和LateUpdate 的执行速度,  
        //Time.deltaTime是以秒计算,完成最后一帧的时间  
        //相除即可得到相应的一帧所用的时间  
        _accum += Time.timeScale / Time.deltaTime;
        ++_frames;//帧数  

        if (_timeLeft <= 0)
        {
    
    
            float fps = _accum / _frames;
            //Debug.Log(_accum + "__" + _frames);  
            fpsFormat = System.String.Format("{0:F2}FPS", fps);//保留两位小数  
                                                               // Debug.LogError(fpsFormat);

            _timeLeft = _updateInterval;
            _accum = .0f;
            _frames = 0;
        }
        text.text = fpsFormat;
    }
}

然后Unity内主动赋值需要展示的Text就可以
在这里插入图片描述

第二种Text

下面为2021版本以下的text版本

using TMPro;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 打印FPS
/// </summary>
public class FPS : MonoBehaviour
{
    
    
    float _updateInterval = 1f;//设定更新帧率的时间间隔为1秒  
    float _accum = .0f;//累积时间  
    int _frames = 0;//在_updateInterval时间内运行了多少帧  
    float _timeLeft;
    string fpsFormat;
    public Text text;

    void Start()
    {
    
    
        _timeLeft = _updateInterval;
    }
    void Update()
    {
    
    
        _timeLeft -= Time.deltaTime;
        //Time.timeScale可以控制Update 和LateUpdate 的执行速度,  
        //Time.deltaTime是以秒计算,完成最后一帧的时间  
        //相除即可得到相应的一帧所用的时间  
        _accum += Time.timeScale / Time.deltaTime;
        ++_frames;//帧数  

        if (_timeLeft <= 0)
        {
    
    
            float fps = _accum / _frames;
            //Debug.Log(_accum + "__" + _frames);  
            fpsFormat = System.String.Format("{0:F2}FPS", fps);//保留两位小数  
                                                               // Debug.LogError(fpsFormat);

            _timeLeft = _updateInterval;
            _accum = .0f;
            _frames = 0;
        }
        text.text = fpsFormat;
    }
}

白色FPS版本

与其他版本的差异为此脚本不用赋值 直接可以使用 差异也只是多了一个OnGUi方法
重点为此脚本不论版本都可以使用

using TMPro;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 打印FPS
/// </summary>
public class FPS : MonoBehaviour
{
    
    
    float _updateInterval = 1f;//设定更新帧率的时间间隔为1秒  
    float _accum = .0f;//累积时间  
    int _frames = 0;//在_updateInterval时间内运行了多少帧  
    float _timeLeft;
    string fpsFormat;

    void Start()
    {
    
    
        _timeLeft = _updateInterval;
    }

    void OnGUI()
    {
    
    
        GUI.Label(new Rect(100, 100, 200, 200), fpsFormat);
    }

    void Update()
    {
    
    
        _timeLeft -= Time.deltaTime;
        //Time.timeScale可以控制Update 和LateUpdate 的执行速度,  
        //Time.deltaTime是以秒计算,完成最后一帧的时间  
        //相除即可得到相应的一帧所用的时间  
        _accum += Time.timeScale / Time.deltaTime;
        ++_frames;//帧数  

        if (_timeLeft <= 0)
        {
    
    
            float fps = _accum / _frames;
            //Debug.Log(_accum + "__" + _frames);  
            fpsFormat = System.String.Format("{0:F2}FPS", fps);//保留两位小数  
                                                               // Debug.LogError(fpsFormat);

            _timeLeft = _updateInterval;
            _accum = .0f;
            _frames = 0;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42746271/article/details/123916748
FPS