-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugOverlay.cs
More file actions
239 lines (194 loc) · 7.16 KB
/
DebugOverlay.cs
File metadata and controls
239 lines (194 loc) · 7.16 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using TMPro;
using System;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
public class DebugOverlay : MonoBehaviour
{
[Header("Settings")]
[SerializeField] private float updateInterval = 0.5f;
[SerializeField][Range(0f, 1f)] private float backgroundOpacity = 0.2f;
[SerializeField] private Color textColor = Color.green;
[SerializeField] private int fontSize = 25;
[SerializeField] private Vector2 padding = new(10, 10);
[SerializeField] private Vector2 resolution = new(1920, 1080);
[SerializeField] private int panelWidth = 400;
[Header("Toggle Settings")]
[SerializeField] private bool isVisible = true;
[SerializeField] private bool showFPS = true;
[SerializeField] private bool showPerformance = true;
private Canvas debugCanvas;
private Image backgroundPanel;
private TextMeshProUGUI debugText;
private System.Text.StringBuilder stringBuilder = new(256);
private readonly Dictionary<string, string> debugValues = new();
private readonly object lockObject = new object(); // Lock for thread safety
// FPS calculation variables
private float fpsUpdateTimer;
private int frameCount;
private float fps;
// Performance tracking variables
private float performanceUpdateTimer;
private float frameTime;
private long memoryUsage;
private int gcCollectionCount;
// NEW INPUT SYSTEM
#if ENABLE_INPUT_SYSTEM
[SerializeField] private Key toggleKey = Key.Backquote;
#else
[SerializeField] private KeyCode toggleKey = KeyCode.BackQuote;
#endif
private static DebugOverlay _instance;
public static DebugOverlay Instance
{
get
{
if (_instance == null)
{
// Try to find an existing instance
_instance = FindFirstObjectByType<DebugOverlay>();
// If no instance exists, create one
if (_instance == null)
{
GameObject debuggerObj = new("VisualDebugger");
_instance = debuggerObj.AddComponent<DebugOverlay>();
DontDestroyOnLoad(debuggerObj);
}
}
return _instance;
}
}
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
if (debugCanvas == null)
{
InitializeUI();
}
}
private void InitializeUI()
{
GameObject canvasObj = new("DebugCanvas"); // Canvas
canvasObj.transform.SetParent(transform);
debugCanvas = canvasObj.AddComponent<Canvas>();
debugCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
debugCanvas.sortingOrder = 1000; // Ensure it's on top of everything
CanvasScaler scaler = canvasObj.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = resolution;
GameObject panelObj = new("DebugPanel"); // Background panel
panelObj.transform.SetParent(canvasObj.transform, false);
backgroundPanel = panelObj.AddComponent<Image>();
backgroundPanel.color = new Color(0, 0, 0, backgroundOpacity);
RectTransform panelRect = panelObj.GetComponent<RectTransform>();
panelRect.anchorMin = new(0, 1);
panelRect.anchorMax = new(0, 1);
panelRect.pivot = new(0, 1);
panelRect.anchoredPosition = new(padding.x, -padding.y);
panelRect.sizeDelta = new(400, 200);
GameObject textObj = new("DebugText"); // Debug text
textObj.transform.SetParent(panelObj.transform, false);
debugText = textObj.AddComponent<TextMeshProUGUI>();
debugText.color = textColor;
debugText.fontSize = fontSize;
debugText.alignment = TextAlignmentOptions.TopLeft;
debugText.textWrappingMode = TextWrappingModes.Normal;
RectTransform textRect = textObj.GetComponent<RectTransform>();
textRect.anchorMin = Vector2.zero;
textRect.anchorMax = Vector2.one;
textRect.pivot = new(0.5f, 0.5f);
textRect.anchoredPosition = Vector2.zero;
textRect.sizeDelta = new(-15, -15);
if (showFPS)
SetDebugValue("FPS", "0");
if(showPerformance)
{
SetDebugValue("Frame Time", "0.0ms");
SetDebugValue("Memory", "0MB");
SetDebugValue("GC Collections", "0");
}
}
private void Update()
{
#if ENABLE_INPUT_SYSTEM
if (Keyboard.current != null && Keyboard.current[toggleKey].wasPressedThisFrame)
ToggleVisibility();
#else
if (Input.GetKeyDown(toggleKey))
{
ToggleVisibility();
}
#endif
if(showFPS)
CalculateAndUpdateFPS();
if(showPerformance)
CalculateAndUpdatePerformance();
UpdateDebugDisplay();
}
private void ToggleVisibility()
{
isVisible = !isVisible;
if (debugCanvas != null)
debugCanvas.gameObject.SetActive(isVisible);
}
private void CalculateAndUpdateFPS()
{
frameCount++;
fpsUpdateTimer += Time.unscaledDeltaTime;
if (fpsUpdateTimer >= updateInterval)
{
fps = frameCount / fpsUpdateTimer;
frameCount = 0;
fpsUpdateTimer = 0;
SetDebugValue("FPS", ((int)fps).ToString());
}
}
private void CalculateAndUpdatePerformance()
{
performanceUpdateTimer += Time.unscaledDeltaTime;
frameTime = Time.unscaledDeltaTime * 1000f;
if (performanceUpdateTimer >= updateInterval)
{
memoryUsage = GC.GetTotalMemory(false) / 1024 / 1024;
gcCollectionCount = GC.CollectionCount(0) + GC.CollectionCount(1) + GC.CollectionCount(2);
performanceUpdateTimer = 0;
SetDebugValue("Frame Time", frameTime.ToString("F1") + "ms");
SetDebugValue("Memory", memoryUsage.ToString() + "MB");
SetDebugValue("GC Collections", gcCollectionCount.ToString());
}
}
private void UpdateDebugDisplay()
{
if (debugText == null) return;
stringBuilder.Clear();
foreach (KeyValuePair<string, string> pair in debugValues)
{
stringBuilder.AppendLine($"{pair.Key}: {pair.Value}");
}
debugText.text = stringBuilder.ToString();
RectTransform panelRect = backgroundPanel.GetComponent<RectTransform>();
float height = Mathf.Min(debugText.preferredHeight + 20, Screen.height * 0.7f);
panelRect.sizeDelta = new(panelWidth, height);
}
public static void SetDebugValue(string key, object value)
{
Instance.SetDebugValueInternal(key, value);
}
private void SetDebugValueInternal(string key, object value)
{
lock (lockObject)
{
string valueText = value?.ToString() ?? "null";
debugValues[key] = valueText;
}
}
}