1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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<Renderer>();
- 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<MeshFilter>())
- {
- if ((currentMesh = r.sharedMesh) == null) { continue; }
- if (b.size == Vector3.zero)
- {
- b = currentMesh.bounds;
- }
- else
- {
- b.Encapsulate(currentMesh.bounds);
- }
- }
- return b;
- }
- /// <summary>
- /// Transforms the size from local to world.
- /// </summary>
- /// <param name="transform">The transform.</param>
- /// <param name="localSize">The local size.</param>
- /// <returns>World size.</returns>
- 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;
- }
- }
|