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