Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- [RequireComponent(typeof(Text))]
- public class FPSCounter : MonoBehaviour {
- [HideInInspector]
- public float fps = 0.0f;
- public float updateRate = 4;
- private Text outputText;
- private int frameCount = 0;
- private float dTime = 0.0f;
- private string textColor {
- get {
- if(fps <= 15f)
- return "red";
- if(fps <= 25f)
- return "orange";
- if(fps > 50f)
- return "lime";
- return "";
- }
- }
- private string formatedText {
- get {
- return "FPS: <color=" + textColor + ">" + fps.ToString("00.0") + "</color>";
- }
- }
- void Start() {
- outputText = GetComponent<Text>();
- }
- void Update() {
- frameCount++;
- dTime += Time.deltaTime;
- if(dTime > 1.0f / updateRate) {
- fps = frameCount / dTime;
- frameCount = 0;
- dTime -= 1.0f / updateRate;
- }
- outputText.text = formatedText;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement