-
-
Save AShim3D/d76e2026c5655b3b34e2 to your computer and use it in GitHub Desktop.
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class TempTools { | |
[MenuItem ("AShim/SortChildrenByName")] | |
public static void SortChildrenByName() { | |
foreach (GameObject obj in Selection.gameObjects) { | |
List<Transform> children = new List<Transform>(); | |
for (int i = obj.transform.childCount - 1; i >= 0; i--) { | |
Transform child = obj.transform.GetChild(i); | |
children.Add(child); | |
child.parent = null; | |
} | |
children.Sort((Transform t1, Transform t2) => { return t1.name.CompareTo(t2.name); }); | |
foreach (Transform child in children) { | |
child.parent = obj.transform; | |
} | |
} | |
} // SortChildrenByName() | |
} // class TempTools |
You can also use Transform.SetSiblingIndex. In this example I also added the posibility to undo the sort action. Here is the modified code:
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
public class TempTools
{
[MenuItem("AShim/SortChildrenByName")]
public static void SortChildrenByName()
{
foreach (Transform t in Selection.transforms)
{
List<Transform> children = t.Cast<Transform>().ToList();
children.Sort((Transform t1, Transform t2) => { return t1.name.CompareTo(t2.name); });
for (int i = 0; i < children.Count; ++i)
{
Undo.SetTransformParent(children[i], children[i].parent, "Sort Children");
children[i].SetSiblingIndex(i);
}
}
} // SortChildrenByName()
} // class TempTools
Thanks!
Don't work inside prefabs with the current system of unity, so bad
This works with prefabs just put the method in the script of the container then call the method from outside or from updates.
public void SortChildrenByNameTag()
{
foreach (GameObject obj in Selection.gameObjects)
{
List children = new List();
for (int i = obj.transform.childCount - 1; i >= 0; i--)
{
Transform child = obj.transform.GetChild(i);
PrefabScript entryPart = obj.transform.GetChild(i).GetComponent() as PrefabScript;
string curname = entryPart.GetName(); // have a method on the prefab script that returns the name
child.name = curname;
//Debug.Log(curname);
children.Add(child);
child.transform.SetParent(null, false);
//child.parent = null;
}
children.Sort((Transform t1, Transform t2) => { return t1.name.CompareTo(t2.name); });
foreach (Transform child in children)
{
//child.parent = obj.transform;
child.transform.SetParent(obj.transform, false);
}
}
}
The script on the prefab
string curname;
//call this when instantiating to set the instances name from a databank
public void SetCurName(string newname)
{
curname = newname;
public string GetName()
{
return curname;
}
Works great. Thank you!
For prefabs, need to use this inside the prefab editor.
Method to sort all the children inside another children
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
public static class SortChildren
{
[MenuItem("Setup/Sort All Children")]
public static void SortAllChildren()
{
foreach (Transform t in Selection.transforms)
{
SortChildrenRecursively(t);
}
}
public static void SortChildrenRecursively(Transform parent)
{
List<Transform> children = parent.Cast<Transform>().ToList();
children.Sort((Transform t1, Transform t2) => { return t1.name.CompareTo(t2.name); });
for (int i = 0; i < children.Count; ++i)
{
Undo.SetTransformParent(children[i], children[i].parent, "Sort Children");
children[i].SetSiblingIndex(i);
}
foreach (Transform child in parent)
{
SortChildrenRecursively(child);
}
}
}
Don't work inside prefabs with the current system of unity, so bad