Skip to content

Instantly share code, notes, and snippets.

@mstevenson
Last active April 19, 2024 03:04
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • 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));
}
}
@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.

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