代碼如下:
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