Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An accurate FPS counter for Unity. Works in builds.
using UnityEngine;
using System.Collections;
public class Fps : MonoBehaviour
{
private float count;
private IEnumerator Start()
{
GUI.depth = 2;
while (true)
{
count = 1f / Time.unscaledDeltaTime;
yield return new WaitForSeconds(0.1f);
}
}
private void OnGUI()
{
GUI.Label(new Rect(5, 40, 100, 25), "FPS: " + Mathf.Round(count));
}
}
@TAKiKAXD
Copy link

good

@leonlol-dev
Copy link

good

@moffatjason
Copy link

good

@Erfan-Sheikh
Copy link

good

@morwoen
Copy link

morwoen commented Jul 24, 2022

Use Time.unscaledDeltaTime instead, so that you can continue to calculate an accurate FPS while the Time.timeScale is not 1.

@mstevenson
Copy link
Author

Replaced with Time.unscaledDeltaTime, thanks

@eerikmikael
Copy link

Thank you! Simple and works!

@Razorsun
Copy link

Thanks!!!!!

@dginovker
Copy link

dginovker commented Nov 22, 2022

Works great! If you're looking for a little bit more detail, replace OnGUI with this:

private void OnGUI()
{
    Rect location = new Rect(5, 5, 85, 25);
    string text = $"FPS: {Mathf.Round(count)}";
    Texture black = Texture2D.linearGrayTexture;
    GUI.DrawTexture(location, black, ScaleMode.StretchToFill);
    GUI.color = Color.black;
    GUI.skin.label.fontSize = 18;
    GUI.Label(location, text);
}

@omer9214
Copy link

omer9214 commented Dec 3, 2022

thanks mate

@EwigeDreamer
Copy link

EwigeDreamer commented Dec 20, 2022

good, thanks!

@jaisonrobson
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment