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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章