TMP_UiFrameRateCounter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class TMP_UiFrameRateCounter : MonoBehaviour
  6. {
  7. public float UpdateInterval = 5.0f;
  8. private float m_LastInterval = 0;
  9. private int m_Frames = 0;
  10. public enum FpsCounterAnchorPositions { TopLeft, BottomLeft, TopRight, BottomRight };
  11. public FpsCounterAnchorPositions AnchorPosition = FpsCounterAnchorPositions.TopRight;
  12. private string htmlColorTag;
  13. private const string fpsLabel = "{0:2}</color> <#8080ff>FPS \n<#FF8000>{1:2} <#8080ff>MS";
  14. private TextMeshProUGUI m_TextMeshPro;
  15. private RectTransform m_frameCounter_transform;
  16. private FpsCounterAnchorPositions last_AnchorPosition;
  17. void Awake()
  18. {
  19. if (!enabled)
  20. return;
  21. Application.targetFrameRate = 1000;
  22. GameObject frameCounter = new GameObject("Frame Counter");
  23. m_frameCounter_transform = frameCounter.AddComponent<RectTransform>();
  24. m_frameCounter_transform.SetParent(this.transform, false);
  25. m_TextMeshPro = frameCounter.AddComponent<TextMeshProUGUI>();
  26. m_TextMeshPro.font = Resources.Load<TMP_FontAsset>("Fonts & Materials/LiberationSans SDF");
  27. m_TextMeshPro.fontSharedMaterial = Resources.Load<Material>("Fonts & Materials/LiberationSans SDF - Overlay");
  28. m_TextMeshPro.enableWordWrapping = false;
  29. m_TextMeshPro.fontSize = 36;
  30. m_TextMeshPro.isOverlay = true;
  31. Set_FrameCounter_Position(AnchorPosition);
  32. last_AnchorPosition = AnchorPosition;
  33. }
  34. void Start()
  35. {
  36. m_LastInterval = Time.realtimeSinceStartup;
  37. m_Frames = 0;
  38. }
  39. void Update()
  40. {
  41. if (AnchorPosition != last_AnchorPosition)
  42. Set_FrameCounter_Position(AnchorPosition);
  43. last_AnchorPosition = AnchorPosition;
  44. m_Frames += 1;
  45. float timeNow = Time.realtimeSinceStartup;
  46. if (timeNow > m_LastInterval + UpdateInterval)
  47. {
  48. // display two fractional digits (f2 format)
  49. float fps = m_Frames / (timeNow - m_LastInterval);
  50. float ms = 1000.0f / Mathf.Max(fps, 0.00001f);
  51. if (fps < 30)
  52. htmlColorTag = "<color=yellow>";
  53. else if (fps < 10)
  54. htmlColorTag = "<color=red>";
  55. else
  56. htmlColorTag = "<color=green>";
  57. m_TextMeshPro.SetText(htmlColorTag + fpsLabel, fps, ms);
  58. m_Frames = 0;
  59. m_LastInterval = timeNow;
  60. }
  61. }
  62. void Set_FrameCounter_Position(FpsCounterAnchorPositions anchor_position)
  63. {
  64. switch (anchor_position)
  65. {
  66. case FpsCounterAnchorPositions.TopLeft:
  67. m_TextMeshPro.alignment = TextAlignmentOptions.TopLeft;
  68. m_frameCounter_transform.pivot = new Vector2(0, 1);
  69. m_frameCounter_transform.anchorMin = new Vector2(0.01f, 0.99f);
  70. m_frameCounter_transform.anchorMax = new Vector2(0.01f, 0.99f);
  71. m_frameCounter_transform.anchoredPosition = new Vector2(0, 1);
  72. break;
  73. case FpsCounterAnchorPositions.BottomLeft:
  74. m_TextMeshPro.alignment = TextAlignmentOptions.BottomLeft;
  75. m_frameCounter_transform.pivot = new Vector2(0, 0);
  76. m_frameCounter_transform.anchorMin = new Vector2(0.01f, 0.01f);
  77. m_frameCounter_transform.anchorMax = new Vector2(0.01f, 0.01f);
  78. m_frameCounter_transform.anchoredPosition = new Vector2(0, 0);
  79. break;
  80. case FpsCounterAnchorPositions.TopRight:
  81. m_TextMeshPro.alignment = TextAlignmentOptions.TopRight;
  82. m_frameCounter_transform.pivot = new Vector2(1, 1);
  83. m_frameCounter_transform.anchorMin = new Vector2(0.99f, 0.99f);
  84. m_frameCounter_transform.anchorMax = new Vector2(0.99f, 0.99f);
  85. m_frameCounter_transform.anchoredPosition = new Vector2(1, 1);
  86. break;
  87. case FpsCounterAnchorPositions.BottomRight:
  88. m_TextMeshPro.alignment = TextAlignmentOptions.BottomRight;
  89. m_frameCounter_transform.pivot = new Vector2(1, 0);
  90. m_frameCounter_transform.anchorMin = new Vector2(0.99f, 0.01f);
  91. m_frameCounter_transform.anchorMax = new Vector2(0.99f, 0.01f);
  92. m_frameCounter_transform.anchoredPosition = new Vector2(1, 0);
  93. break;
  94. }
  95. }
  96. }
  97. }