using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class BatchGenerateBoxColliders : Editor
{
    [MenuItem("添加碰撞体/创建")]
    static void AddSingleBoxCollider()
    {
        for (int i = 0; i < Selection.gameObjects.Length; i++)
        {
            AddGameObjectCollider(Selection.gameObjects[i]);
        }
    }

    [MenuItem("添加碰撞体/创建限制移动碰撞体")]
    static void AddUnMoveBoxCollider()
    {
        for (int i = 0; i < Selection.gameObjects.Length; i++)
        {
            AddGameObjectWithCollider(Selection.gameObjects[i]);
        }
    }

    /// <summary>
    /// 添加碰撞体
    /// </summary>
    /// <param name="gameObject"></param>  
    public static void AddGameObjectCollider(GameObject gameObject)
    {
        Vector3 pos = gameObject.transform.localPosition;
        Quaternion qt = gameObject.transform.localRotation;
        Vector3 ls = gameObject.transform.localScale;

        gameObject.transform.position = Vector3.zero;
        gameObject.transform.eulerAngles = Vector3.zero;
        gameObject.transform.localScale = Vector3.one;
        //获取物体的最小包围盒
        Bounds itemBound = GetLocalBounds(gameObject);

        gameObject.transform.localPosition = pos;
        gameObject.transform.localRotation = qt;
        gameObject.transform.localScale = ls;


        //parent = null;
        if (!gameObject.GetComponent<Collider>())
            gameObject.AddComponent<BoxCollider>();
        if (gameObject.GetComponent<BoxCollider>())
        {
            gameObject.GetComponent<BoxCollider>().size = itemBound.size;

            gameObject.GetComponent<BoxCollider>().center = itemBound.center;
        }
    }
    /// <summary>
    /// 添加碰撞体(并创建新物体)
    /// </summary>
    /// <param name="gameObject"></param>  
    public static void AddGameObjectWithCollider(GameObject gameObject)
    {
        Vector3 pos = gameObject.transform.localPosition;
        Quaternion qt = gameObject.transform.localRotation;
        Vector3 ls = gameObject.transform.localScale;

        gameObject.transform.position = Vector3.zero;
        gameObject.transform.eulerAngles = Vector3.zero;
        gameObject.transform.localScale = Vector3.one;
        //获取物体的最小包围盒
        Bounds itemBound = GetLocalBounds(gameObject);

        gameObject.transform.localPosition = pos;
        gameObject.transform.localRotation = qt;
        gameObject.transform.localScale = ls;
        //创建物体
        GameObject UnMoveCollider = new GameObject();
        UnMoveCollider.transform.SetParent(gameObject.transform);
        UnMoveCollider.transform.localPosition = Vector3.zero;
        UnMoveCollider.transform.localRotation = Quaternion.identity;
        UnMoveCollider.transform.localScale = Vector3.one;
        UnMoveCollider.name = gameObject.name + "_UnMoveCollider";
        UnMoveCollider.tag = "UnMoveArea";

        BoxCollider boxCollider = UnMoveCollider.AddComponent<BoxCollider>();
        boxCollider.size = itemBound.size;
        boxCollider.center = itemBound.center;

        GameObject UnMoveRoot = GameObject.Find("UnMoveRoot");
        if (UnMoveRoot == null)
        {
            UnMoveRoot = new GameObject("UnMoveRoot");
            UnMoveRoot.transform.position = Vector3.zero;
            UnMoveRoot.transform.rotation = Quaternion.identity;
            UnMoveRoot.transform.localScale = Vector3.one;
        }
        UnMoveCollider.transform.SetParent(UnMoveRoot.transform);
    }
    /// <summary>
    /// 获得对象的最小包围盒
    /// </summary>
    public static Bounds GetLocalBounds(GameObject target)
    {
        Renderer[] mfs = target.GetComponentsInChildren<Renderer>();
        Bounds bounds = new Bounds();
        if (mfs.Length != 0)
        {
            bounds = mfs[0].bounds;
            foreach (Renderer mf in mfs)
            {
                bounds.Encapsulate(mf.bounds);
            }
        }
        return bounds;
    }

}