-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathRuntimeDebugWindow.cs
More file actions
304 lines (244 loc) · 11.1 KB
/
Copy pathRuntimeDebugWindow.cs
File metadata and controls
304 lines (244 loc) · 11.1 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#if ENABLE_UIELEMENTS_MODULE && (UNITY_EDITOR || DEVELOPMENT_BUILD)
#define ENABLE_RENDERING_DEBUGGER_UI
#endif
#if ENABLE_RENDERING_DEBUGGER_UI
using System.Collections.Generic;
using UnityEngine.UIElements;
namespace UnityEngine.Rendering
{
[AddComponentMenu("")] // Hide from Add Component menu
class RuntimeDebugWindow : MonoBehaviour
{
PanelRenderer m_PanelRenderer;
VisualElement m_RootVisualElement;
VisualElement m_PanelRootElement;
TabView m_TabViewElement;
DebugUI.Panel m_SelectedPanel;
bool m_PortraitOrientation;
bool m_IsDirty;
void Awake()
{
DebugManager.instance.onSetDirty -= RequestRecreateGUI;
DebugManager.instance.onSetDirty += RequestRecreateGUI;
if (m_PanelRenderer == null)
{
m_PanelRenderer = gameObject.AddComponent<PanelRenderer>();
if (GraphicsSettings.TryGetRenderPipelineSettings<RenderingDebuggerRuntimeResources>(out var resources))
{
m_PanelRenderer.panelSettings = resources.panelSettings;
m_PanelRenderer.visualTreeAsset = resources.visualTreeAsset;
}
}
m_PanelRenderer.RegisterUIReloadCallback(OnUIReload);
}
internal void OnUIReload(PanelRenderer renderer, VisualElement rootElement)
{
// Called on initial load AND on any asset change
if (rootElement == null || rootElement.childCount == 0)
return;
m_PanelRootElement = rootElement;
m_RootVisualElement = rootElement[0];
var resources = GraphicsSettings.GetRenderPipelineSettings<RenderingDebuggerRuntimeResources>();
var styleSheets = resources.styleSheets;
foreach (var uss in styleSheets)
m_PanelRootElement.styleSheets.Add(uss);
BuildDebugUI();
}
void BuildDebugUI()
{
if (m_RootVisualElement == null)
return;
UpdateOrientation(forceUpdate: true);
m_TabViewElement = m_RootVisualElement.Q<TabView>(name: "debug-window-tabview");
m_TabViewElement.Clear();
var resetButton = m_RootVisualElement.Q<Button>(name: "btn-reset");
resetButton.clicked -= ResetClicked;
resetButton.clicked += ResetClicked;
var panels = DebugManager.instance.panels;
var activePanels = new List<DebugUI.Panel>();
// Filter out editor only panels and panels with no active children
foreach (var panel in panels)
{
bool isEditorOnlyPanel = panel.isEditorOnly;
bool hasVisibleChildren = false;
foreach (var w in panel.children)
{
if (!w.isEditorOnly && !w.isHidden)
{
hasVisibleChildren = true;
break;
}
}
if (!isEditorOnlyPanel && hasVisibleChildren)
{
activePanels.Add(panel);
}
}
bool hasDebugItems = activePanels.Count > 0;
m_RootVisualElement.Q<HelpBox>(name: "no-debug-items-message").style.display = hasDebugItems ? DisplayStyle.None : DisplayStyle.Flex;
m_RootVisualElement.Q<VisualElement>(name: "content-container").style.display = hasDebugItems ? DisplayStyle.Flex : DisplayStyle.None;
if (!hasDebugItems)
return;
var uiPanels = DebugUIExtensions.CreatePanels(activePanels, DebugUI.Context.Runtime);
foreach (var (tabLabel, panel) in uiPanels)
{
ScrollView scrollView = new ScrollView();
scrollView.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
scrollView.verticalScroller.slider.focusable = false;
scrollView.Add(panel);
Tab tab = new Tab(tabLabel.text);
tab.name = tabLabel.name;
tab.selected += t => SetSelectedPanel(t.label);
tab.Add(scrollView);
m_TabViewElement.Add(tab);
}
// We want to treat Up/Down NavigationMoveEvents as Next/Previous instead to get correct focus ring behavior, i.e. make
// up/down arrows behave as tab/shift+tab. To do this we intercept the Up/Down events and send Next/Previous instead.
m_PanelRootElement.UnregisterCallback<NavigationMoveEvent>(ConvertNavigationMoveEvents, TrickleDown.TrickleDown);
m_PanelRootElement.RegisterCallback<NavigationMoveEvent>(ConvertNavigationMoveEvents, TrickleDown.TrickleDown);
string selectedPanelName;
if (m_SelectedPanel == null || !activePanels.Contains(m_SelectedPanel))
selectedPanelName = DebugManager.instance.panels.Count > 0 ? DebugManager.instance.panels[0].displayName : null;
else
selectedPanelName = m_SelectedPanel.displayName;
SetSelectedPanel(selectedPanelName);
}
void OnDestroy()
{
if (m_PanelRenderer != null)
{
// Unregister the UI reload callback
m_PanelRenderer.UnregisterUIReloadCallback(OnUIReload);
// Need to unregister here as well because when the UI is closed and reopened, it is a different object so the member
// function will be a different object and the Unregister call in RecreateGUI does nothing.
m_PanelRootElement?.UnregisterCallback<NavigationMoveEvent>(ConvertNavigationMoveEvents, TrickleDown.TrickleDown);
}
DebugManager.instance.displayRuntimeUI = false;
DebugManager.instance.onSetDirty -= RequestRecreateGUI;
}
void ConvertNavigationMoveEvents(NavigationMoveEvent evt)
{
if (m_PanelRootElement == null)
return;
if (IsPopupOpen())
return; // Popup navigation uses up/down normally
if (evt.direction != NavigationMoveEvent.Direction.Up &&
evt.direction != NavigationMoveEvent.Direction.Down)
return;
evt.StopPropagation();
m_PanelRootElement.focusController.IgnoreEvent(evt);
var newDirection = evt.direction == NavigationMoveEvent.Direction.Down
? NavigationMoveEvent.Direction.Next
: NavigationMoveEvent.Direction.Previous;
using (var newEvt = NavigationMoveEvent.GetPooled(newDirection))
{
newEvt.target = evt.target;
m_PanelRootElement.panel.visualTree.SendEvent(newEvt);
}
}
internal void RequestRecreateGUI()
{
m_IsDirty = true;
}
void Update()
{
UpdateOrientation();
if (m_IsDirty)
{
m_IsDirty = false;
BuildDebugUI();
}
}
void UpdateOrientation(bool forceUpdate = false)
{
// We use screen dimensions instead of Screen.orientation to handle desktop platforms where it's better
// to treat typical screen resolutions as "Landscape", but Screen.orientation reports it as "Portrait".
bool portraitOrientation = Screen.width < Screen.height;
if (forceUpdate || m_PortraitOrientation != portraitOrientation)
{
m_PortraitOrientation = portraitOrientation;
const string portraitClassName = "portrait-orientation";
const string landscapeClassName = "landscape-orientation";
var debugWindowElement = m_PanelRootElement?.Q("debug-window");
if (debugWindowElement == null)
return;
debugWindowElement.RemoveFromClassList(portraitClassName);
debugWindowElement.RemoveFromClassList(landscapeClassName);
if (m_PortraitOrientation)
{
debugWindowElement.AddToClassList(portraitClassName);
}
else
{
debugWindowElement.AddToClassList(landscapeClassName);
}
}
}
void ResetClicked()
{
DebugDisplaySerializer.SaveFoldoutStates();
DebugDisplaySerializer.Clear();
DebugManager.instance.Reset();
DebugDisplaySerializer.LoadFoldoutStates();
}
void SetSelectedPanel(string panelName)
{
if (string.IsNullOrEmpty(panelName))
return;
if (m_SelectedPanel != null)
{
var previousPanel = DebugManager.instance.GetPanel(m_SelectedPanel.displayName);
if (previousPanel != null)
DebugManager.instance.schedulerTracker.SetHierarchyEnabled(DebugUI.Context.Runtime, previousPanel, false);
}
m_SelectedPanel = DebugManager.instance.GetPanel(panelName);
if (m_SelectedPanel != null)
{
var newSelectedTab = m_TabViewElement.Q<Tab>(name: $"{m_SelectedPanel.displayName}_Tab");
if (newSelectedTab != null)
m_TabViewElement.activeTab = newSelectedTab;
DebugManager.instance.schedulerTracker.SetHierarchyEnabled(DebugUI.Context.Runtime, m_SelectedPanel, true);
// Focus first focusable child in the panel
foreach (var widget in m_SelectedPanel.children)
{
if (widget.m_VisualElement is { focusable: true })
{
widget.m_VisualElement.Focus();
break;
}
}
}
}
internal bool IsPopupOpen()
{
if (m_PanelRootElement == null)
return false;
const string popupClassName = "unity-base-dropdown";
var numPanelChildren = m_PanelRootElement.childCount;
// reverse loop because the popup will appear at the end
for (int i = numPanelChildren - 1; i >= 0; i--)
{
var child = m_PanelRootElement[i];
if (child != null && child.ClassListContains(popupClassName))
return true;
}
return false;
}
internal void SelectNextPanel() => SelectPanelWithOffset(+1);
internal void SelectPreviousPanel() => SelectPanelWithOffset(-1);
void SelectPanelWithOffset(int offset)
{
if (m_SelectedPanel == null)
return;
var panels = DebugManager.instance.panels;
int index = DebugManager.instance.FindPanelIndex(m_SelectedPanel.displayName);
int Mod(int x, int m)
{
return (x % m + m) % m; // Handle negative offset correctly
}
int nextIndex = Mod(index + offset, panels.Count);
SetSelectedPanel(panels[nextIndex].displayName);
}
}
}
#endif