Skip to content

Instantly share code, notes, and snippets.

@mstevenson
Last active November 8, 2024 02:25
Show Gist options
  • Save mstevenson/5103365 to your computer and use it in GitHub Desktop.
Save mstevenson/5103365 to your computer and use it in GitHub Desktop.
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));
}
}
@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

@JayAree999
Copy link

nice

@IsmailAzzouz
Copy link

Thanks bruv

@rickomax
Copy link

rickomax commented May 9, 2023

good

@andrewle9510
Copy link

hi guys, why on my android device, fps only show 60 and 30 only. is there sth wrong?

@morwoen
Copy link

morwoen commented Aug 8, 2023

@andrewle9510 , In case you didn't find the answer in the link shared or for other people searching, such behaviour is due to vertical sync or vSync. You need to disable it in order to allow the application to run based on the performance of the machine or the frame rate set in the Application.targetFrameRate (which is essentially a maximum).

@pablxx
Copy link

pablxx commented Sep 19, 2023

Hi, to make it a bit more performant, since it's getting called so often, maybe you could cache that wait, something like

private WaitForSeconds countWait

assign it on Start()
countWait = new WaitForSeconds(0.1f)

and then just call

yield return countWait

Thanks for the code by the way!!

@AnythingNose
Copy link

Very good stuff thanks !

@BrokenHelmet
Copy link

BrokenHelmet commented Jan 17, 2024

Thanks for this!

I've also added to @dginovker code:

[SerializeField] private Rect location = new (5, 5, 85, 25);
[SerializeField] private int fontSize = 18;
[SerializeField] private Color32 fontColor = Color.black;

to change things on the fly (I'm working on a VR title so I have to nudge things around due to screen real estate).

*Sorry for the multiple edits
** UPDATE OnGUI doesn't work within the headset, but will display on the Game window within the editor.

@Ahmad1441
Copy link

To get smooth FPS (Weighted average over time):

using UnityEngine;
using System.Collections;

public class Fps : MonoBehaviour
{
    private float currentFps;
    private float smoothedFps;
    private float smoothingFactor = 0.1f; // Adjust this to control how much weight is given to recent FPS

    private IEnumerator Start()
    {
        GUI.depth = 2;
        while (true)
        {
            currentFps = 1f / Time.unscaledDeltaTime;

            // Apply exponential smoothing to calculate the weighted average
            smoothedFps = (smoothingFactor * currentFps) + (1f - smoothingFactor) * smoothedFps;

            yield return new WaitForSeconds(0.1f);
        }
    }

    private void OnGUI()
    {
        // Display the smoothed FPS
        GUI.Label(new Rect(5, 40, 100, 25), "FPS: " + Mathf.Round(smoothedFps));
    }
}

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