-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPSDisplay.cs
More file actions
155 lines (128 loc) · 5.03 KB
/
FPSDisplay.cs
File metadata and controls
155 lines (128 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace balloonCatch
{
[RequireComponent(typeof(Text))]
public class FPSDisplay : MonoBehaviour
{
[SerializeField] private Text label;
[SerializeField] private TMPro.TextMeshProUGUI tmpLabel;
private System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(50);
private float fpsAVG;
private string format = "+000.00;-000.00";
private float gcMemory = 0F;
private float lastGcMemory = 0F;
private float gcMemoryInMB = 0F;
private float gcDiff = 0F;
private float gcDiffAVG = 0F;
private float gcDiffAVGkB = 0F;
private float byteToMB = 0.00000095367431640625F; // / 1024 / 1024;
private float byteTokB = 0.0009765625F; // / 1024 / 1024;
public bool richText = false;
public bool useTextMeshPro = false;
public float curTimeFac;
public bool updateText;
public void ToggleTextUpdate()
{
updateText = !updateText;
label.text = tmpLabel.text = "FPS";
}
// public bool precacheActive = true;
// void Awake(){
// if(precacheActive){
// FontUtils.GetPreCacheRoutine( this, label, ()=>{ precacheActive = false; });
// }
// }
void Update()
{
if (!label.gameObject.activeInHierarchy /*|| precacheActive*/ ) { return; }
// print(1F/Time.deltaTime);
//Total GC Mem
gcMemory = (((float)System.GC.GetTotalMemory(false)));
gcMemoryInMB = gcMemory * byteToMB;
//Total GC Increase per Frame
gcDiff = gcMemory - lastGcMemory;
lastGcMemory = gcMemory;
gcDiffAVG += (gcDiff - gcDiffAVG) * 0.03f;
gcDiffAVGkB = gcDiffAVG * byteTokB;
//frames per Second
fpsAVG += ((Time.unscaledDeltaTime) - fpsAVG) * 0.03f;
if (!useTextMeshPro)
{
if (updateText)
{
if (richText) { label.text = GetRichText(); }
else { label.text = GetText(); }
}
}
else
{
if (updateText)
{
tmpLabel.text = GetText();
}
}
if (selfdestruct) { Destroy(this.gameObject); }
}
private string GetText()
{
stringBuilder.Length = 0;
stringBuilder.Append((1F / fpsAVG).ToString(format)); stringBuilder.Append(" FPS\n");
stringBuilder.Append(gcMemoryInMB.ToString(format)); stringBuilder.Append(" MB Total\n");
stringBuilder.Append(gcDiffAVGkB.ToString(format)); stringBuilder.Append(" kB/frame");
return stringBuilder.ToString();
}
private string GetRichText()
{
return
"<color=#00ffffff>" + (1F / fpsAVG).ToString(format) + "</color> FPS\n" +
gcDiffAVGkB.ToString(format) + " kB Diff\n" +
gcMemoryInMB.ToString(format) + " MB Total";
}
private static bool selfdestruct = false;
public static void DestroyAllFPSLabels()
{
selfdestruct = true;
}
// //Changing a Text every frame causes performance loss, cache per label
//
// // replaced UILabel with Text:
// //https://gist.github.com/lonewolfwilliams/beaec7f85cb739bd770a4026336ee1ee
//
// //adapted from:
// //http://answers.unity3d.com/questions/733570/massive-lag-due-to-fontcachefontfortext-please-hel.html
// public static class FontUtils
// {
//
// public static void GetPreCacheRoutine( MonoBehaviour coroutineTarget, Text label, System.Action callback ){
// coroutineTarget.StartCoroutine(GetPreCacheRoutine(label, callback));
// }
//
// private const string ALL_GLYPHS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_+=~`[]{}|\\:;\"'<>,.?/ ";
//
// private static IEnumerator GetPreCacheRoutine( Text label, System.Action callback )
// {
// return PrecacheFontGlyphs(label.font, label.fontSize, label.fontStyle, ALL_GLYPHS, callback);
// }
// private static IEnumerator PrecacheFontGlyphs(Font theFont, int fontSize, FontStyle style, string glyphs, System.Action callback)
// {
// for (int i=0; i<glyphs.Length; i++)
// {
// theFont.RequestCharactersInTexture(glyphs[i].ToString());
// yield return null;
// }
// yield return new WaitForEndOfFrame();
//
// callback();
// }
// }
//
// //example usage with an NGUI label
// //
// // if(levelLabel != null) StartCoroutine(FontUtils.GetPreCacheRoutine(levelLabel, () => {levelLabel.text = level.ToString();}));
// //
// //
}
}