Guest User

Untitled

a guest
May 28th, 2011
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GUIScript : MonoBehaviour
  5. {
  6.  
  7.     public void OnGUI ()
  8.     {
  9.         // call the method to draw the FPS label
  10.         XGUI.xLabelFPS (3);
  11.     }
  12.  
  13.     public void FixedUpdate ()
  14.     {
  15.         // increment the time that has passed with the fixedDeltaTime
  16.         XGUI.timePassed++;
  17.     }
  18.    
  19. }
  20.  
  21. /// <summary>
  22. /// Class for storing some stuffz we need
  23. /// </summary>
  24. public abstract class XGUIAbstract {
  25.    
  26.  
  27.     private static float? _fixedDeltaTime;
  28.         /// <summary>
  29.     /// interval at which the FixedUpdate method is called
  30.     /// </summary>
  31.     protected static float fixedDeltaTime
  32.     {
  33.         get { if(!_fixedDeltaTime.HasValue) _fixedDeltaTime = Time.fixedDeltaTime;
  34.             return (float)_fixedDeltaTime; }
  35.     }
  36.    
  37.    
  38.  
  39.     private static float _timePassed = 0;
  40.         /// <summary>
  41.     /// How much time has passed since last FPS display
  42.     /// </summary>
  43.     public static float timePassed {
  44.         get { return _timePassed; }
  45.         set { _timePassed = value == 0 ? 0 : _timePassed+fixedDeltaTime; }
  46.     }
  47.    
  48.     /// <summary>
  49.     /// Store the FPS of last update
  50.     /// </summary>
  51.     protected static float framesPerSecond = 0;
  52.    
  53.     /// <summary>
  54.     /// Store how many frames have passed since last FPS update
  55.     /// </summary>
  56.     protected static int framesPassed = 0;
  57.    
  58. }
  59.  
  60. public class XGUI : XGUIAbstract
  61. {
  62.    
  63.     /// <summary>
  64.     /// stores how many seconds needs to pass for FPS update
  65.     /// </summary>
  66.     private static float _interval;
  67.    
  68.     private XGUI() {
  69.        
  70.     }
  71.    
  72.     public static void xLabelFPS ()
  73.     {
  74.         xLabelFPS(1);
  75.     }
  76.    
  77.     public static void xLabelFPS (float interval )
  78.     {
  79.         _interval = interval;
  80.         string fps = XGUI.calculateFPS ();
  81.         GUI.Label (new Rect (0, 0, 100, 50), fps);
  82.     }
  83.  
  84.     /// <summary>
  85.     /// Calculate the FPS
  86.     /// </summary>
  87.     /// <returns>String of FPS</returns>
  88.     protected static string calculateFPS ()
  89.     {
  90.         if (timePassed >= _interval) {
  91.             float FPS = (Time.frameCount - framesPassed) / timePassed;
  92.            
  93.             framesPassed = Time.frameCount;
  94.             framesPerSecond = FPS;
  95.             timePassed = 0;
  96.         }
  97.         return framesPerSecond.ToString ();
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment