Created
April 23, 2024 08:38
-
-
Save unitycoder/d5f576d7ae43d14422ec6c94923b03a2 to your computer and use it in GitHub Desktop.
Revisions
-
unitycoder created this gist
Apr 23, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ // https://github.com/UnityCommunity/UnityLibrary/blob/master/Assets/Scripts/Editor/ContextMenu/BoxColliderFitChildren.cs // 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); } } } }