TMP_FrameRateCounter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class TMP_FrameRateCounter : 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 TextMeshPro m_TextMeshPro;
  15. private Transform m_frameCounter_transform;
  16. private Camera m_camera;
  17. private FpsCounterAnchorPositions last_AnchorPosition;
  18. void Awake()
  19. {
  20. if (!enabled)
  21. return;
  22. m_camera = Camera.main;
  23. Application.targetFrameRate = 9999;
  24. GameObject frameCounter = new GameObject("Frame Counter");
  25. m_TextMeshPro = frameCounter.AddComponent<TextMeshPro>();
  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_frameCounter_transform = frameCounter.transform;
  29. m_frameCounter_transform.SetParent(m_camera.transform);
  30. m_frameCounter_transform.localRotation = Quaternion.identity;
  31. m_TextMeshPro.enableWordWrapping = false;
  32. m_TextMeshPro.fontSize = 24;
  33. //m_TextMeshPro.FontColor = new Color32(255, 255, 255, 128);
  34. //m_TextMeshPro.edgeWidth = .15f;
  35. //m_TextMeshPro.isOverlay = true;
  36. //m_TextMeshPro.FaceColor = new Color32(255, 128, 0, 0);
  37. //m_TextMeshPro.EdgeColor = new Color32(0, 255, 0, 255);
  38. //m_TextMeshPro.FontMaterial.renderQueue = 4000;
  39. //m_TextMeshPro.CreateSoftShadowClone(new Vector2(1f, -1f));
  40. Set_FrameCounter_Position(AnchorPosition);
  41. last_AnchorPosition = AnchorPosition;
  42. }
  43. void Start()
  44. {
  45. m_LastInterval = Time.realtimeSinceStartup;
  46. m_Frames = 0;
  47. }
  48. void Update()
  49. {
  50. if (AnchorPosition != last_AnchorPosition)
  51. Set_FrameCounter_Position(AnchorPosition);
  52. last_AnchorPosition = AnchorPosition;
  53. m_Frames += 1;
  54. float timeNow = Time.realtimeSinceStartup;
  55. if (timeNow > m_LastInterval + UpdateInterval)
  56. {
  57. // display two fractional digits (f2 format)
  58. float fps = m_Frames / (timeNow - m_LastInterval);
  59. float ms = 1000.0f / Mathf.Max(fps, 0.00001f);
  60. if (fps < 30)
  61. htmlColorTag = "<color=yellow>";
  62. else if (fps < 10)
  63. htmlColorTag = "<color=red>";
  64. else
  65. htmlColorTag = "<color=green>";
  66. //string format = System.String.Format(htmlColorTag + "{0:F2} </color>FPS \n{1:F2} <#8080ff>MS",fps, ms);
  67. //m_TextMeshPro.text = format;
  68. m_TextMeshPro.SetText(htmlColorTag + fpsLabel, fps, ms);
  69. m_Frames = 0;
  70. m_LastInterval = timeNow;
  71. }
  72. }
  73. void Set_FrameCounter_Position(FpsCounterAnchorPositions anchor_position)
  74. {
  75. //Debug.Log("Changing frame counter anchor position.");
  76. m_TextMeshPro.margin = new Vector4(1f, 1f, 1f, 1f);
  77. switch (anchor_position)
  78. {
  79. case FpsCounterAnchorPositions.TopLeft:
  80. m_TextMeshPro.alignment = TextAlignmentOptions.TopLeft;
  81. m_TextMeshPro.rectTransform.pivot = new Vector2(0, 1);
  82. m_frameCounter_transform.position = m_camera.ViewportToWorldPoint(new Vector3(0, 1, 100.0f));
  83. break;
  84. case FpsCounterAnchorPositions.BottomLeft:
  85. m_TextMeshPro.alignment = TextAlignmentOptions.BottomLeft;
  86. m_TextMeshPro.rectTransform.pivot = new Vector2(0, 0);
  87. m_frameCounter_transform.position = m_camera.ViewportToWorldPoint(new Vector3(0, 0, 100.0f));
  88. break;
  89. case FpsCounterAnchorPositions.TopRight:
  90. m_TextMeshPro.alignment = TextAlignmentOptions.TopRight;
  91. m_TextMeshPro.rectTransform.pivot = new Vector2(1, 1);
  92. m_frameCounter_transform.position = m_camera.ViewportToWorldPoint(new Vector3(1, 1, 100.0f));
  93. break;
  94. case FpsCounterAnchorPositions.BottomRight:
  95. m_TextMeshPro.alignment = TextAlignmentOptions.BottomRight;
  96. m_TextMeshPro.rectTransform.pivot = new Vector2(1, 0);
  97. m_frameCounter_transform.position = m_camera.ViewportToWorldPoint(new Vector3(1, 0, 100.0f));
  98. break;
  99. }
  100. }
  101. }
  102. }