HKI Core
FPSCalc.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Text;
3 using System.Collections.Generic;
4 
5 namespace HKI.Core.Debuging
6 {
10  [System.Serializable]
11  public class FPSCalc
12  {
13  // Enums
14  [System.Serializable]
15  public enum GraphStyles
16  {
17  Solid,
18  Gradient,
19  FPSBased
20  }
21 
22  // Public variables
23  [SerializeField] Color32 GraphColor = Color.white;
24  [SerializeField] Color32 LowColor = Color.red;
25  [SerializeField] int LowThreshold = 15;
26  [SerializeField] Color32 MediumColor = Color.yellow;
27  [SerializeField] int MediumThreshold = 30;
28  [SerializeField] Color32 GoodColor = Color.green;
29  [SerializeField] int GoodThreshold = 60;
30  [SerializeField] Color32 HighColor = Color.cyan;
31 
32  [SerializeField] int GraphWidth = 400;
33  [SerializeField] int GraphHeight = 50;
34 
35  [SerializeField] GraphStyles GraphStyle = GraphStyles.Solid;
36 
37  // Private variables
38  float newPeriod = 0;
39  ulong totalFrames = 0;
40  int intervalTotalFrames = 0;
41  int intervalFrameSum = 0;
42 
45  bool minChanged = false;
46  int minFps = 999999;
47  float minFrametime = 0.0f;
48  bool maxChanged = false;
49  int maxFps = 0;
50  float maxFrametime = 0.0f;
51  int avgFps = 0;
52  float avgFrametime = 0.0f;
53 
54  float updateInterval = 0.2f;
55 
56  const float msTimeFactor = 1000.0f;
57 
58  Color[] cleanGraph = null;
59  Texture2D graphTexture = null;
60  List<int> fpsGraphBarsVal = null;
61 
62  int graphDataPoints = 200;
63 
64  string lowHexColorString = "";
65  string mediumHexColorString = "";
66  string goodHexColorString = "";
67  string highHexColorString = "";
68 
69  const string fpsFTStringPart1 = "<color=#";
70  const string fpsFTStringPart2a = "><size=40>";
71  const string fpsFTStringPart2b = "><size=25>";
72  const string fpsFTStringPart3 = "</size>\n(";
73  const string fpsFTStringPart4 = " ms)</color>";
74 
75  const string format = "n2";
76 
77  StringBuilder sb = new StringBuilder(80);
78 
79  // Getter
80  public Texture2D GraphTexture { get { return graphTexture; } }
81 
82  public string CurrentFPSAndFrameTimeString { get { return FPSAndFrameTimeToString(currentFps, currentFrametime); } }
83  public string CurrentFPSAndFrameTimeStringSmall { get { return FPSAndFrameTimeToStringSmall(currentFps, currentFrametime); } }
84  public string AvgFPSAndFrameTimeString { get { return FPSAndFrameTimeToString(avgFps, avgFrametime); } }
85 
86  public bool MinFPSChanged { get { return minChanged; } }
87  public string MinFPSAndFrameTimeString { get { minChanged = false; return FPSAndFrameTimeToString(minFps, minFrametime); } }
88 
89  public bool MaxFPSChanged { get { return maxChanged; } }
90  public string MaxFPSAndFrameTimeString { get { maxChanged = false; return FPSAndFrameTimeToString(maxFps, maxFrametime); } }
91 
92  // Setter
93  public float SetUpdateInterval { set { updateInterval = value; } }
94 
95  // Interface functions
96  public void Init()
97  {
98  graphTexture = new Texture2D(GraphWidth, GraphHeight);
99  graphTexture.wrapMode = TextureWrapMode.Clamp;
100  graphTexture.filterMode = FilterMode.Point;
101 
102  graphTexture.Apply();
103 
104  cleanGraph = new Color[GraphWidth * GraphHeight];
105 
106  graphDataPoints = GraphWidth / 2;
107  fpsGraphBarsVal = new List<int>(graphDataPoints);
108 
109  for (int i = 0; i < graphDataPoints; i++)
110  fpsGraphBarsVal.Add(0);
111 
112  graphTexture = new Texture2D(GraphWidth, GraphHeight);
113 
114  Color emptyColor = new Color(0, 0, 0, 0);
115 
116  for (int i = 0; i < GraphWidth; i++)
117  {
118  for (int j = 0; j < GraphHeight; j++)
119  cleanGraph[i * GraphHeight + j] = emptyColor;
120  }
121 
122  graphTexture.SetPixels(cleanGraph);
123  graphTexture.Apply();
124 
125  lowHexColorString = ColorToHEX(LowColor);
126  mediumHexColorString = ColorToHEX(MediumColor);
127  goodHexColorString = ColorToHEX(GoodColor);
128  highHexColorString = ColorToHEX(HighColor);
129 
130  }
131 
132  public void Update()
133  {
134  float deltaTime = Time.unscaledDeltaTime;
135  float time = Time.time;
136 
137  totalFrames++;
138  intervalTotalFrames++;
139  intervalFrameSum += (int)(1.0f / (deltaTime));
140  if (time > newPeriod)
141  {
142  currentFrametime = deltaTime * msTimeFactor;
143 
144  currentFps = intervalFrameSum / intervalTotalFrames;
145  intervalTotalFrames = 0;
146  intervalFrameSum = 0;
147 
148  avgFps = (int)(totalFrames / time);
149  avgFrametime = time / totalFrames * msTimeFactor;
150 
151  if (currentFps < minFps)
152  {
153  minFps = currentFps;
154  minFrametime = currentFrametime;
155  minChanged = true;
156  }
157 
158  if (currentFps > maxFps)
159  {
160  maxFps = currentFps;
161  maxFrametime = currentFrametime;
162  maxChanged = true;
163  }
164 
165  newPeriod += updateInterval;
166 
167  UpdateGraph();
168  }
169  }
170 
171  public void ResetMinAndMaxFPS()
172  {
173  minFps = 999999;
174  maxFps = 0;
175  }
176 
177  // Helper functions
178  void UpdateGraph()
179  {
180  fpsGraphBarsVal.Add(currentFps);
181  fpsGraphBarsVal.RemoveAt(0);
182 
183  float max = 100;
184  for (int i = 0; i < fpsGraphBarsVal.Count; i++)
185  {
186  if (fpsGraphBarsVal[i] > max)
187  max = fpsGraphBarsVal[i];
188  }
189 
190  graphTexture.SetPixels(cleanGraph);
191 
192  for (int i = 0; i < GraphWidth; i += 2)
193  {
194  for (int j = 0; j < (int)((fpsGraphBarsVal[i / 2] / max) * GraphHeight); j++)
195  {
196  switch (GraphStyle)
197  {
198  case GraphStyles.Solid:
199  graphTexture.SetPixel(i, j, GraphColor);
200  graphTexture.SetPixel(i + 1, j, GraphColor);
201  break;
202  case GraphStyles.Gradient:
203  graphTexture.SetPixel(i, j, new Color(1 - (0.02f * j), 0.02f * j, 0));
204  graphTexture.SetPixel(i + 1, j, new Color(1 - (0.02f * j), 0.02f * j, 0));
205  break;
206  case GraphStyles.FPSBased:
207  graphTexture.SetPixel(i, j, GetColor(fpsGraphBarsVal[i / 2]));
208  graphTexture.SetPixel(i + 1, j, GetColor(fpsGraphBarsVal[i / 2]));
209  break;
210  }
211  }
212  }
213 
214  graphTexture.wrapMode = TextureWrapMode.Clamp;
215  graphTexture.filterMode = GraphStyle == GraphStyles.Gradient ? FilterMode.Bilinear : FilterMode.Point;
216  graphTexture.Apply();
217  }
218 
219  string FPSAndFrameTimeToString(int fps, float frametime)
220  {
221  sb.Clear();
222  sb.Append(fpsFTStringPart1);
223 
224  if (fps <= LowThreshold)
225  sb.Append(lowHexColorString);
226  else if (fps <= MediumThreshold)
227  sb.Append(mediumHexColorString);
228  else if (fps <= GoodThreshold)
229  sb.Append(goodHexColorString);
230  else
231  sb.Append(highHexColorString);
232 
233  sb.Append(fpsFTStringPart2a);
234 
235  sb.Append(fps.ToString());
236 
237  sb.Append(fpsFTStringPart3);
238 
239  sb.Append(frametime.ToString(format));
240 
241  sb.Append(fpsFTStringPart4);
242 
243  return sb.ToString();
244  }
245 
246  string FPSAndFrameTimeToStringSmall(int fps, float frametime)
247  {
248  sb.Clear();
249  sb.Append(fpsFTStringPart1);
250 
251  if (fps <= LowThreshold)
252  sb.Append(lowHexColorString);
253  else if (fps <= MediumThreshold)
254  sb.Append(mediumHexColorString);
255  else if (fps <= GoodThreshold)
256  sb.Append(goodHexColorString);
257  else
258  sb.Append(highHexColorString);
259 
260  sb.Append(fpsFTStringPart2b);
261 
262  sb.Append(fps.ToString());
263 
264  sb.Append(fpsFTStringPart3);
265 
266  sb.Append(frametime.ToString(format));
267 
268  sb.Append(fpsFTStringPart4);
269 
270  return sb.ToString();
271  }
272  string ColorToHEX(Color32 color)
273  {
274  return color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2");
275  }
276 
277  Color GetColor(int fps)
278  {
279  if (fps <= LowThreshold)
280  return LowColor;
281  if (fps <= MediumThreshold)
282  return MediumColor;
283  if (fps <= GoodThreshold)
284  return GoodColor;
285 
286  return HighColor;
287  }
288  }
289 }
string FPSAndFrameTimeToString(int fps, float frametime)
Definition: FPSCalc.cs:219
string ColorToHEX(Color32 color)
Definition: FPSCalc.cs:272
This class calcs the fps and greates the graph.
Definition: FPSCalc.cs:11
string FPSAndFrameTimeToStringSmall(int fps, float frametime)
Definition: FPSCalc.cs:246
Color GetColor(int fps)
Definition: FPSCalc.cs:277