Advertisement
Purianite

Untitled

Mar 25th, 2012
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. SS// Attach this to a GUIText to make a frames/second indicator.
  2.  
  3. //
  4.  
  5. // It calculates frames/second over each updateInterval,
  6.  
  7. // so the display does not keep changing wildly.
  8.  
  9. //
  10.  
  11. // It is also fairly accurate at very low FPS counts (<10).
  12.  
  13. // We do this not by simply counting frames per interval, but
  14.  
  15. // by accumulating FPS for each frame. This way we end up with
  16.  
  17. // correct overall FPS even if the interval renders something like
  18.  
  19. // 5.5 frames.
  20.  
  21.  
  22.  
  23. var updateInterval = 0.5;
  24.  
  25. var grazer : GameObject;
  26.  
  27. var grazerComp;
  28.  
  29.  
  30.  
  31. private var accum = 0.0; // FPS accumulated over the interval
  32.  
  33. private var frames = 0; // Frames drawn over the interval
  34.  
  35. private var timeleft : float; // Left time for current interval
  36.  
  37.  
  38.  
  39. function Start()
  40.  
  41. {
  42.  
  43. if( !guiText )
  44.  
  45. {
  46.  
  47. print ("FramesPerSecond needs a GUIText component!");
  48.  
  49. enabled = false;
  50.  
  51. return;
  52.  
  53. }
  54.  
  55. timeleft = updateInterval;
  56.  
  57. grazerComp = grazer.GetComponent(Graze);
  58.  
  59. }
  60.  
  61.  
  62.  
  63. function Update()
  64.  
  65. {
  66.  
  67. timeleft -= Time.deltaTime;
  68.  
  69. accum += Time.timeScale/Time.deltaTime;
  70.  
  71. ++frames;
  72.  
  73. grazerComp = grazer.GetComponent(Graze);
  74.  
  75.  
  76.  
  77. // Interval ended - update GUI text and start new interval
  78.  
  79. if( timeleft <= 0.0 )
  80.  
  81. {
  82.  
  83. // display two fractional digits (f2 format)
  84.  
  85. guiText.text = "FPS: " + (accum/frames).ToString("f2") + "\nGraze: " + grazerComp.grazeCount.ToString();
  86.  
  87. timeleft = updateInterval;
  88.  
  89. accum = 0.0;
  90.  
  91. frames = 0;
  92.  
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement