How to Display FPS (Frames Per Second) in Unity – Unity By Example

How to Display FPS (Frames Per Second) in Unity

FPS (Frames Per Second) is a value that represents the number of frames that the computer renders in one second.

Steps

  • Create script FPS.cs
using System.Collections;
using TMPro;
using UnityEngine;

[RequireComponent(typeof(TMP_Text))]
public class FPS : MonoBehaviour
{
    private TMP_Text _fpsText;

    private void Start()
    {
        _fpsText = GetComponent<TMP_Text>();
        StartCoroutine(FramesPerSecond());
    }

    private IEnumerator FramesPerSecond()
    {
        while (true)
        {
            int fps = (int) (1f / Time.deltaTime);
            DisplayFPS(fps);

            yield return new WaitForSeconds(0.2f);
        }
    }

    private void DisplayFPS(float fps)
    {
        _fpsText.text = $"{fps} FPS";
    }
}
  • Add Text Mesh Pro object to a Scene.
  • Drag and drop FPS.cs to Text Mesh Pro in the Scene.

Result

Now FPS will be displayed on a screen.

Download

5 2 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments