Saturday, November 7, 2015

FPS (Frames Per Second)

Computing FPS value is pretty important. When the game is tested on target hardware (especially on devices like Samsung TVs) we need to know exactly that value.

I am using following C# script assigned to the empty game object. I simply count number Update() method calls and every second I update the FPS value and I log it.
public class Fps : MonoBehaviour
{
 // Update interval is 1 second
 const float UPDATE_INTERVAL_1SEC = 1.0f;
 
 // Frames drawn over the interval
 private int frames  = 0;
 
 // Left time for current interval
 private float timeLeft;
 
 // Is the debug mode enabled?
 public bool debugEnabled = true;

 // FPS
 private int fps = -1;
 public int GetFps
 {
  get
  {
   return fps;
  }
 }
 
 void Start()
 {
  timeLeft = UPDATE_INTERVAL_1SEC;
 }

 void Update()
 {
  if ( debugEnabled )
  {
   // Decrease the time
   timeLeft -= Time.deltaTime;
   
   // Increase number of frames
   frames++;
   
   if( timeLeft <= 0.0 )
   {
    fps = frames;      
    timeLeft += UPDATE_INTERVAL_1SEC;
    frames = 0;
    Debug.Log ("[Fps]:[Update] === FPS: " + fps +"("+Time.time+")" );
   }
  }
 }
}

No comments:

Post a Comment