In custom engines I've often made a frame rate meter that shows how much of the each frame was actually used. In other words if the game is locked to 60fps using vblank I show a graph of how much of each frame was used for processing and how much was just waiting for the next vblank. This gives a visual indication of how much more can be added to a scene without hitting my frame rate budget.

Is this possible to do in Unity?

What I tried:

I made a script that creates a System.Diagnostics.StopWatch and starts it. I set that script's Script Execution Order to be the very first script.

In the script's Update I record the ElapsedTicks from the stopwatch as startTicks. In the scripts's OnGUI I compute a deltaTicks as in

long deltaTicks = m_stopWatch.ElapsedTicks - startTicks;

I can then compute how much of a 60fps frame was taken with

long ticksPerFrame = System.Diagnostics.Stopwatch.Frequency / 60;
float amountOfFrameUsed = (float)((double)deltaTicks / ticksPerFrame);

Note: I get that OnGUI is called multiple times per frame but the way I end up storing amountOfFrameUsed I only end up recording the time from the last time OnGUI is called that frame.

Anyway, this does give me a graph to look at but I have no idea if what it's slowing me is correct for Unity.

In my own engine I'd record the time at VBlank and then in my code, when all processing was done I'd compute the elapsed time since vblank.

Is what I'm doing correct? Is there a better or another way I should be doing this? (note: I have not used the profiler because I wanted to see this FPS meter in a production app, not in the Editor where there is tons of overhead).

  • \$\begingroup\$ profiler tells you how much time is spent doing what \$\endgroup\$ – Almo Apr 10 '18 at 15:49
  • \$\begingroup\$ And my question says why I can't use the profiler so still looking for an answer. \$\endgroup\$ – gman Apr 10 '18 at 15:59
  • \$\begingroup\$ Sorry, I missed that. Apologies. \$\endgroup\$ – Almo Apr 10 '18 at 16:00
  • \$\begingroup\$ Thank you for the suggestion though! \$\endgroup\$ – gman Apr 10 '18 at 16:09
  • 1
    \$\begingroup\$ You can use the profiler with a build, not only in the editor. \$\endgroup\$ – Nikaas Apr 11 '18 at 6:17

Your method looks valid. If not a precise measure of used vs unused time, it will at least serve as a useful proxy, with values that climb when pre-render scripts or rendering workload take longer.

If you're not happy with the OnGUI approach, you could also try doing this in a Coroutine with:

 yield return new WaitForEndOfFrame(). 

This "waits until the end of the frame after all cameras and GUI is rendered, just before displaying the frame on screen."

For mobile games, you can also use the built-in profiler (on the device, not the editor profiler).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.