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