FPSDisplay.cs 652 B

12345678910111213141516171819202122232425262728293031
  1. using UnityEngine;
  2. public class FPSDisplay:MonoBehaviour {
  3. private int frames = 0;
  4. private float updateInterval = 0.5f; // Time to wait, in seconds
  5. private float lastUpdate = 0;
  6. private TextMesh textMesh;
  7. void Start() {
  8. textMesh = GetComponent<TextMesh>();
  9. }
  10. void Update() {
  11. frames++;
  12. if (Time.time - lastUpdate > updateInterval) {
  13. updateValues();
  14. }
  15. }
  16. void updateValues() {
  17. float timePassed = Time.time - lastUpdate;
  18. float msec = timePassed / (float)frames * 1000f;
  19. float fps = frames / timePassed;
  20. lastUpdate += timePassed;
  21. frames = 0;
  22. textMesh.text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
  23. }
  24. }