Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the frame-rate in a Unity scene

I'm making a project with Augmented Reality, using Unity and Vuforia extensions. I'm new to C#, but I was looking for a method similar to ARToolKit's getFrame(), and I'm really not finding anything.

My questions are:

  1. Is it necessary that I can calculate the frame-rate that my scene is operating at?
  2. Which scene object should i use to track the frame-rate?
like image 982
Joana Avatar asked Aug 27 '15 14:08

Joana


People also ask

How do I calculate my frame rate?

To calculate frames per second, you just take the number of rendered frames and divide it by the seconds passed.

What is fps rate in Unity?

Usually, the default frame rate on mobile platforms is 30 fps. To target the default frame rate, set Application. targetFrameRate to -1. To target a frame rate other than the maximum achievable frame rate or the platform default on mobile platforms, set Application.

How do I set framerate in Unity?

The simplest way to start controlling frame rate is to explicitly set the QualitySettings. vSyncCount so that rendering will occur at an interval related to the display device's refresh rate (e.g., for a 60Hz display, setting vSyncCount=2 will cause Unity to render at 30fps in sync with the display).


2 Answers

Thats as simple as:

public float avgFrameRate;

public void Update()
{
    avgFrameRate = Time.frameCount / Time.time;
}

Put this code in any MonoBehaviour and attatch it to any GameObject in the scene hierarchy.

Please note: this will only give you an average frame-rate. For a more current frame-rate, other answers have addressed effective ways of accomplishing that.

like image 75
maksymiuk Avatar answered Oct 20 '22 09:10

maksymiuk


None of the answers here consider the fact that the timescale can be modified in Unity and if it is, all the above approaches will be incorrect. This is because Time.Delta time is influenced by the timescale.

As such, you need to use Time.unscaledDeltaTime:

int fps = 0;
void Update () {
    fps = (int)(1f / Time.unscaledDeltaTime);
}
like image 31
pookie Avatar answered Oct 20 '22 08:10

pookie