using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class FPSDisplay : MonoBehaviour { // Start is called before the first frame update float updateInterval = 1f; // 更新帧率的时间间隔为1秒 float accum = 0f; // 累积时间 int frames = 0; // 在 updateInterval 时间内运行了多少帧 float timeLeft; string fpsFormat; public Text fff; void Start() { timeLeft = updateInterval; } // Update is called once per frame void Update() { timeLeft -= Time.deltaTime; accum += Time.timeScale / Time.deltaTime; ++frames; if (timeLeft <= 0) { float fps = accum / frames; fpsFormat = System.String.Format("{0:F2} FPS", fps); // 保留两位小数 //Debug.LogError(fpsFormat); timeLeft = updateInterval; accum = 0f; frames = 0; fff.text = fpsFormat; } } }