Created
October 24, 2013 08:05
-
-
Save attilam/7133080 to your computer and use it in GitHub Desktop.
Small Unity3D editor script that will create a BoxCollider that encapsulates all Renderers in the selected Transform's hierarchy.
This file contains 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 characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
public class FittedBoxCollider { | |
[MenuItem("GameObject/Create Other/Create Fitted BoxCollider")] | |
static void FittedBoxColliderMenu() { | |
Transform transform = Selection.activeTransform; | |
Quaternion rotation = transform.rotation; | |
transform.rotation = Quaternion.identity; | |
BoxCollider collider = transform.collider as BoxCollider; | |
if (collider == null) { | |
transform.gameObject.AddComponent<BoxCollider>(); | |
collider = transform.collider as BoxCollider; | |
} | |
Bounds bounds = new Bounds(transform.position, Vector3.zero); | |
ExtendBounds(transform, ref bounds); | |
collider.center = bounds.center-transform.position; | |
collider.size = bounds.size; | |
transform.rotation = rotation; | |
} | |
[MenuItem("GameObject/Create Other/Create Fitted BoxCollider", true)] | |
static bool ValidateFittedBoxColliderMenu() { | |
return Selection.activeTransform != null; | |
} | |
static void ExtendBounds(Transform t, ref Bounds b) { | |
Renderer rend = t.renderer; | |
if (rend != null) { | |
b.Encapsulate(rend.bounds.min); | |
b.Encapsulate(rend.bounds.max); | |
} | |
foreach(Transform t2 in t) { | |
ExtendBounds(t2, ref b); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment