duck

duck

Sep 3rd, 2010
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. // Frames Per Second visual display
  2. // Robotduck 2010
  3.  
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. public class FramesPerSecond : MonoBehaviour {
  8.  
  9.     private float updatePeriod = 0.5f;
  10.     private float[] runningAverages = { 60, 60, 60, 60, 60 };
  11.     private int runningAverageIndex = 0;
  12.     private float fps;
  13.     private float countFrames = 0;
  14.     private float nextAverageTime;
  15.     private float lastSampleTime;
  16.    
  17.     void Start ()
  18.     {
  19.         float now = Time.realtimeSinceStartup;
  20.         nextAverageTime = now + updatePeriod;
  21.         lastSampleTime = now;
  22.     }
  23.  
  24.     void Update ()
  25.     {
  26.         float now = Time.realtimeSinceStartup;
  27.         if (now > nextAverageTime)
  28.         {
  29.             float samplePeriod = now - lastSampleTime;
  30.             lastSampleTime = now;
  31.             float periodFPS = countFrames / samplePeriod;
  32.             runningAverageIndex++;
  33.             runningAverageIndex %= runningAverages.Length;
  34.             runningAverages[runningAverageIndex] = periodFPS;
  35.            
  36.             fps = 0;
  37.             foreach(float entry in runningAverages)
  38.             {
  39.                 fps += entry;
  40.             }
  41.             fps /= runningAverages.Length;
  42.  
  43.             nextAverageTime += updatePeriod;
  44.             countFrames = 0;
  45.         }
  46.        
  47.         countFrames++;
  48.     }
  49.    
  50.     void OnGUI ()
  51.     {
  52.         GUI.Label (new Rect (10, 10, 100, 20), ((int) fps)+" FPS");
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment