using System.Collections; using System.Collections.Generic; using UnityEngine; public class BoundingBoxUtils { public static void ApplyMaterialToAllRenderers(GameObject root, Material material) { if (material != null) { Renderer[] renderers = root.GetComponentsInChildren(); for (int i = 0; i < renderers.Length; ++i) { renderers[i].sharedMaterial = material; } } } public static Bounds GetMaxBounds(GameObject g) { var b = new Bounds(); Mesh currentMesh; foreach (MeshFilter r in g.GetComponentsInChildren()) { if ((currentMesh = r.sharedMesh) == null) { continue; } if (b.size == Vector3.zero) { b = currentMesh.bounds; } else { b.Encapsulate(currentMesh.bounds); } } return b; } /// /// Transforms the size from local to world. /// /// The transform. /// The local size. /// World size. public static Vector3 TransformSize(Transform transform, Vector3 localSize) { Vector3 transformedSize = new Vector3(localSize.x, localSize.y, localSize.z); Transform t = transform; do { t = t.parent; if (t != null) { transformedSize.x *= t.localScale.x; transformedSize.y *= t.localScale.y; transformedSize.z *= t.localScale.z; } } while (t != null); return transformedSize; } }