显示FPS

作者:追风剑情 发布于:2019-4-3 17:41 分类:Unity3d

示例

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

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号