unity 子物体包围盒
程序员文章站
2024-03-16 18:24:28
...
public static Vector3 GetCenter(Transform tans)
{
maxBound = new Bounds();
if (tans.childCount > 0)
{
if (tans.GetChild(0).childCount > 0)
{
if (tans.GetChild(0).GetChild(0).GetComponent<Renderer>() != null)
{
maxBound = tans.GetChild(0).GetChild(0).GetComponent<Renderer>().bounds;
for (int i = 1; i < tans.childCount; i++)
{
Transform child = tans.GetChild(i);
if (child.childCount > 0)
{
Renderer render = child.GetChild(0).GetComponent<Renderer>();
maxBound.Encapsulate(render.bounds);
}
}
}
}
}
return maxBound.center;
}