Skip to content

Instantly share code, notes, and snippets.

@AShim3D
Last active February 23, 2024 10:11
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AShim3D/d76e2026c5655b3b34e2 to your computer and use it in GitHub Desktop.
Save AShim3D/d76e2026c5655b3b34e2 to your computer and use it in GitHub Desktop.
Sort children by name in selected gameobjects. Unity3D 4.5+ (C#).
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
@Dorumeka
Copy link

Thanks for this 💯

@usernameHed
Copy link

Don't work inside prefabs with the current system of unity, so bad

@mikelortega
Copy link

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

@mmalinin
Copy link

mmalinin commented Oct 3, 2020

Thanks!

@Spade92
Copy link

Spade92 commented Feb 25, 2021

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;
}

@gackt2
Copy link

gackt2 commented Mar 6, 2023

Works great. Thank you!
For prefabs, need to use this inside the prefab editor.

@ALI3NPANDA
Copy link

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);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment