카테고리 없음
flattens the hierarchy by assigning all child objects directly to an parent in the Unity Editor. 모든 자식을 하나의 부모로 평탄화 하는 스크립트
시코.
2024. 4. 30. 16:07
728x90
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class HierarchyFlattor: EditorWindow
{
private Transform _parent;
[MenuItem("Tools/HierarchyFlattor")]
public static void ShowWindow()
{
GetWindow<HierarchyFlattor>("HierarchyFlattor");
}
private void OnGUI()
{
GUILayout.Label("Set all childs parent to selected parent", EditorStyles.boldLabel);
_parent = EditorGUILayout.ObjectField("Parent Object", _parent, typeof(Transform), true) as Transform;
if(GUILayout.Button("Flatten"))
{
if(_parent != null)
{
Execute(_parent);
}
}
}
public void Execute(Transform parent)
{
List<Transform> allChild = new();
AddAllChild(allChild, _parent);
FlattenAllSub(allChild, parent);
}
private void AddAllChild(List<Transform> container, Transform parant)
{
for (int i = 0; i < parant.childCount; i++)
{
Transform CurChild = parant.GetChild(i).transform;
container.Add(CurChild);
AddAllChild(container, CurChild);
}
}
private void FlattenAllSub(List<Transform> container, Transform parent)
{
foreach (Transform child in container)
{
Debug.Log($"Flatten{child.name}");
Undo.SetTransformParent(child, parent, "Flatten All Sub");
}
}
}
Asset에 Editor 폴더 생성후 스크립트를 그 안에 넣은후 Tool에서 HierarchyFlattor 실행하면 됩니다.
728x90