Advertisement
PLG_turret

Dynamic FPS Counter

Aug 21st, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ClientSettings : MonoBehaviour
  6. {
  7.     public enum TargetFrameRate { Limit_30 = 30, Limit_60 = 60, Limit_120 = 120,Limit_144 = 144, Limit_240 = 240, Unlimited = 1337}
  8.     public TargetFrameRate frameRate = TargetFrameRate.Unlimited;
  9.  
  10.     public enum VSyncLevels { disabled, MonitorRefreshRate, HalfMonitorRefreshRate }
  11.     public VSyncLevels vSync = VSyncLevels.MonitorRefreshRate;
  12.  
  13.     public bool ShowFPS = false;
  14.  
  15.     private string fpsLabel;
  16.     private float count;
  17.     private int hideOffset = 0;
  18.     private Color fpsColor = Color.green;
  19.  
  20.     IEnumerator Start()
  21.     {
  22.         QualitySettings.vSyncCount = (int)vSync;
  23.         Application.targetFrameRate = (int)frameRate;
  24.  
  25.         float maxFPS = 0f;
  26.         float minFPS = 1337f;
  27.  
  28.         GUI.depth = 2;
  29.  
  30.         QualitySettings.vSyncCount = (int)vSync;
  31.         Application.targetFrameRate = (int)frameRate;
  32.  
  33.         while (true)
  34.         {
  35.             if (Time.timeScale == 1 && ShowFPS)
  36.             {
  37.                 hideOffset = 0;
  38.                 yield return new WaitForSeconds(0.1f);
  39.                 count = (1 / Time.deltaTime);
  40.  
  41.                 if (count > maxFPS) maxFPS = count;
  42.                 if (count < minFPS) minFPS = count;
  43.                                          
  44.                 string target = "(" + frameRate.ToString() + ")";
  45.                 if (frameRate == TargetFrameRate.Unlimited) target = "";
  46.                 fpsLabel = "" + Mathf.Round(count) + target + "\nmin:" + Mathf.Round(minFPS) +"\nmax:"+ Mathf.Round(maxFPS);
  47.  
  48.                 //Color Changing in relation to fps count
  49.                 if (Mathf.Round(count) < 30) fpsColor = Color.red;
  50.                 if (Mathf.Round(count) < 59 && Mathf.Round(count) >= 30) fpsColor = Color.yellow;
  51.                 if(Mathf.Round(count) >= 60) fpsColor = Color.green;
  52.             }
  53.             else
  54.             {
  55.                 //Reset and hide box outside the screen
  56.                 fpsLabel = "";
  57.                 maxFPS = 0f;
  58.                 minFPS = 1337f;
  59.                 hideOffset = -500;
  60.             }
  61.             yield return new WaitForSeconds(0.5f);
  62.         }
  63.     }
  64.  
  65.     void OnGUI()
  66.     {
  67.         GUI.color = fpsColor;
  68.         GUI.Box(new Rect(hideOffset + 1, 1, 125, 50), fpsLabel);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement