Skip to content

Instantly share code, notes, and snippets.

@karljj1
Last active September 19, 2024 15:54
Show Gist options
  • Save karljj1/6adeeebc529321ed693624503bbceba8 to your computer and use it in GitHub Desktop.
Save karljj1/6adeeebc529321ed693624503bbceba8 to your computer and use it in GitHub Desktop.
Print the VisualElement hierarchy with debug info. Use this with a diff tool to see what changed.
public static class DebugHierarchy
{
public static string HierarchyToString(VisualElement root)
{
var sb = new StringBuilder();
HierarchyToStringRecursive(root, sb, 0);
var s = sb.ToString();
EditorGUIUtility.systemCopyBuffer = s;
return s;
}
static void HierarchyToStringRecursive(VisualElement root, StringBuilder sb, int depth)
{
ElementToString(root, sb, depth);
depth++;
for (int i = 0; i < root.hierarchy.childCount; i++)
{
var child = root.hierarchy[i];
HierarchyToStringRecursive(child, sb, depth);
}
}
static void PrintAllStyles(VisualElement element, StringBuilder sb)
{
element.resolvedStyle.GetType().GetProperties().ToList().ForEach(p =>
{
sb.AppendFormat("{0}: {1} ", p.Name, p.GetValue(element.resolvedStyle));
});
}
static void ElementToString(VisualElement element, StringBuilder sb, int depth)
{
for (int i = 0; i < depth; i++)
sb.Append(" ");
sb.Append($"{element.GetType().Name}({element.name}) ");
sb.AppendFormat("{0} ", element.boundingBox);
sb.AppendFormat("{0} ", element.worldBoundingBox);
sb.AppendFormat("{0} ", element.layout);
sb.Append("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment