Unity FPS Counter
In video games, frames-per-second (or fps for short) is a value that represents the number of frames that the computer renders in one second.
Frames-per-second is a great indicator of performance and can be used during the optimization process, or simply to get feedback on how fast/smooth the game runs.
In this tutorial, I will be showing how to add a simple fps counter to your game in Unity.
Steps
To show fps in the game, we will need to create a script that will count frames and show them on the screen.
- Create a new script, call it "SC_FPSCounter" and paste the code below inside it:
SC_FPSCounter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SC_FPSCounter : MonoBehaviour
{
/* Assign this script to any object in the Scene to display frames per second */
public float updateInterval = 0.5f; //How often should the number update
float accum = 0.0f;
int frames = 0;
float timeleft;
float fps;
GUIStyle textStyle = new GUIStyle();
// Use this for initialization
void Start()
{
timeleft = updateInterval;
textStyle.fontStyle = FontStyle.Bold;
textStyle.normal.textColor = Color.white;
}
// Update is called once per frame
void Update()
{
timeleft -= Time.deltaTime;
accum += Time.timeScale / Time.deltaTime;
++frames;
// Interval ended - update GUI text and start new interval
if (timeleft <= 0.0)
{
// display two fractional digits (f2 format)
fps = (accum / frames);
timeleft = updateInterval;
accum = 0.0f;
frames = 0;
}
}
void OnGUI()
{
//Display the fps and round to 2 decimals
GUI.Label(new Rect(5, 5, 100, 25), fps.ToString("F2") + "FPS", textStyle);
}
}
- Attach the SC_FPSCounter script to any object in the Scene and press Play:
Fps should now be displayed in the top-left corner.