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.

37 lines
1.0 KiB

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