forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxColliderFitChildren.cs
More file actions
46 lines (40 loc) · 1.42 KB
/
BoxColliderFitChildren.cs
File metadata and controls
46 lines (40 loc) · 1.42 KB
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
41
42
43
44
45
46
// Adjust Box Collider to fit child meshes inside
// Usage: You have empty parent transform, with child meshes inside, add box collider to parent then use this
// NOTE: Doesnt work if root transform is rotated
using UnityEngine;
using UnityEditor;
namespace UnityLibrary
{
public class BoxColliderFitChildren : MonoBehaviour
{
[MenuItem("CONTEXT/BoxCollider/Fit to Children")]
static void FixSize(MenuCommand command)
{
BoxCollider col = (BoxCollider)command.context;
// record undo
Undo.RecordObject(col.transform, "Fit Box Collider To Children");
// get child mesh bounds
var b = GetRecursiveMeshBounds(col.gameObject);
// set collider local center and size
col.center = col.transform.root.InverseTransformVector(b.center) - col.transform.position;
col.size = b.size;
}
public static Bounds GetRecursiveMeshBounds(GameObject go)
{
var r = go.GetComponentsInChildren<Renderer>();
if (r.Length > 0)
{
var b = r[0].bounds;
for (int i = 1; i < r.Length; i++)
{
b.Encapsulate(r[i].bounds);
}
return b;
}
else // TODO no renderers
{
return new Bounds(Vector3.one, Vector3.one);
}
}
}
}