Unity之如何计算实时帧率

代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CalcFPSTool : MonoBehaviour
{
    private float m_UpdateInterval, m_FPS;

    float UpdateInterval
    {
        set => m_UpdateInterval = Mathf.Clamp(value, 0.1f, 10f);
    }

// Start is called before the first frame update
    void Start()
    {
        UpdateInterval = 1;
        StartCoroutine(UpdateCounter());
    }

    IEnumerator UpdateCounter()
    {
        while (true)
        {
            var previousUpdateTime = Time.unscaledTime;
            var previousUpdateFrames = Time.frameCount;

            while (Time.unscaledTime < previousUpdateTime + m_UpdateInterval)
            {
                yield return null;
            }

            var timeElapsed = Time.unscaledTime - previousUpdateTime;
            var framesChanged = Time.frameCount - previousUpdateFrames;

            m_FPS = framesChanged / timeElapsed;
        }
    }

    private void OnGUI()
    {
        GUILayout.Label($"fps:{Mathf.FloorToInt(m_FPS)}");
    }
}

 

转载请注明出处:https://www.cnblogs.com/jietian331/p/17636916.html

posted @ 2023-08-17 10:18  孤独の巡礼  阅读(298)  评论(0编辑  收藏  举报