Add Editor script of BoxCollider to irregular model (sub object)

preface

We often encounter some irregular models that do not need accurate collision. At this time, we only need a BoxCollider to wrap it and its sub objects.
 Irregular sub object

Adding the collision box manually is very troublesome. Not only is the center point inaccurate, but the size also needs to be adjusted manually. By using the Renderer component, you can obtain the rendering size and center point of the model, and automatically generate a suitable BoxCollider after simple calculation.

realization

1. Key methods

  1. Selection.activeGameObject
    Under the UnityEditor namespace, get the selected game object in the hierarchy view.
  2. Render.bounds
    The rendering bounding box of MeshRender (or other Render components) of GameObject, that is, the actual size.
  3. Transform.InverseTransformPoint
    Converts world coordinates to local coordinates.

2. Complete script

 using UnityEngine; using UnityEditor; public class MyTools : Editor { [MenuItem("MyTools/AddBoxCollider")] static void AddBoxCollider() { if (Selection.activeGameObject == null) { #if UNITY_EDITOR Debug. Log ("<color=red>MyTools/AddBoxCollider: objects not selected</color>"); #endif return; } Transform target = Selection.activeGameObject.transform; Vector3 center = Vector3.zero; //Note that the components obtained by GetComponentsInChildren include the components attached to the parent object var renders = target.GetComponentsInChildren<Renderer>(); for (int i = 0;  i < renders.Length; i++) { //Bounds.center is the world coordinate of the center point of the rendering boundary center += renders[i].bounds.center; } //Find the average center point, similar to (point A+point B)/2 as the center point of the two-point line. center /= renders.Length; //Create a bounding box whose center is the calculated average center point. Bounds bounds = new Bounds(center, Vector3.zero); for (int i = 0;  i < renders.Length; i++) { //Make the bounding box wrap the RendererBounds of the sub object bounds.Encapsulate(renders[i].bounds); } BoxCollider boxCollider = target.gameObject.AddComponent<BoxCollider>(); //Bounds.center is the world coordinate, which needs to be converted to the local coordinate of the parent object boxCollider.center = target.InverseTransformPoint(bounds.center); //After the parent object is scaled, the length represented by size 1 of boxCollider will also be scaled, 1m ->0.1m //Bounds is different. It is always the standard 1m, so it needs to be divided by the parent object scaling. boxCollider.size = bounds.size / target.localScale.z; } }

effect

Put the script in the Editor folder, select the parent object Cube, find MyTools/AddBoxCollider in the menu bar, and click to add it.
 Irregular sub object

References

  1. [Header] [Unity] Unity Japan UnityChanSD Role
Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/4437.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*