48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace Core.Editor
|
|
{
|
|
public class RecenterParentTool
|
|
{
|
|
[MenuItem("Tools/Recenter Parent")]
|
|
public static void RecenterParent()
|
|
{
|
|
if(Selection.gameObjects.Length != 1)
|
|
return;
|
|
|
|
Transform parent = Selection.gameObjects[0].transform;
|
|
Vector3 center = Vector3.zero;
|
|
|
|
var undoObjects = new List<Object> {
|
|
parent
|
|
};
|
|
|
|
int childCount = parent.childCount;
|
|
int activeCount = 0;
|
|
for (int i = 0; i < childCount; i++)
|
|
{
|
|
Transform child = parent.GetChild(i);
|
|
undoObjects.Add(child);
|
|
if(child.gameObject.activeSelf)
|
|
{
|
|
center += child.position;
|
|
activeCount++;
|
|
}
|
|
}
|
|
|
|
Undo.RecordObjects(undoObjects.ToArray(), "Recenter parent");
|
|
|
|
center /= activeCount;
|
|
Vector3 translation = center - parent.position;
|
|
parent.position = center;
|
|
for (int i = 0; i < childCount; i++)
|
|
{
|
|
Transform child = parent.GetChild(i);
|
|
child.position -= translation;
|
|
}
|
|
|
|
}
|
|
}
|
|
} |