FpsMonitor.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace OpenCVForUnityExample
  5. {
  6. public class FpsMonitor : MonoBehaviour
  7. {
  8. int tick = 0;
  9. float elapsed = 0;
  10. float fps = 0;
  11. public enum Alignment
  12. {
  13. LeftTop,
  14. RightTop,
  15. LeftBottom,
  16. RightBottom,
  17. }
  18. public Alignment alignment = Alignment.RightTop;
  19. const float GUI_WIDTH = 75f;
  20. const float GUI_HEIGHT = 30f;
  21. const float MARGIN_X = 10f;
  22. const float MARGIN_Y = 10f;
  23. const float INNER_X = 8f;
  24. const float INNER_Y = 5f;
  25. const float GUI_CONSOLE_HEIGHT = 50f;
  26. public Vector2 offset = new Vector2 (MARGIN_X, MARGIN_Y);
  27. public bool boxVisible = true;
  28. public float boxWidth = GUI_WIDTH;
  29. public float boxHeight = GUI_HEIGHT;
  30. public Vector2 padding = new Vector2 (INNER_X, INNER_Y);
  31. public float consoleHeight = GUI_CONSOLE_HEIGHT;
  32. GUIStyle console_labelStyle;
  33. float x, y;
  34. Rect outer;
  35. Rect inner;
  36. float console_x, console_y;
  37. Rect console_outer;
  38. Rect console_inner;
  39. int oldScrWidth;
  40. int oldScrHeight;
  41. Dictionary<string, string> outputDict = new Dictionary<string, string> ();
  42. public string consoleText;
  43. // Use this for initialization
  44. void Start ()
  45. {
  46. console_labelStyle = new GUIStyle ();
  47. console_labelStyle.fontSize = 32;
  48. console_labelStyle.fontStyle = FontStyle.Normal;
  49. console_labelStyle.wordWrap = true;
  50. console_labelStyle.normal.textColor = Color.white;
  51. oldScrWidth = Screen.width;
  52. oldScrHeight = Screen.height;
  53. LocateGUI ();
  54. }
  55. // Update is called once per frame
  56. void Update ()
  57. {
  58. tick++;
  59. elapsed += Time.deltaTime;
  60. if (elapsed >= 1f) {
  61. fps = tick / elapsed;
  62. tick = 0;
  63. elapsed = 0;
  64. }
  65. }
  66. void OnGUI ()
  67. {
  68. if (oldScrWidth != Screen.width || oldScrHeight != Screen.height) {
  69. LocateGUI ();
  70. }
  71. oldScrWidth = Screen.width;
  72. oldScrHeight = Screen.height;
  73. if (boxVisible) {
  74. GUI.Box (outer, "");
  75. }
  76. GUILayout.BeginArea (inner);
  77. {
  78. GUILayout.BeginVertical ();
  79. GUILayout.Label ("fps : " + fps.ToString ("F1"));
  80. foreach (KeyValuePair<string, string> pair in outputDict) {
  81. GUILayout.Label (pair.Key + " : " + pair.Value);
  82. }
  83. GUILayout.EndVertical ();
  84. }
  85. GUILayout.EndArea ();
  86. if (!string.IsNullOrEmpty (consoleText)) {
  87. if (boxVisible) {
  88. GUI.Box (console_outer, "");
  89. }
  90. GUILayout.BeginArea (console_inner);
  91. {
  92. GUILayout.BeginVertical ();
  93. GUILayout.Label (consoleText, console_labelStyle);
  94. GUILayout.EndVertical ();
  95. }
  96. GUILayout.EndArea ();
  97. }
  98. }
  99. public void Add (string key, string value)
  100. {
  101. if (outputDict.ContainsKey (key)) {
  102. outputDict [key] = value;
  103. } else {
  104. outputDict.Add (key, value);
  105. }
  106. }
  107. public void Remove (string key)
  108. {
  109. outputDict.Remove (key);
  110. }
  111. public void Clear ()
  112. {
  113. outputDict.Clear ();
  114. }
  115. public void LocateGUI ()
  116. {
  117. x = GetAlignedX (alignment, boxWidth);
  118. y = GetAlignedY (alignment, boxHeight);
  119. outer = new Rect (x, y, boxWidth, boxHeight);
  120. inner = new Rect (x + padding.x, y + padding.y, boxWidth, boxHeight);
  121. console_x = GetAlignedX (Alignment.LeftBottom, Screen.width);
  122. console_y = GetAlignedY (Alignment.LeftBottom, consoleHeight);
  123. console_outer = new Rect (console_x, console_y, Screen.width - offset.x * 2, consoleHeight);
  124. console_inner = new Rect (console_x + padding.x, console_y + padding.y, Screen.width - offset.x * 2 - padding.x, consoleHeight);
  125. }
  126. float GetAlignedX (Alignment anchor, float w)
  127. {
  128. switch (anchor) {
  129. default:
  130. case Alignment.LeftTop:
  131. case Alignment.LeftBottom:
  132. return offset.x;
  133. case Alignment.RightTop:
  134. case Alignment.RightBottom:
  135. return Screen.width - w - offset.x;
  136. }
  137. }
  138. float GetAlignedY (Alignment anchor, float h)
  139. {
  140. switch (anchor) {
  141. default:
  142. case Alignment.LeftTop:
  143. case Alignment.RightTop:
  144. return offset.y;
  145. case Alignment.LeftBottom:
  146. case Alignment.RightBottom:
  147. return Screen.height - h - offset.y;
  148. }
  149. }
  150. }
  151. }