Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yuri-tikhomirov
Created May 13, 2019 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuri-tikhomirov/95963b9500c573343a08293898181859 to your computer and use it in GitHub Desktop.
Save yuri-tikhomirov/95963b9500c573343a08293898181859 to your computer and use it in GitHub Desktop.
Unity GameObject hierarchy serialization (name & components)
//
// usage :
// Debug.Log( HierarchySerializer.SerializeGameObject(myGameObject) );
//
public static class HierarchySerializer
{
public static System.Text.StringBuilder SerializeGameObject(
UnityEngine.GameObject go,
System.Text.StringBuilder sb = null,
int tab = 0)
{
sb = sb ?? new System.Text.StringBuilder(1024);
var comps = go.GetComponents<UnityEngine.Component>();
sb.Append(' ', tab*2);
sb.Append('$');
sb.AppendLine(go.name);
tab ++;
foreach (var c in comps)
{
if (c is UnityEngine.Transform)
continue;
sb.Append(' ', tab*2);
sb.Append('-');
sb.AppendLine(c.GetType().Name);
}
var tr = go.transform;
for (int i = 0; i < tr.childCount; i++)
{
var ch = tr.GetChild(i);
SerializeGameObject(ch.gameObject, sb, tab);
}
return sb;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment