FPSCounter.cs 887 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. /// <summary>
  7. /// This script calculate the current fps and show it to a text ui.
  8. /// </summary>
  9. namespace Imagine.WebAR.Samples
  10. {
  11. public class FPSCounter : MonoBehaviour
  12. {
  13. [SerializeField] private TextMeshProUGUI fpsText;
  14. public float updateRateSeconds = 3.0f;
  15. int frameCount = 0;
  16. float elapsedTime = 0f;
  17. float fps = 0f;
  18. void Update()
  19. {
  20. frameCount++;
  21. elapsedTime += Time.unscaledDeltaTime;
  22. if (elapsedTime >= updateRateSeconds)
  23. {
  24. fps = 1 / (elapsedTime / frameCount);
  25. frameCount = 0;
  26. elapsedTime = 0;
  27. fpsText.text = System.Math.Round(fps, 1).ToString("0.0");
  28. }
  29. }
  30. }
  31. }