Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class UIFPSCounter : MonoBehaviour
- {
- static UIFPSCounter mInst;
- public GameObject root;
- public UILabel label;
- int mFPS = 0;
- int mCounter = 0;
- float mElapsed = 0f;
- void OnEnable () { mInst = this; }
- void OnDisable () { if (mInst == this) mInst = null; }
- void Update ()
- {
- ++mCounter;
- mElapsed += Time.deltaTime;
- if (mElapsed > 0.5f)
- {
- mFPS = Mathf.RoundToInt(mCounter / mElapsed);
- mElapsed = 0f;
- mCounter = 0;
- if (label != null && root.activeInHierarchy) label.text = mFPS.ToString();
- }
- }
- /// <summary>
- /// Show the FPS counter.
- /// </summary>
- static public void Show ()
- {
- if (mInst != null && mInst.root != null && !mInst.root.activeSelf)
- {
- if (mInst.label != null) mInst.label.text = mInst.mFPS.ToString();
- mInst.root.SetActive(true);
- }
- }
- /// <summary>
- /// Hide the FPS counter.
- /// </summary>
- static public void Hide () { if (mInst != null && mInst.root != null && mInst.root.activeSelf) mInst.root.SetActive(false); }
- /// <summary>
- /// Toggle the FPS counter on/off.
- /// </summary>
- static public void Toggle ()
- {
- if (mInst != null && mInst.root != null)
- {
- if (mInst.root.activeSelf) Hide();
- else Show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement