using UnityEngine;

namespace NBF
{
    public static class ModelUtils
    {
        public static void AlignmentParentCenter(this Transform transform)
        {
            // 获取子物体所有渲染器的边界
            Renderer[] renderers = transform.GetComponentsInChildren<Renderer>();
            Bounds bounds = new Bounds(transform.transform.position, Vector3.zero);
            foreach (Renderer renderer in renderers)
            {
                bounds.Encapsulate(renderer.bounds);
            }

            Vector3 center = bounds.center;

            // 将子物体设为父对象的子物体
            transform.transform.SetParent(transform.parent.transform);

            // 计算偏移量并调整位置
            Vector3 offset = center - transform.parent.transform.position;
            transform.transform.localPosition = -offset;
        }
    }
}