鸟语天空
显示FPS
post by:追风剑情 2019-4-3 17:41

示例

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

[RequireComponent(typeof(Text))]
public class FPS : MonoBehaviour
{
    private Text textField;
    private float fps = 60;

    void Awake()
    {
        textField = GetComponent<Text>();
    }

    void LateUpdate()
    {
        // Avoid crash when timeScale is 0.
        if (Time.deltaTime == 0)
        {
            textField.text = "0fps";
            return;
        }

        string text = "";

        float interp = Time.deltaTime / (0.5f + Time.deltaTime);//~0.3
        float currentFPS = 1.0f / Time.deltaTime;
        //利用插值可保证相对fps只有1帧上下浮动时,
        //保持显示值不变,避免数值频繁变化导致显示闪烁。
        fps = Mathf.Lerp(fps, currentFPS, interp);
        text += Mathf.RoundToInt(fps) + "fps";
        textField.text = text;
    }
}

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容