Unity FPS Counter


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 9:06 p.m.

FORMAT: Text only

SIZE: 1.3 kB

HITS: 1171

  1. // Attach this to a GUIText to make a frames/second indicator.
  2. //
  3. // It calculates frames/second over each updateInterval,
  4. // so the display does not keep changing wildly.
  5. //
  6. // It is also fairly accurate at very low FPS counts (<10).
  7. // We do this not by simply counting frames per interval, but
  8. // by accumulating FPS for each frame. This way we end up with
  9. // correct overall FPS even if the interval renders something like
  10. // 5.5 frames.
  11. var updateInterval = 0.5;
  12. private var accum = 0.0; // FPS accumulated over the interval
  13. private var frames = 0; // Frames drawn over the interval
  14. private var timeleft : float; // Left time for current interval
  15. function Start()
  16. {
  17. if( !guiText )
  18. {
  19. print ("FramesPerSecond needs a GUIText component!");
  20. enabled = false;
  21. return;
  22. }
  23. timeleft = updateInterval;
  24. }
  25. function Update()
  26. {
  27. timeleft -= Time.deltaTime;
  28. accum += Time.timeScale/Time.deltaTime;
  29. ++frames;
  30. // Interval ended - update GUI text and start new interval
  31. if( timeleft <= 0.0 )
  32. {
  33. // display two fractional digits (f2 format)
  34. guiText.text = "" + (accum/frames).ToString("f2");
  35. timeleft = updateInterval;
  36. accum = 0.0;
  37. frames = 0;
  38. }
  39. }
  40. @script RequireComponent (GUIText)

comments powered by Disqus