using UnityEngine; /// /// 获取某一物体的实际Bounds 参数 /// /// /// public class GetAllBounds { public static Bounds GetRendererBounds( GameObject obj) { Renderer[] renderers = obj.GetComponentsInChildren(); Bounds bounds = new Bounds(Vector3.zero, Vector3.zero); if (renderers.Length < 1) return bounds; bounds = new Bounds(renderers[0].bounds.center, Vector3.zero); float maxX = renderers[0].bounds.center.x; float minX = renderers[0].bounds.center.x; float maxY = renderers[0].bounds.center.y; float minY = renderers[0].bounds.center.y; float maxZ = renderers[0].bounds.center.z; float minZ = renderers[0].bounds.center.z; foreach (Renderer renderer in renderers) { if(renderer.enabled) { bounds.Encapsulate(renderer.bounds); if (maxX < renderer.bounds.center.x) maxX = renderer.bounds.center.x; if (minX > renderer.bounds.center.x) minX = renderer.bounds.center.x; if (maxY < renderer.bounds.center.y) maxY = renderer.bounds.center.y; if (minY > renderer.bounds.center.y) minY = renderer.bounds.center.y; if (maxZ < renderer.bounds.center.z) maxZ = renderer.bounds.center.z; if (minZ > renderer.bounds.center.z) minZ = renderer.bounds.center.z; } } bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f); return bounds; } public static Bounds GetMeshFiletBounds(GameObject obj) { MeshFilter[] meshFilters = obj.GetComponentsInChildren(); Bounds bounds = new Bounds(Vector3.zero, Vector3.zero); if (meshFilters.Length < 1) return bounds; bounds = new Bounds(meshFilters[0].transform.position, Vector3.zero); Bounds meshBounds = GetMeshBounds(meshFilters[0].gameObject, meshFilters[0].mesh); float maxX = meshFilters[0].transform.position.x + meshBounds.size.x / 2.0f; float minX = meshFilters[0].transform.position.x - meshBounds.size.x / 2.0f; float maxY = meshFilters[0].transform.position.y + meshBounds.size.y / 2.0f; float minY = meshFilters[0].transform.position.y - meshBounds.size.y / 2.0f; float maxZ = meshFilters[0].transform.position.z + meshBounds.size.z / 2.0f; float minZ = meshFilters[0].transform.position.z - meshBounds.size.z / 2.0f; foreach (MeshFilter meshFilter in meshFilters) { meshBounds = GetMeshBounds(meshFilter.gameObject, meshFilter.mesh); bounds.Encapsulate(meshBounds); if (maxX < meshFilter.transform.position.x + meshBounds.size.x / 2.0f) maxX = meshFilter.transform.position.x + meshBounds.size.x / 2.0f; if (minX > meshFilter.transform.position.x - meshBounds.size.x / 2.0f) minX = meshFilter.transform.position.x - meshBounds.size.x / 2.0f; if (maxY < meshFilter.transform.position.y + meshBounds.size.y / 2.0f) maxY = meshFilter.transform.position.y + meshBounds.size.y / 2.0f; if (minY > meshFilter.transform.position.y - meshBounds.size.y / 2.0f) minY = meshFilter.transform.position.y - meshBounds.size.y / 2.0f; if (maxZ < meshFilter.transform.position.z + meshBounds.size.z / 2.0f) maxZ = meshFilter.transform.position.z + meshBounds.size.z / 2.0f; if (minZ > meshFilter.transform.position.z - meshBounds.size.z / 2.0f) minZ = meshFilter.transform.position.z - meshBounds.size.z / 2.0f; } bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f); return bounds; } public static Bounds GetSkinnedMeshBounds(GameObject obj) { SkinnedMeshRenderer[] skinnedMeshRenderers = obj.GetComponentsInChildren(); Bounds bounds = new Bounds(Vector3.zero, Vector3.zero); if (skinnedMeshRenderers.Length < 1) return bounds; Mesh mesh = new Mesh(); skinnedMeshRenderers[0].BakeMesh(mesh); bounds = new Bounds(skinnedMeshRenderers[0].transform.position, Vector3.zero); Bounds meshBounds = GetMeshBounds(skinnedMeshRenderers[0].gameObject, mesh); float maxX = skinnedMeshRenderers[0].transform.position.x + meshBounds.size.x / 2.0f; float minX = skinnedMeshRenderers[0].transform.position.x - meshBounds.size.x / 2.0f; float maxY = skinnedMeshRenderers[0].transform.position.y + meshBounds.size.y / 2.0f; float minY = skinnedMeshRenderers[0].transform.position.y - meshBounds.size.y / 2.0f; float maxZ = skinnedMeshRenderers[0].transform.position.z + meshBounds.size.z / 2.0f; float minZ = skinnedMeshRenderers[0].transform.position.z - meshBounds.size.z / 2.0f; foreach (SkinnedMeshRenderer skinnedRenderer in skinnedMeshRenderers) { skinnedRenderer.BakeMesh(mesh); meshBounds = GetMeshBounds(skinnedRenderer.gameObject, mesh); bounds.Encapsulate(meshBounds); if (maxX < skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f) maxX = skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f; if (minX > skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f) minX = skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f; if (maxY < skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f) maxY = skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f; if (minY > skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f) minY = skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f; if (maxZ < skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f) maxZ = skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f; if (minZ > skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f) minZ = skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f; } bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f); return bounds; } public static Bounds GetAllObjectBounds(GameObject obj) { Renderer[] renderers = obj.GetComponentsInChildren(); MeshFilter[] meshFilters = obj.GetComponentsInChildren(); Bounds meshBounds = new Bounds(Vector3.zero,Vector3.zero); SkinnedMeshRenderer[] skinnedMeshRenderers = obj.GetComponentsInChildren(); Mesh mesh = new Mesh(); Bounds bounds = new Bounds(Vector3.zero, Vector3.zero); float maxX; float minX; float maxY; float minY; float maxZ; float minZ; if (renderers.Length>0) { bounds = new Bounds(renderers[0].transform.position, Vector3.zero); maxX = renderers[0].transform.position.x + renderers[0].bounds.size.x / 2.0f; minX = renderers[0].transform.position.x - renderers[0].bounds.size.x / 2.0f; maxY = renderers[0].transform.position.y + renderers[0].bounds.size.y / 2.0f; minY = renderers[0].transform.position.y - renderers[0].bounds.size.y / 2.0f; maxZ = renderers[0].transform.position.z + renderers[0].bounds.size.z / 2.0f; minZ = renderers[0].transform.position.z - renderers[0].bounds.size.z / 2.0f; } else if( meshFilters.Length>0) { bounds = new Bounds(meshFilters[0].transform.position, Vector3.zero); meshBounds = GetMeshBounds(meshFilters[0].gameObject, meshFilters[0].mesh); maxX = meshFilters[0].transform.position.x + meshBounds.size.x / 2.0f; minX = meshFilters[0].transform.position.x - meshBounds.size.x / 2.0f; maxY = meshFilters[0].transform.position.y + meshBounds.size.y / 2.0f; minY = meshFilters[0].transform.position.y - meshBounds.size.y / 2.0f; maxZ = meshFilters[0].transform.position.z + meshBounds.size.z / 2.0f; minZ = meshFilters[0].transform.position.z - meshBounds.size.z / 2.0f; } else if( skinnedMeshRenderers.Length>0) { skinnedMeshRenderers[0].BakeMesh(mesh); meshBounds = GetMeshBounds(skinnedMeshRenderers[0].gameObject, mesh); maxX = skinnedMeshRenderers[0].transform.position.x + meshBounds.size.x / 2.0f; minX = skinnedMeshRenderers[0].transform.position.x - meshBounds.size.x / 2.0f; maxY = skinnedMeshRenderers[0].transform.position.y + meshBounds.size.y / 2.0f; minY = skinnedMeshRenderers[0].transform.position.y - meshBounds.size.y / 2.0f; maxZ = skinnedMeshRenderers[0].transform.position.z + meshBounds.size.z / 2.0f; minZ = skinnedMeshRenderers[0].transform.position.z - meshBounds.size.z / 2.0f; } else { return bounds; } foreach (Renderer renderer in renderers) { bounds.Encapsulate(renderer.bounds); if (maxX < renderer.transform.position.x + renderer.bounds.size.x / 2.0f) maxX = renderer.transform.position.x + renderer.bounds.size.x / 2.0f; if (minX > renderer.transform.position.x - renderer.bounds.size.x / 2.0f) minX = renderer.transform.position.x - renderer.bounds.size.x / 2.0f; if (maxY < renderer.transform.position.y + renderer.bounds.size.y / 2.0f) maxY = renderer.transform.position.y + renderer.bounds.size.y / 2.0f; if (minY > renderer.transform.position.y - renderer.bounds.size.y / 2.0f) minY = renderer.transform.position.y - renderer.bounds.size.y / 2.0f; if (maxZ < renderer.transform.position.z + renderer.bounds.size.z / 2.0f) maxZ = renderer.transform.position.z + renderer.bounds.size.z / 2.0f; if (minZ > renderer.transform.position.z - renderer.bounds.size.z / 2.0f) minZ = renderer.transform.position.z - renderer.bounds.size.z / 2.0f; } foreach (MeshFilter meshFilter in meshFilters) { meshBounds = GetMeshBounds(meshFilter.gameObject, meshFilter.mesh); bounds.Encapsulate(meshBounds); if (maxX < meshFilter.transform.position.x + meshBounds.size.x / 2.0f) maxX = meshFilter.transform.position.x + meshBounds.size.x / 2.0f; if (minX > meshFilter.transform.position.x - meshBounds.size.x / 2.0f) minX = meshFilter.transform.position.x - meshBounds.size.x / 2.0f; if (maxY < meshFilter.transform.position.y + meshBounds.size.y / 2.0f) maxY = meshFilter.transform.position.y + meshBounds.size.y / 2.0f; if (minY > meshFilter.transform.position.y - meshBounds.size.y / 2.0f) minY = meshFilter.transform.position.y - meshBounds.size.y / 2.0f; if (maxZ < meshFilter.transform.position.z + meshBounds.size.z / 2.0f) maxZ = meshFilter.transform.position.z + meshBounds.size.z / 2.0f; if (minZ > meshFilter.transform.position.z - meshBounds.size.z / 2.0f) minZ = meshFilter.transform.position.z - meshBounds.size.z / 2.0f; } foreach (SkinnedMeshRenderer skinnedRenderer in skinnedMeshRenderers) { skinnedRenderer.BakeMesh(mesh); meshBounds = GetMeshBounds(skinnedRenderer.gameObject, mesh); bounds.Encapsulate(meshBounds); Debug.Log(skinnedRenderer.transform.position); Debug.Log(meshBounds.size); if (maxX < skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f) maxX = skinnedRenderer.transform.position.x + meshBounds.size.x / 2.0f; if (minX > skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f) minX = skinnedRenderer.transform.position.x - meshBounds.size.x / 2.0f; if (maxY < skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f) maxY = skinnedRenderer.transform.position.y + meshBounds.size.y / 2.0f; if (minY > skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f) minY = skinnedRenderer.transform.position.y - meshBounds.size.y / 2.0f; if (maxZ < skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f) maxZ = skinnedRenderer.transform.position.z + meshBounds.size.z / 2.0f; if (minZ > skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f) minZ = skinnedRenderer.transform.position.z - meshBounds.size.z / 2.0f; } bounds.center = new Vector3(maxX - (maxX - minX) / 2.0f, maxY - (maxY - minY) / 2.0f, maxZ - (maxZ - minZ) / 2.0f); return bounds; } private static Bounds GetMeshBounds(GameObject gameObject, Mesh mesh) { Bounds bounds = new Bounds(); Vector3[] vertices = mesh.vertices; if (vertices.Length != 0) { bounds = new Bounds(gameObject.transform.TransformPoint(vertices[0]), Vector3.zero); for (int index = 1; index < vertices.Length; ++index) bounds.Encapsulate(gameObject.transform.TransformPoint(vertices[index])); } return bounds; } }