Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class FPSCounter : MonoBehaviour
  6. {
  7. public static float msec;
  8. public static float fps;
  9. public static float avgFps
  10. {
  11. get
  12. {
  13. return (minFps + maxFps) / 2;
  14. }
  15. }
  16. public static float maxFps = Mathf.NegativeInfinity;
  17. public static float minFps = Mathf.Infinity;
  18. public bool showFPS;
  19. public Text text;
  20. float deltaTime = 0.0f;
  21.  
  22. public void Awake()
  23. {
  24. StartCoroutine(ResetCounter(3));
  25. }
  26.  
  27. void Update()
  28. {
  29. deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
  30. msec = deltaTime * 1000.0f;
  31. fps = 1.0f / deltaTime;
  32.  
  33. minFps = fps < minFps ? fps : minFps;
  34. maxFps = fps > maxFps ? fps : maxFps;
  35.  
  36. if (showFPS)
  37. text.text = ((int)fps).ToString();
  38. else
  39. text.gameObject.SetActive(false);
  40. }
  41.  
  42. public IEnumerator ResetCounter(float period)
  43. {
  44. while (true)
  45. {
  46. maxFps = Mathf.NegativeInfinity;
  47. minFps = Mathf.Infinity;
  48. yield return new WaitForSeconds(period);
  49. }
  50.  
  51. }
  52.  
  53. //void OnGUI()
  54. //{
  55. // if (!showFPS)
  56. // return;
  57.  
  58. // int w = Screen.width, h = Screen.height;
  59. // GUIStyle style = new GUIStyle();
  60. // Rect rect = new Rect(0, 0, w, h * 10 / 100);
  61. // style.alignment = TextAnchor.UpperRight;
  62. // style.fontSize = h * 3 / 100;
  63. // style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
  64.  
  65. // GUI.Label(rect, "\nMin " + minFps, style);
  66. // GUI.Label(rect, "\n\nMax " + maxFps, style);
  67. // GUI.Label(rect, "\n\n\nAvg " + avgFps, style);
  68.  
  69. // if (fps < avgFps)
  70. // style.normal.textColor = Color.red;
  71. // else
  72. // style.normal.textColor = Color.green;
  73. // string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
  74. // GUI.Label(rect, text, style);
  75.  
  76. //}
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement