显示Unity实时帧率的脚本
时间: 2024-05-15 09:14:45 浏览: 132
您好!关于显示Unity实时帧率的脚本,我可以给您提供以下示例代码:
```
using UnityEngine;
public class FPSCounter : MonoBehaviour
{
[SerializeField] private float m_updateInterval = 0.5f;
private float m_accum = .0f;
private float m_frames = .0f;
private float m_timeleft;
private float m_fps;
private void Start()
{
m_timeleft = m_updateInterval;
}
private void Update()
{
m_timeleft -= Time.deltaTime;
m_accum += Time.timeScale / Time.deltaTime;
m_frames++;
if (m_timeleft <= 0.0)
{
m_fps = m_accum / m_frames;
m_timeleft = m_updateInterval;
m_accum = 0.0f;
m_frames = 0.0f;
}
}
private void OnGUI()
{
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
style.fontSize = 20;
style.normal.textColor = Color.white;
Rect rect = new Rect(10, 10, 200, 40);
GUI.Label(rect, string.Format("FPS: {0}", m_fps.ToString("f2")), style);
}
}
```
这个脚本会在屏幕左上角实时显示当前的帧率,您可以将其挂载在游戏对象上使用。希望能对您有所帮助!
阅读全文