GameObjectExtensions.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using UnityEngine;
  2. public static class GameObjectExtensions
  3. {
  4. public static Bounds CalculatePreciseBounds(this GameObject gameObject)
  5. {
  6. Bounds bounds = new Bounds();
  7. bool flag = false;
  8. MeshFilter[] componentsInChildren1 = gameObject.GetComponentsInChildren<MeshFilter>();
  9. if (componentsInChildren1.Length != 0)
  10. {
  11. bounds = GetMeshBounds(componentsInChildren1[0].gameObject, componentsInChildren1[0].sharedMesh);
  12. flag = true;
  13. for (int index = 1; index < componentsInChildren1.Length; ++index)
  14. bounds.Encapsulate(GetMeshBounds(componentsInChildren1[index].gameObject,
  15. componentsInChildren1[index].sharedMesh));
  16. }
  17. SkinnedMeshRenderer[] componentsInChildren2 = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
  18. if (componentsInChildren2.Length != 0)
  19. {
  20. Mesh mesh = new Mesh();
  21. if (!flag)
  22. {
  23. componentsInChildren2[0].BakeMesh(mesh);
  24. bounds = GetMeshBounds(componentsInChildren2[0].gameObject, mesh);
  25. }
  26. for (int index = 1; index < componentsInChildren2.Length; ++index)
  27. {
  28. componentsInChildren2[index].BakeMesh(mesh);
  29. bounds = GetMeshBounds(componentsInChildren2[index].gameObject, mesh);
  30. }
  31. Object.Destroy(mesh);
  32. }
  33. return bounds;
  34. }
  35. private static Bounds GetMeshBounds(GameObject gameObject, Mesh mesh)
  36. {
  37. Bounds bounds = new Bounds();
  38. Vector3[] vertices = mesh.vertices;
  39. if (vertices.Length != 0)
  40. {
  41. bounds = new Bounds(gameObject.transform.TransformPoint(vertices[0]), Vector3.zero);
  42. for (int index = 1; index < vertices.Length; ++index)
  43. bounds.Encapsulate(gameObject.transform.TransformPoint(vertices[index]));
  44. }
  45. return bounds;
  46. }
  47. public static Bounds CalculateBounds(this GameObject gameObject, bool localSpace = false)
  48. {
  49. Vector3 position = gameObject.transform.position;
  50. Quaternion rotation = gameObject.transform.rotation;
  51. Vector3 localScale = gameObject.transform.localScale;
  52. if (localSpace)
  53. {
  54. gameObject.transform.position = Vector3.zero;
  55. gameObject.transform.rotation = Quaternion.identity;
  56. gameObject.transform.localScale = Vector3.one;
  57. }
  58. Bounds bounds1 = new Bounds();
  59. Renderer[] componentsInChildren = gameObject.GetComponentsInChildren<Renderer>();
  60. if (componentsInChildren.Length != 0)
  61. {
  62. Bounds bounds2 = componentsInChildren[0].bounds;
  63. bounds1.center = bounds2.center;
  64. bounds1.extents = bounds2.extents;
  65. for (int index = 1; index < componentsInChildren.Length; ++index)
  66. {
  67. Bounds bounds3 = componentsInChildren[index].bounds;
  68. bounds1.Encapsulate(bounds3);
  69. }
  70. }
  71. if (localSpace)
  72. {
  73. gameObject.transform.position = position;
  74. gameObject.transform.rotation = rotation;
  75. gameObject.transform.localScale = localScale;
  76. }
  77. return bounds1;
  78. }
  79. /// <summary>
  80. /// 获取模型的Bounds
  81. /// </summary>
  82. /// <param name="gameObject"></param>
  83. /// <returns></returns>
  84. public static Bounds CalculateModelBounds(GameObject gameObject)
  85. {
  86. Bounds bounds = gameObject.CalculateBounds(false);
  87. return bounds;
  88. }
  89. }