using UnityEditor; using UnityEngine; /// /// An Editor Script to sort all direct children of a selected GameObject. /// public class SortChildren : ScriptableObject { [MenuItem("Utilities/Sort Children")] static void Execute() { var selected = Selection.transforms; if (selected.Length == 0) { Debug.LogError("No GameObjects were selected!"); return; } if (selected.Length > 1) { Debug.LogError("Only one GameObject at a time can be sorted!"); return; } var obj = selected[0]; var children = obj.childCount; do { var n = 1; for (int i = 0; i < children - 1; i++) { var child1 = obj.GetChild(i); var child2 = obj.GetChild(i + 1); if (child1.name.CompareTo(child2.name) > 0) { child1.SetSiblingIndex(i + 1); child2.SetSiblingIndex(i); n = i + 1; } } children = n; } while (children > 1); } }