You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.0 KiB

5 years ago
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace FogVolumeUtils.FPSCounter
  5. {
  6. [RequireComponent(typeof(Text))]
  7. public class FPSCounter : MonoBehaviour
  8. {
  9. const float fpsMeasurePeriod = 0.5f;
  10. private int m_FpsAccumulator = 0;
  11. private float m_FpsNextPeriod = 0;
  12. private int m_CurrentFps;
  13. const string display = "{0} FPS";
  14. private Text m_Text;
  15. private void Start()
  16. {
  17. m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
  18. m_Text = GetComponent<Text>();
  19. }
  20. private void Update()
  21. {
  22. // measure average frames per second
  23. m_FpsAccumulator++;
  24. if (Time.realtimeSinceStartup > m_FpsNextPeriod)
  25. {
  26. m_CurrentFps = (int)(m_FpsAccumulator / fpsMeasurePeriod);
  27. m_FpsAccumulator = 0;
  28. m_FpsNextPeriod += fpsMeasurePeriod;
  29. m_Text.text = string.Format(display, m_CurrentFps);
  30. }
  31. }
  32. }
  33. }