Created
May 13, 2019 13:46
-
-
Save yuri-tikhomirov/95963b9500c573343a08293898181859 to your computer and use it in GitHub Desktop.
Unity GameObject hierarchy serialization (name & components)
This file contains 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
// | |
// 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