Unity3D遍歷所有空物體下的子物體的MeshFiles,創建對應大小的Box

通過計算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);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章