Advertisement
ArenMook

FPS counter

Jan 17th, 2017
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class UIFPSCounter : MonoBehaviour
  4. {
  5.     static UIFPSCounter mInst;
  6.  
  7.     public GameObject root;
  8.     public UILabel label;
  9.  
  10.     int mFPS = 0;
  11.     int mCounter = 0;
  12.     float mElapsed = 0f;
  13.  
  14.     void OnEnable () { mInst = this; }
  15.     void OnDisable () { if (mInst == this) mInst = null; }
  16.  
  17.     void Update ()
  18.     {
  19.         ++mCounter;
  20.         mElapsed += Time.deltaTime;
  21.  
  22.         if (mElapsed > 0.5f)
  23.         {
  24.             mFPS = Mathf.RoundToInt(mCounter / mElapsed);
  25.             mElapsed = 0f;
  26.             mCounter = 0;
  27.             if (label != null && root.activeInHierarchy) label.text = mFPS.ToString();
  28.         }
  29.     }
  30.  
  31.     /// <summary>
  32.     /// Show the FPS counter.
  33.     /// </summary>
  34.  
  35.     static public void Show ()
  36.     {
  37.         if (mInst != null && mInst.root != null && !mInst.root.activeSelf)
  38.         {
  39.             if (mInst.label != null) mInst.label.text = mInst.mFPS.ToString();
  40.             mInst.root.SetActive(true);
  41.         }
  42.     }
  43.  
  44.     /// <summary>
  45.     /// Hide the FPS counter.
  46.     /// </summary>
  47.  
  48.     static public void Hide () { if (mInst != null && mInst.root != null && mInst.root.activeSelf) mInst.root.SetActive(false); }
  49.  
  50.     /// <summary>
  51.     /// Toggle the FPS counter on/off.
  52.     /// </summary>
  53.  
  54.     static public void Toggle ()
  55.     {
  56.         if (mInst != null && mInst.root != null)
  57.         {
  58.             if (mInst.root.activeSelf) Hide();
  59.             else Show();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement