3 using System.Collections.Generic;
5 namespace HKI.Core.Debuging
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;
32 [SerializeField]
int GraphWidth = 400;
33 [SerializeField]
int GraphHeight = 50;
39 ulong totalFrames = 0;
40 int intervalTotalFrames = 0;
41 int intervalFrameSum = 0;
45 bool minChanged =
false;
47 float minFrametime = 0.0f;
48 bool maxChanged =
false;
50 float maxFrametime = 0.0f;
52 float avgFrametime = 0.0f;
54 float updateInterval = 0.2f;
56 const float msTimeFactor = 1000.0f;
58 Color[] cleanGraph = null;
59 Texture2D graphTexture = null;
60 List<int> fpsGraphBarsVal = null;
62 int graphDataPoints = 200;
64 string lowHexColorString =
"";
65 string mediumHexColorString =
"";
66 string goodHexColorString =
"";
67 string highHexColorString =
"";
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>";
75 const string format =
"n2";
77 StringBuilder sb =
new StringBuilder(80);
80 public Texture2D GraphTexture {
get {
return graphTexture; } }
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); } }
86 public bool MinFPSChanged {
get {
return minChanged; } }
87 public string MinFPSAndFrameTimeString {
get { minChanged =
false;
return FPSAndFrameTimeToString(minFps, minFrametime); } }
89 public bool MaxFPSChanged {
get {
return maxChanged; } }
90 public string MaxFPSAndFrameTimeString {
get { maxChanged =
false;
return FPSAndFrameTimeToString(maxFps, maxFrametime); } }
93 public float SetUpdateInterval {
set { updateInterval = value; } }
98 graphTexture =
new Texture2D(GraphWidth, GraphHeight);
99 graphTexture.wrapMode = TextureWrapMode.Clamp;
100 graphTexture.filterMode = FilterMode.Point;
102 graphTexture.Apply();
104 cleanGraph =
new Color[GraphWidth * GraphHeight];
106 graphDataPoints = GraphWidth / 2;
107 fpsGraphBarsVal =
new List<int>(graphDataPoints);
109 for (
int i = 0; i < graphDataPoints; i++)
110 fpsGraphBarsVal.Add(0);
112 graphTexture =
new Texture2D(GraphWidth, GraphHeight);
114 Color emptyColor =
new Color(0, 0, 0, 0);
116 for (
int i = 0; i < GraphWidth; i++)
118 for (
int j = 0; j < GraphHeight; j++)
119 cleanGraph[i * GraphHeight + j] = emptyColor;
122 graphTexture.SetPixels(cleanGraph);
123 graphTexture.Apply();
125 lowHexColorString = ColorToHEX(LowColor);
126 mediumHexColorString = ColorToHEX(MediumColor);
127 goodHexColorString = ColorToHEX(GoodColor);
128 highHexColorString = ColorToHEX(HighColor);
134 float deltaTime = Time.unscaledDeltaTime;
135 float time = Time.time;
138 intervalTotalFrames++;
139 intervalFrameSum += (int)(1.0f / (deltaTime));
140 if (time > newPeriod)
142 currentFrametime = deltaTime * msTimeFactor;
144 currentFps = intervalFrameSum / intervalTotalFrames;
145 intervalTotalFrames = 0;
146 intervalFrameSum = 0;
148 avgFps = (int)(totalFrames / time);
149 avgFrametime = time / totalFrames * msTimeFactor;
151 if (currentFps < minFps)
154 minFrametime = currentFrametime;
158 if (currentFps > maxFps)
161 maxFrametime = currentFrametime;
165 newPeriod += updateInterval;
180 fpsGraphBarsVal.Add(currentFps);
181 fpsGraphBarsVal.RemoveAt(0);
184 for (
int i = 0; i < fpsGraphBarsVal.Count; i++)
186 if (fpsGraphBarsVal[i] > max)
187 max = fpsGraphBarsVal[i];
190 graphTexture.SetPixels(cleanGraph);
192 for (
int i = 0; i < GraphWidth; i += 2)
194 for (
int j = 0; j < (int)((fpsGraphBarsVal[i / 2] / max) * GraphHeight); j++)
199 graphTexture.SetPixel(i, j, GraphColor);
200 graphTexture.SetPixel(i + 1, j, GraphColor);
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));
207 graphTexture.SetPixel(i, j, GetColor(fpsGraphBarsVal[i / 2]));
208 graphTexture.SetPixel(i + 1, j, GetColor(fpsGraphBarsVal[i / 2]));
214 graphTexture.wrapMode = TextureWrapMode.Clamp;
215 graphTexture.filterMode = GraphStyle ==
GraphStyles.Gradient ? FilterMode.Bilinear : FilterMode.Point;
216 graphTexture.Apply();
222 sb.Append(fpsFTStringPart1);
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);
231 sb.Append(highHexColorString);
233 sb.Append(fpsFTStringPart2a);
235 sb.Append(fps.ToString());
237 sb.Append(fpsFTStringPart3);
239 sb.Append(frametime.ToString(format));
241 sb.Append(fpsFTStringPart4);
243 return sb.ToString();
249 sb.Append(fpsFTStringPart1);
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);
258 sb.Append(highHexColorString);
260 sb.Append(fpsFTStringPart2b);
262 sb.Append(fps.ToString());
264 sb.Append(fpsFTStringPart3);
266 sb.Append(frametime.ToString(format));
268 sb.Append(fpsFTStringPart4);
270 return sb.ToString();
274 return color.r.ToString(
"X2") + color.g.ToString(
"X2") + color.b.ToString(
"X2");
279 if (fps <= LowThreshold)
281 if (fps <= MediumThreshold)
283 if (fps <= GoodThreshold)
string FPSAndFrameTimeToString(int fps, float frametime)
string ColorToHEX(Color32 color)
This class calcs the fps and greates the graph.
string FPSAndFrameTimeToStringSmall(int fps, float frametime)