Last active
September 19, 2024 15:54
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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