-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColliderToFit.cs
40 lines (35 loc) · 1.26 KB
/
ColliderToFit.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using UnityEngine;
using UnityEditor;
public class ColliderToFit : MonoBehaviour
{
[MenuItem("My Tools/Collider/Fit to Children")]
static void FitToChildren()
{
foreach (GameObject rootGameObject in Selection.gameObjects)
{
if (!(rootGameObject.GetComponent<BoxCollider>() is BoxCollider))
continue;
bool hasBounds = false;
Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
for (int i = 0; i < rootGameObject.transform.childCount; ++i)
{
Renderer childRenderer = rootGameObject.transform.GetChild(i).GetComponent<Renderer>();
if (childRenderer != null)
{
if (hasBounds)
{
bounds.Encapsulate(childRenderer.bounds);
}
else
{
bounds = childRenderer.bounds;
hasBounds = true;
}
}
}
BoxCollider collider = (BoxCollider)rootGameObject.GetComponent<BoxCollider>();
collider.center = bounds.center - rootGameObject.transform.position;
collider.size = bounds.size;
}
}
}