![c4feb1b028488af83d66fad3615ff7b4.png](https://i-blog.csdnimg.cn/blog_migrate/2f53e466aa69f6b2822b7e89fa0c75c0.jpeg)
在使用unity开发过程中,许多时候需要显示当前项目的帧率FPS,用于观察项目的流程度,那么如何计算FPS呢?请看下面代码演示:
public class FPSShow:MonoBehaviour
{
public Text[] ShowText;
private float accum;
private int frames;
void Start()
{
StartCoroutine(FPS());
}
void Update()
{
accum += Time.timeScale / Time.deltaTime;
++frames;
}
IEnumerator FPS()
{
// Infinite loop executed every "frenquency" secondes.
while (true)
{
// Update the FPS
float fps = accum / frames;
foreach (var ttt in ShowText)
{
ttt.text = fps.ToString("F1");
}
accum = 0.0F;
frames = 0;
yield return new WaitForSeconds(0.5f);
}
}
}