# Is it possible to measure Used vs Unused time in a 60hz frame in Unity?

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).

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

 yield return new WaitForEndOfFrame().