Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This might be overkill for most people, that's why I hadn't posted it when I implemented it. But it's very robust and flexible.</p> <p>It stores a Queue with the last frame times, so it can accurately calculate an average FPS value much better than just taking the last frame into consideration.</p> <p>It also allows you to ignore one frame, if you are doing something that you know is going to artificially screw up that frame's time.</p> <p>It also allows you to change the number of frames to store in the Queue as it runs, so you can test it out on the fly what is the best value for you.</p> <pre class="lang-cs prettyprint-override"><code>// Number of past frames to use for FPS smooth calculation - because // Unity's smoothedDeltaTime, well - it kinda sucks private int frameTimesSize = 60; // A Queue is the perfect data structure for the smoothed FPS task; // new values in, old values out private Queue&lt;float&gt; frameTimes; // Not really needed, but used for faster updating then processing // the entire queue every frame private float __frameTimesSum = 0; // Flag to ignore the next frame when performing a heavy one-time operation // (like changing resolution) private bool _fpsIgnoreNextFrame = false; //============================================================================= // Call this after doing a heavy operation that will screw up with FPS calculation void FPSIgnoreNextFrame() { this._fpsIgnoreNextFrame = true; } //============================================================================= // Smoothed FPS counter updating void Update() { if (this._fpsIgnoreNextFrame) { this._fpsIgnoreNextFrame = false; return; } // While looping here allows the frameTimesSize member to be changed dinamically while (this.frameTimes.Count &gt;= this.frameTimesSize) { this.__frameTimesSum -= this.frameTimes.Dequeue(); } while (this.frameTimes.Count &lt; this.frameTimesSize) { this.__frameTimesSum += Time.deltaTime; this.frameTimes.Enqueue(Time.deltaTime); } } //============================================================================= // Public function to get smoothed FPS values public int GetSmoothedFPS() { return (int)(this.frameTimesSize / this.__frameTimesSum * Time.timeScale); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload