Fps.cs 947 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class Fps : MonoBehaviour {
  6. int FrameCount = 0;
  7. private float _framesPerSecond;
  8. private TextMesh textMesh;
  9. // Use this for initialization
  10. void Start () {
  11. textMesh = GetComponent<TextMesh>();
  12. if(textMesh) {
  13. StartCoroutine(CalculateFramesPerSecond());
  14. }
  15. }
  16. // Update is called once per frame
  17. void Update () {
  18. FrameCount++;
  19. textMesh.text = string.Format("{0:F2}", _framesPerSecond);
  20. }
  21. private IEnumerator CalculateFramesPerSecond() {
  22. int lastFrameCount = 0;
  23. while (true) {
  24. yield return new WaitForSecondsRealtime(1f);
  25. var elapsedFrames = FrameCount - lastFrameCount;
  26. _framesPerSecond = elapsedFrames / 1f;
  27. lastFrameCount = FrameCount;
  28. }
  29. }
  30. }