Eonirr

Untitled

Nov 21st, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEditor;
  3. using UnityEngine;
  4.  
  5. #if UNITY_EDITOR
  6. [InitializeOnLoad]
  7. #endif
  8.  
  9. public class FPSDisplay : MonoBehaviour
  10. {
  11. [SerializeField] private Rect startRect = new Rect(10, 10, 145, 40);
  12. [SerializeField] private float fps;
  13. [SerializeField] private int MaxFPS = 0;
  14. [SerializeField] private int MinFPS = 9999;
  15. public bool updateColor = true;
  16. public bool allowDrag = true;
  17. public float frequency = 0.5F;
  18. private float accum = 0f;
  19. private int frames = 0;
  20. private Color color = Color.white;
  21. private GUIStyle style;
  22.  
  23. #if UNITY_EDITOR
  24. [MenuItem("GameObject/Utils/FPSDisplay", false, 10)]
  25. static void CreateCustomGameObject(MenuCommand menuCommand)
  26. {
  27. GameObject go = new GameObject("FPSDisplay");
  28. go.AddComponent<FPSDisplay>();
  29. GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
  30. Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
  31. Selection.activeObject = go;
  32. }
  33. #endif
  34.  
  35. void Start()
  36. {
  37. StartCoroutine(FPS());
  38. InvokeRepeating(nameof(GetMinMax), 1f, 1f);
  39. }
  40.  
  41. void Update()
  42. {
  43. accum += Time.timeScale / Time.deltaTime;
  44. ++frames;
  45. }
  46.  
  47. void GetMinMax()
  48. {
  49. if (MaxFPS < fps)
  50. MaxFPS = (int)Mathf.Max(fps);
  51. if (MinFPS > fps)
  52. MinFPS = (int)Mathf.Min(fps);
  53. }
  54.  
  55. IEnumerator FPS()
  56. {
  57. while (true)
  58. {
  59. fps = accum / frames;
  60. color = (fps >= 30) ? new Color(0, 255, 0) : ((fps > 10) ? new Color(255, 0, 0) : new Color(255, 165, 0));
  61. accum = 0;
  62. frames = 0;
  63. yield return new WaitForSeconds(frequency);
  64. }
  65. }
  66.  
  67. void OnGUI()
  68. {
  69. if (style == null)
  70. {
  71. style = new GUIStyle(GUI.skin.label);
  72. style.normal.textColor = Color.white;
  73. style.alignment = TextAnchor.MiddleCenter;
  74. }
  75. GUI.color = updateColor ? color : Color.white;
  76. startRect = ClampToScreen(GUI.Window(0, startRect, FPSWindow, ""));
  77. }
  78.  
  79. void FPSWindow(int windowID)
  80. {
  81. GUI.Label(new Rect(0, 0, startRect.width, startRect.height), $"{(int)fps} FPS\n[{MinFPS}Min {MaxFPS}Max]", style);
  82. if (allowDrag) GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
  83. }
  84.  
  85. private Rect ClampToScreen(Rect r)
  86. {
  87. r.x = Mathf.Clamp(r.x, 0, Screen.width - r.width);
  88. r.y = Mathf.Clamp(r.y, 0, Screen.height - r.height);
  89. return r;
  90. }
  91. }
  92.  
Add Comment
Please, Sign In to add comment