-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathAssetDependencyGraph.cs
366 lines (302 loc) · 11.2 KB
/
AssetDependencyGraph.cs
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using System.Collections.Generic;
using UnityEditor;
#if UNITY_2019_1_OR_NEWER
using UnityEditor.Experimental.GraphView;
using UnityEngine.UIElements;
#else
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleEnums;
#endif
using UnityEngine;
public class AssetGraphView : GraphView
{
public AssetGraphView()
{
SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
this.AddManipulator(new ContentDragger());
this.AddManipulator(new SelectionDragger());
this.AddManipulator(new RectangleSelector());
this.AddManipulator(new FreehandSelector());
VisualElement background = new VisualElement
{
style =
{
backgroundColor = new Color(0.17f, 0.17f, 0.17f, 1f)
}
};
Insert(0, background);
background.StretchToParentSize();
}
}
public class AssetDependencyGraph : EditorWindow
{
private const float kNodeWidth = 250.0f;
private GraphView m_GraphView;
private readonly List<GraphElement> m_AssetElements = new List<GraphElement>();
private readonly Dictionary<string, Node> m_GUIDNodeLookup = new Dictionary<string, Node>();
private readonly List<Node> m_DependenciesForPlacement = new List<Node>();
#if !UNITY_2019_1_OR_NEWER
private VisualElement rootVisualElement;
#endif
[MenuItem("Window/Analysis/Asset Dependency Graph")]
public static void CreateTestGraphViewWindow()
{
var window = GetWindow<AssetDependencyGraph>();
window.titleContent = new GUIContent("Asset Dependency Graph");
}
public void OnEnable()
{
m_GraphView = new AssetGraphView
{
name = "Asset Dependency Graph",
};
var toolbar = new VisualElement
{
style =
{
flexDirection = FlexDirection.Row,
flexGrow = 0,
backgroundColor = new Color(0.25f, 0.25f, 0.25f, 0.75f)
}
};
var options = new VisualElement
{
style = { alignContent = Align.Center }
};
toolbar.Add(options);
toolbar.Add(new Button(ExplodeAsset)
{
text = "Explore Asset",
});
toolbar.Add(new Button(ClearGraph)
{
text = "Clear"
});
#if !UNITY_2019_1_OR_NEWER
rootVisualElement = this.GetRootVisualContainer();
#endif
rootVisualElement.Add(toolbar);
rootVisualElement.Add(m_GraphView);
m_GraphView.StretchToParentSize();
toolbar.BringToFront();
}
public void OnDisable()
{
rootVisualElement.Remove(m_GraphView);
}
private void ExplodeAsset()
{
Object obj = Selection.activeObject;
if (!obj)
return;
string assetPath = AssetDatabase.GetAssetPath(obj);
Group groupNode = new Group {title = obj.name};
Object mainObject = AssetDatabase.LoadMainAssetAtPath(assetPath);
string[] dependencies = AssetDatabase.GetDependencies(assetPath, false);
bool hasDependencies = dependencies.Length > 0;
Node mainNode = CreateNode(mainObject, assetPath, true, hasDependencies);
mainNode.SetPosition(new Rect(0, 0, 0, 0));
m_GraphView.AddElement(groupNode);
m_GraphView.AddElement(mainNode);
groupNode.AddElement(mainNode);
CreateDependencyNodes(dependencies, mainNode, groupNode, 1);
m_AssetElements.Add(mainNode);
m_AssetElements.Add(groupNode);
groupNode.capabilities &= ~Capabilities.Deletable;
groupNode.Focus();
mainNode.RegisterCallback<GeometryChangedEvent>(UpdateDependencyNodePlacement);
}
private void CreateDependencyNodes(string[] dependencies, Node parentNode, Group groupNode, int depth)
{
foreach (string dependencyString in dependencies)
{
Object dependencyAsset = AssetDatabase.LoadMainAssetAtPath(dependencyString);
string[] deeperDependencies = AssetDatabase.GetDependencies(dependencyString, false);
Node dependencyNode = CreateNode(dependencyAsset, AssetDatabase.GetAssetPath(dependencyAsset),
false, deeperDependencies.Length > 0);
if (!m_AssetElements.Contains(dependencyNode))
dependencyNode.userData = depth;
CreateDependencyNodes(deeperDependencies, dependencyNode, groupNode, depth + 1);
if (!m_GraphView.Contains(dependencyNode))
m_GraphView.AddElement(dependencyNode);
Edge edge = new Edge
{
input = dependencyNode.inputContainer[0] as Port,
output = parentNode.outputContainer[0] as Port,
};
edge.input?.Connect(edge);
edge.output?.Connect(edge);
dependencyNode.RefreshPorts();
m_GraphView.AddElement(edge);
if (!m_AssetElements.Contains(dependencyNode))
groupNode.AddElement(dependencyNode);
edge.capabilities &= ~Capabilities.Deletable;
m_AssetElements.Add(edge);
m_AssetElements.Add(dependencyNode);
if (!m_DependenciesForPlacement.Contains(dependencyNode))
m_DependenciesForPlacement.Add(dependencyNode);
}
}
private Node CreateNode(Object obj, string assetPath, bool isMainNode, bool hasDependencies)
{
Node resultNode;
string assetGUID = AssetDatabase.AssetPathToGUID(assetPath);
if (m_GUIDNodeLookup.TryGetValue(assetGUID, out resultNode))
{
int currentDepth = (int)resultNode.userData;
resultNode.userData = currentDepth + 1;
return resultNode;
}
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out var assetGuid, out long _))
{
var objNode = new Node
{
title = obj.name,
style =
{
width = kNodeWidth
}
};
objNode.extensionContainer.style.backgroundColor = new Color(0.24f, 0.24f, 0.24f, 0.8f);
objNode.titleContainer.Add(new Button(() =>
{
Selection.activeObject = obj;
EditorGUIUtility.PingObject(obj);
})
{
style =
{
height = 16.0f,
alignSelf = Align.Center,
alignItems = Align.Center
},
text = "Select"
});
var infoContainer = new VisualElement
{
style =
{
paddingBottom = 4.0f,
paddingTop = 4.0f,
paddingLeft = 4.0f,
paddingRight = 4.0f
}
};
infoContainer.Add(new Label
{
text = assetPath,
#if UNITY_2019_1_OR_NEWER
style = { whiteSpace = WhiteSpace.Normal }
#else
style = { wordWrap = true }
#endif
});
var typeName = obj.GetType().Name;
if (isMainNode)
{
var prefabType = PrefabUtility.GetPrefabAssetType(obj);
if (prefabType != PrefabAssetType.NotAPrefab)
typeName = $"{prefabType} Prefab";
}
var typeLabel = new Label
{
text = $"Type: {typeName}"
};
infoContainer.Add(typeLabel);
objNode.extensionContainer.Add(infoContainer);
Texture assetTexture = AssetPreview.GetAssetPreview(obj);
if (!assetTexture)
assetTexture = AssetPreview.GetMiniThumbnail(obj);
if (assetTexture)
{
AddDivider(objNode);
objNode.extensionContainer.Add(new Image
{
image = assetTexture,
scaleMode = ScaleMode.ScaleToFit,
style =
{
paddingBottom = 4.0f,
paddingTop = 4.0f,
paddingLeft = 4.0f,
paddingRight = 4.0f
}
});
}
// Ports
if (!isMainNode)
{
Port realPort = objNode.InstantiatePort(Orientation.Horizontal, Direction.Input, Port.Capacity.Single, typeof(Object));
realPort.portName = "Dependent";
objNode.inputContainer.Add(realPort);
}
if (hasDependencies)
{
#if UNITY_2018_1
Port port = objNode.InstantiatePort(Orientation.Horizontal, Direction.Output, typeof(Object));
#else
Port port = objNode.InstantiatePort(Orientation.Horizontal, Direction.Output, Port.Capacity.Single, typeof(Object));
#endif
port.portName = "Dependencies";
objNode.outputContainer.Add(port);
objNode.RefreshPorts();
}
resultNode = objNode;
resultNode.RefreshExpandedState();
resultNode.RefreshPorts();
resultNode.capabilities &= ~Capabilities.Deletable;
resultNode.capabilities |= Capabilities.Collapsible;
}
m_GUIDNodeLookup[assetGUID] = resultNode;
return resultNode;
}
private static void AddDivider(Node objNode)
{
var divider = new VisualElement {name = "divider"};
divider.AddToClassList("horizontal");
objNode.extensionContainer.Add(divider);
}
private void ClearGraph()
{
foreach (var edge in m_AssetElements)
{
m_GraphView.RemoveElement(edge);
}
m_AssetElements.Clear();
foreach (var node in m_AssetElements)
{
m_GraphView.RemoveElement(node);
}
m_AssetElements.Clear();
m_GUIDNodeLookup.Clear();
}
private void UpdateDependencyNodePlacement(GeometryChangedEvent e)
{
// The current y offset in per depth
var depthYOffset = new Dictionary<int, float>();
foreach (var node in m_DependenciesForPlacement)
{
int depth = (int)node.userData;
if (!depthYOffset.ContainsKey(depth))
depthYOffset.Add(depth, 0.0f);
depthYOffset[depth] += node.layout.height;
}
// Move half of the node into negative y space so they're on either size of the main node in y axis
var depths = new List<int>(depthYOffset.Keys);
foreach (int depth in depths)
{
if (depth == 0)
continue;
float offset = depthYOffset[depth];
depthYOffset[depth] = (0f - offset / 2.0f);
}
foreach (var node in m_DependenciesForPlacement)
{
int depth = (int)node.userData;
node.SetPosition(new Rect(kNodeWidth * 1.5f * depth, depthYOffset[depth], 0, 0));
depthYOffset[depth] += node.layout.height;
}
m_DependenciesForPlacement.Clear();
}
}