BoundingBoxUtils.cs 989 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class BoundingBoxUtils
  5. {
  6. public static void ApplyMaterialToAllRenderers(GameObject root, Material material)
  7. {
  8. if (material != null)
  9. {
  10. Renderer[] renderers = root.GetComponentsInChildren<Renderer>();
  11. for (int i = 0; i < renderers.Length; ++i)
  12. {
  13. renderers[i].sharedMaterial = material;
  14. }
  15. }
  16. }
  17. public static Bounds GetMaxBounds(GameObject g)
  18. {
  19. var b = new Bounds();
  20. Mesh currentMesh;
  21. foreach (MeshFilter r in g.GetComponentsInChildren<MeshFilter>())
  22. {
  23. if ((currentMesh = r.sharedMesh) == null) { continue; }
  24. if (b.size == Vector3.zero)
  25. {
  26. b = currentMesh.bounds;
  27. }
  28. else
  29. {
  30. b.Encapsulate(currentMesh.bounds);
  31. }
  32. }
  33. return b;
  34. }
  35. }