在Unity中显示帧率
1.将脚本挂在于一个物体,
2.为FPS赋值一个UnityEngine.UI.Text text运行即可
FrameUpdata是帧数的在UI上的更新速度。
using UnityEngine;
using UnityEngine.UI;
/// <summary>Simple FPS-Counter.</summary>
[DisallowMultipleComponent]
public class FPSDisplay : MonoBehaviour
{
#region Variables
/// <summary>Text component to display the FPS.</summary>
[Tooltip("Text component to display the FPS.")] public Text FPS;
/// <summary>Update every set frame (default: 5).</summary>
[Tooltip("Update every set frame (default: 5)."), Range(1, 300)] public int FrameUpdate = 5;
private float deltaTime;
private float elapsedTime;
private float msec;
private float fps;
private const string wait = "<i>...calculating <b>FPS</b>...</i>";
private const string red = "<color=#E57373><b>FPS: {0:0.}</b> ({1:0.0} ms)</color>";
private const string orange = "<color=#FFB74D><b>FPS: {0:0.}</b> ({1:0.0} ms)</color>";
private const string green = "<color=#81C784><b>FPS: {0:0.}</b> ({1:0.0} ms)</color>";
#endregion
#region MonoBehaviour methods
private void Update()
{
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
elapsedTime += Time.deltaTime;
if (elapsedTime > 1f)
{
if (Time.frameCount % FrameUpdate == 0)
{
FPS.enabled = true;
msec = deltaTime * 1000f;
fps = 1f / deltaTime;
if (fps < 15f)
{
FPS.text = string.Format(red, fps, msec);
}
else if (fps < 29f)
{
FPS.text = string.Format(orange, fps, msec);
}
else
{
FPS.text = string.Format(green, fps, msec);
}
}
}
else
{
FPS.text = wait;
}
}
#endregion
}
标签:片段,fps,代码,deltaTime,private,Unity,FPS,string
From: https://www.cnblogs.com/goodcooking/p/17768168.html