using System.Collections; using System.Collections.Generic; using UnityEngine; public class JISuanBox { public static void setBox(GameObject gameObject) { Transform[] children = gameObject.GetComponentsInChildren(); Bounds totalBounds = new Bounds(); totalBounds.center = gameObject.transform.position; foreach (Transform child in children) { MeshRenderer meshFilter = child.GetComponent(); if (meshFilter != null) { totalBounds.Encapsulate(meshFilter.bounds); } } Vector3 totalSize = totalBounds.size; BoxCollider box = gameObject.GetComponent(); if (box == null) { box = gameObject.AddComponent(); } box.size = totalSize / gameObject.transform.localScale.x; box.center = gameObject.transform.InverseTransformPoint(totalBounds.center); } }