FPS幀率統計是性能測試經常使用組件之一,下面分享一個簡單的FPS統計腳本。segmentfault
1.效果圖
性能
2.代碼
FPSUI.cs測試
using UnityEngine; using UnityEngine.UI; /// <summary> /// FPS幀率UI /// <para>ZhangYu 2018-02-10</para> /// <para>blog:https://segmentfault.com/a/1190000020159916</para> /// </summary> public class FPSUI : MonoBehaviour { public Text text; // 文本組件 public float sampleTime = 0.5f; // 採樣時間 private int frame; // 通過幀數 private float time = 0; // 運行時間 private void Update () { frame += 1; time += Time.deltaTime; // 刷新幀率 if (time >= sampleTime) { float fps = frame / time; text.text = "FPS:" + fps.ToString("F2"); frame = 0; time = 0; } } }