Advertisement
DaDogeDevelopment

FPS Display

Sep 30th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using TMPro;
  3.  
  4. public class FPSDisplayFR : MonoBehaviour
  5. {
  6.     public float updateInterval = 0.5f;
  7.     private float accum = 0.0f;
  8.     private int frames = 0;
  9.     private float timeleft;
  10.     public TextMeshPro fpsText;
  11.  
  12.     private void Start()
  13.     {
  14.         fpsText = GetComponent<TextMeshPro>();
  15.         if (fpsText == null)
  16.         {
  17.             Debug.LogError("FPSDisplay: No TextMeshPro component found.");
  18.         }
  19.         timeleft = updateInterval;
  20.     }
  21.  
  22.     private void Update()
  23.     {
  24.         timeleft -= Time.deltaTime;
  25.         accum += Time.timeScale / Time.deltaTime;
  26.         ++frames;
  27.  
  28.         if (timeleft <= 0.0)
  29.         {
  30.             float fps = accum / frames;
  31.             fpsText.text = string.Format("{0:F2} FPS", fps);
  32.  
  33.             if (fps < 30)
  34.                 fpsText.color = Color.yellow;
  35.             else
  36.                 if (fps < 10)
  37.                 fpsText.color = Color.red;
  38.             else
  39.                 fpsText.color = Color.green;
  40.  
  41.             timeleft = updateInterval;
  42.             accum = 0.0f;
  43.             frames = 0;
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement