48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
|
using UnityEngine;
|
||
|
using UnityEditor;
|
||
|
using System.Linq;
|
||
|
|
||
|
// ReSharper disable once CheckNamespace
|
||
|
namespace BlueWaterProject
|
||
|
{
|
||
|
public class HierarchySorter : EditorWindow
|
||
|
{
|
||
|
[MenuItem("Tools/Sort Children by Name")]
|
||
|
private static void SortChildrenByName()
|
||
|
{
|
||
|
if (Selection.activeTransform != null)
|
||
|
{
|
||
|
Undo.RecordObject(Selection.activeTransform.gameObject, "Sort Children by Name");
|
||
|
|
||
|
var children = Selection.activeTransform.Cast<Transform>().OrderBy(t => t.name).ToList();
|
||
|
for (var i = 0; i < children.Count; i++)
|
||
|
{
|
||
|
children[i].SetSiblingIndex(i);
|
||
|
}
|
||
|
|
||
|
EditorUtility.SetDirty(Selection.activeTransform.gameObject);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Debug.LogError("No object selected in the hierarchy");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[MenuItem("Tools/Sort Children of Selected Objects by Name")]
|
||
|
private static void SortChildrenOfSelectedObjectsByName()
|
||
|
{
|
||
|
foreach (var parentTransform in Selection.transforms)
|
||
|
{
|
||
|
Undo.RecordObject(parentTransform.gameObject, "Sort Children by Name");
|
||
|
|
||
|
var children = parentTransform.Cast<Transform>().OrderBy(t => t.name).ToList();
|
||
|
for (var i = 0; i < children.Count; i++)
|
||
|
{
|
||
|
children[i].SetSiblingIndex(i);
|
||
|
}
|
||
|
|
||
|
EditorUtility.SetDirty(parentTransform.gameObject);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|