Accurate Frames Per Second Count

What's the best or most accurate way to get a FPS count? Should the frameCount and Time.time division be put in Update or LateUpdate, or does it matter?

Update or LateUpdate doesn't matter. The question is what do you mean by most accurate. frames per second is never a constant value, it can fluctuate even within one second.

var fps = 1.0/Time.deltaTime;

that gives you the current FPS but that will change every frame.

You could smooth the delta, but it will be less accurate.

var deltaTime = 0.0;
var fps = 0.0;

function Update()
{
    deltaTime += Time.deltaTime;
    deltaTime /= 2.0;
    fps = 1.0/deltaTime;
}

If you want to display the value, the best way is to accumulate the frames yourself and calculate the FPS in fix time steps.

var frameCount = 0;
var nextUpdate = 0.0;
var fps = 0.0;
var updateRate = 4.0;  // 4 updates per sec.

function Start()
{
    nextUpdate = Time.time;
}

function Update()
{
    frameCount++;
    if (Time.time > nextUpdate)
    {
        nextUpdate += 1.0/updateRate;
        fps = frameCount * updateRate;
        frameCount = 0;
    }
}

edit
Added a little bit more accuracy to the second solution (like mentioned in the comments) and here’s another one that’s probably the “most” accurate.

var frameCount = 0;
var dt = 0.0;
var fps = 0.0;
var updateRate = 4.0;  // 4 updates per sec.

function Update()
{
    frameCount++;
    dt += Time.deltaTime;
    if (dt > 1.0/updateRate)
    {
        fps = frameCount / dt ;
        frameCount = 0;
        dt -= 1.0/updateRate;
    }
}

Do you mean after the project has been built? If you want to just see the FPS, you can click the "Stats"-button while the program runs in the editor. It displays the FPS.

Bit late to the party, below is the solution provided in the Unity’s Standard Assets.

using System;
using UnityEngine;
using UnityEngine.UI;

namespace UnityStandardAssets.Utility {
  [RequireComponent(typeof(Text))]
  public class FPSCounter : MonoBehaviour {
    const float fpsMeasurePeriod = 0.5f;
    private int m_FpsAccumulator = 0;
    private float m_FpsNextPeriod = 0;
    private int m_CurrentFps;
    const string display = "{0} FPS";
    private Text m_Text;


    private void Start() {
      m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
      m_Text = GetComponent<Text>();
    }


    private void Update() {
      // measure average frames per second
      m_FpsAccumulator++;
      if (Time.realtimeSinceStartup > m_FpsNextPeriod) {
        m_CurrentFps = (int)(m_FpsAccumulator / fpsMeasurePeriod);
        m_FpsAccumulator = 0;
        m_FpsNextPeriod += fpsMeasurePeriod;
        m_Text.text = string.Format(display, m_CurrentFps);
      }
    }
  }
}

That was an awesome insight and wanted to share my own version, based off the same thing but another way to approach it with refresh interval.

 public class FPSCounter : MonoBehaviour {

    public float timer, refresh, avgFramerate;
    string display = "{0} FPS";
    private Text m_Text;

    private void Start()
    {
        m_Text = GetComponent<Text>();
    }


    private void Update()
    {
        float timelapse = Time.smoothDeltaTime;
        timer = timer <= 0 ? refresh : timer -= timelapse;

        if(timer <= 0) avgFramerate = (int) (1f / timelapse);
        m_Text.text = string.Format(display,avgFramerate.ToString());
    } 
}

None of these are counting frames but rather a float value within the time spent during the update.

These solutions are completely false and made up.

The correct solution would be to look into .Net API and getting the GPU’s actual frame completion. Once the frame is completed, you compare that to the time spent.