Advertisement
Muk99

FPSCounter

Jan 8th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. [RequireComponent(typeof(Text))]
  5. public class FPSCounter : MonoBehaviour {
  6.  
  7.     [HideInInspector]
  8.     public float fps = 0.0f;
  9.     public float updateRate = 4;
  10.  
  11.     private Text outputText;
  12.     private int frameCount = 0;
  13.     private float dTime = 0.0f;
  14.     private string textColor {
  15.         get {
  16.             if(fps <= 15f)
  17.                 return "red";
  18.             if(fps <= 25f)
  19.                 return "orange";
  20.             if(fps > 50f)
  21.                 return "lime";
  22.             return "";
  23.         }
  24.     }
  25.     private string formatedText {
  26.         get {
  27.             return "FPS: <color=" + textColor + ">" + fps.ToString("00.0") + "</color>";
  28.         }
  29.     }
  30.  
  31.     void Start() {
  32.         outputText = GetComponent<Text>();
  33.     }
  34.  
  35.     void Update() {
  36.         frameCount++;
  37.         dTime += Time.deltaTime;
  38.         if(dTime > 1.0f / updateRate) {
  39.             fps = frameCount / dTime;
  40.             frameCount = 0;
  41.             dTime -= 1.0f / updateRate;
  42.         }
  43.  
  44.         outputText.text = formatedText;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement