请用unity写一个查看帧率的脚本
时间: 2024-02-09 19:09:49 浏览: 26
好的,以下是一个简单的Unity脚本,可以用来查看帧率:
```csharp
using UnityEngine;
public class FPSDisplay : MonoBehaviour
{
float deltaTime = 0.0f;
void Update()
{
deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f;
}
void OnGUI()
{
int w = Screen.width, h = Screen.height;
GUIStyle style = new GUIStyle();
Rect rect = new Rect(0, 0, w, h * 2 / 100);
style.alignment = TextAnchor.UpperLeft;
style.fontSize = h * 2 / 100;
style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
float msec = deltaTime * 1000.0f;
float fps = 1.0f / deltaTime;
string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
GUI.Label(rect, text, style);
}
}
```
将上述代码保存为一个名为 `FPSDisplay.cs` 的文件,并将其添加到你的场景中的任何对象上。在运行游戏时,你将能够看到一个显示当前帧率的小文本框。