Advertisement
JustPrototype

Unity3D FPSDisplay Counter

Jan 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using UnityEngine;
  2. /// <summary>
  3. /// GUI FPS Display Counter with ms
  4. /// JustPrototype
  5. /// </summary>
  6. public class FPSDisplay : MonoBehaviour
  7. {
  8.     private float deltaTime;
  9.     private float updateDelay;
  10.     private float msec;
  11.     private float fps;
  12.     private string text;
  13.  
  14.     private void Start()
  15.     {
  16.         this.updateDelay = Time.time + 0.5f;
  17.     }
  18.  
  19.     private void Update()
  20.     {
  21.         this.deltaTime += (Time.deltaTime - this.deltaTime) * 0.1f;
  22.     }
  23.  
  24.     private void OnGUI()
  25.     {
  26.         int width = Screen.width;
  27.         int height = Screen.height;
  28.         GUIStyle guistyle = new GUIStyle();
  29.         Rect position = new Rect(0f, 0f, (float)width, (float)(height * 2 / 100));
  30.         guistyle.alignment = TextAnchor.UpperLeft;
  31.         guistyle.fontSize = height * 2 / 100;
  32.         guistyle.normal.textColor = new Color(0f, 0f, 0.5f, 1f);
  33.         if (Time.time > this.updateDelay)
  34.         {
  35.             this.msec = this.deltaTime * 1000f;
  36.             this.fps = 1f / this.deltaTime;
  37.             this.text = string.Format("{0:0.0} ms ({1:0.} fps)", this.msec, this.fps);
  38.             this.updateDelay = Time.time + 0.5f;
  39.         }
  40.         GUI.Label(position, this.text, guistyle);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement