通过计算Mesh长度和物体的Position,来得到一个空物体下所有子物体的最大包围盒

通过计算Mesh长度和物体的Position,来得到一个空物体下所有子物体的最大包围盒

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
 
public class AddBox : Editor
{
	[MenuItem("AddBxoCollider/GetBox")]
	public static void GetBox()
	{
		Transform tran = Selection.activeTransform; //获取当前Hirecarchy窗口选择的物体的Transform
        
        BoxCollider box = tran.gameObject.AddComponent<BoxCollider>(); //给当前物体增加BoxCollider
        
        if (tran.GetComponent<RectTransform>() != null)//判断物体是否UI,如果是UI的话,BOX的XY等于UI的长宽
        {
            box.size = tran.GetComponent<RectTransform>().sizeDelta;
        }
        else if (tran.GetComponent<MeshFilter>() == null) //判断物体本身是否有Meshfilter,此处按照具体功能实现的不同,可以再做修改
        {
            Vector3 centerV3 = new Vector3(); //用来储存偏移量Vector3变量
            MeshFilter _meshFilter = new MeshFilter();//获取当前物体上的Meshfilter
            List<float> _positionXList = new List<float>();//用来储存所有子物体下的Position,X轴上的坐标,之后按从小往大排序,用来计算Box最大范围,以下同理
            List<float> _positionYList = new List<float>();
            List<float> _positionZList = new List<float>();
            float _XMeshLength = 0;//用来储存Mesh在X轴上的最大值,以下同理
            float _YMeshLength = 0;
            float _ZMeshLength = 0;
            foreach (Transform trans in tran.transform.GetComponentsInChildren<Transform>())//遍历所有子物体
            {
                if (trans.GetComponent<MeshFilter>() != null)
                {
                    _meshFilter = trans.GetComponent<MeshFilter>();
                    _XMeshLength = _meshFilter.mesh.bounds.size.x * trans.localScale.x > _XMeshLength ? _meshFilter.mesh.bounds.size.x * trans.localScale.x : _XMeshLength;//用三目运算符比较大小,乘以本地尺寸大小得到实际长度,并赋值
                    _YMeshLength = _meshFilter.mesh.bounds.size.y * trans.localScale.y > _YMeshLength ? _meshFilter.mesh.bounds.size.y * trans.localScale.y : _YMeshLength;
                    _ZMeshLength = _meshFilter.mesh.bounds.size.z * trans.localScale.z > _ZMeshLength ? _meshFilter.mesh.bounds.size.z * trans.localScale.z : _ZMeshLength;
                    _positionXList.Add(trans.localPosition.x);//将所有的坐标添加列表
                    _positionYList.Add(trans.localPosition.y);
                    _positionZList.Add(trans.localPosition.z);
                }
            }
            _positionXList.Sort();//从小往大排序方法,0为最小
            _positionYList.Sort();
            _positionZList.Sort();
            float _rangeX = _positionXList[_positionXList.Count - 1] + Mathf.Abs(_positionXList[0]);//计算X轴的总长度,前者是最大值,后者是最小值的绝对值
            float _rangeY = _positionYList[_positionYList.Count - 1] + Mathf.Abs(_positionYList[0]);
            float _rangeZ = _positionZList[_positionZList.Count - 1] + Mathf.Abs(_positionZList[0]);
            float _sizeX = _XMeshLength + _rangeX;//Box的X轴长度等于Mesh的实际长度加上X轴的最大值
            float _sizeY = _YMeshLength + _rangeY;
            float _sizeZ = _ZMeshLength + _rangeZ;
            float _gapX = _positionXList[_positionXList.Count - 1] + _positionXList[0];//X轴的位置最大值加上最小值,等于两者之间的差值
            float _gapY = _positionYList[_positionYList.Count - 1] + _positionYList[0];
            float _gapZ = _positionZList[_positionZList.Count - 1] + _positionZList[0];
            centerV3 = new Vector3(_gapX / 2, _gapY / 2, _gapZ / 2);//差值除以2等于中心点的偏移量
            box.center = centerV3;
            box.size = new Vector3(_sizeX, _sizeY, _sizeZ);
        }
    }
}

转发自:梧桐Mormon

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值