Advertisement
JustPrototype

Unity3D FPS Counter

Jan 14th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. /// <summary>
  4. /// Created by JustPrototype
  5. /// Prototyping.bplaced.net
  6. /// </summary>
  7. public class FPSCounter : MonoBehaviour
  8. {
  9.     public GameObject m_Panel_FPSCounter;// UI IMG for the output
  10.     public float m_updateInterval = 0.5f;
  11.     public bool m_ShowPFS;
  12.     private float m_Accum;
  13.     private int m_Frames;
  14.     private float m_Timeleft;
  15.     private float m_FPS;
  16.  
  17.     public void Start()
  18.     {
  19.         this.m_Timeleft = this.m_updateInterval;
  20.         this.m_FPS = 30f;
  21.     }
  22.  
  23.     public void Update()
  24.     {
  25.         this.m_Timeleft -= Time.deltaTime;
  26.         this.m_Accum += Time.timeScale / Time.deltaTime;
  27.         this.m_Frames++;
  28.         if ((double)this.m_Timeleft <= 0.0)
  29.         {
  30.             this.m_FPS = this.m_Accum / (float)this.m_Frames;
  31.             this.m_Timeleft = this.m_updateInterval;
  32.             this.m_Accum = 0f;
  33.             this.m_Frames = 0;
  34.         }
  35.         m_Panel_FPSCounter.GetComponent<Text>().text = "FPS: " + m_FPS.ToString("0");
  36.         this.UpdateGUI();
  37.     }
  38.  
  39.     private void UpdateGUI()
  40.     {
  41.         if (m_ShowPFS)
  42.             m_Panel_FPSCounter.SetActive(true);
  43.         else
  44.             m_Panel_FPSCounter.SetActive(false);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement