You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Unity Show FPS in scene

Ben Liu
Ben Liu

The following script will help you to show FPS in scene:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FBSManager : MonoBehaviour
{
    public int rangeInt;
    public float updateInterval = 0.5F;
    private float accum = 0;
    private int frames = 0;
    private float timeleft;
    private string stringFps;

    void Start()
    {
        timeleft = updateInterval;
        Application.targetFrameRate = 30;
    }

    void Update()
    {
        timeleft -= Time.deltaTime;
        accum += Time.timeScale / Time.deltaTime;
        ++frames;
        if (timeleft <= 0.0)
        {
            float fps = accum / frames;
            string format = System.String.Format("{0:F2} FPS", fps);
            stringFps = format;
            timeleft = updateInterval;
            accum = 0.0F;
            frames = 0;
        }
    }

    void OnGUI()
    {
        GUIStyle guiStyle = GUIStyle.none;
        guiStyle.fontSize = 30;
        guiStyle.normal.textColor = Color.red;
        guiStyle.alignment = TextAnchor.UpperLeft;
        Rect rt = new Rect(50, 50, 100, 100);
        GUI.Label(rt, stringFps, guiStyle);
    }
}

How to use?

If you have a GameManager in your scene, add the above script as a component to it. Or you can attach this script to the camera. One example:

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp