demand
Select an object in the Snece, press the shortcut keys to add a child cube to the object, and then manually adjust the Cube’s BoxCollider. Once the adjustment is complete, cancel the meshReaderer. It’s used for collisions when the camera moves.
The effect
implementation
Create a c# script in the editor folder
using UnityEngine;
using UnityEditor;
public class AddBoxCollider : Editor
{
// Add to unity's menu bar, & C shortcut ALT+C
[MenuItem("Tool/AddBoxCollider &C")]
public static void FAddBoxCollider()
{
BoxCollider now;
// GameObject is selected
if(Selection.activeGameObject ! =null)
{
if (Selection.activeGameObject.GetComponent<BoxCollider>() == null)
{
now = Selection.activeGameObject.AddComponent<BoxCollider>();
}
else
{
now = Selection.activeGameObject.GetComponent<BoxCollider>();
}
// Create a child object cube
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.parent = Selection.activeGameObject.transform;
cube.transform.localScale = now.size;
cube.transform.localPosition = now.center;
//cube.transform.localRotation = Quaternion.Euler(Vector3.zero);
cube.transform.localEulerAngles = Vector3.zero;
cube.name = Selection.activeGameObject.name + "collider";
//cube.transform.parent =
// Delete the original object's BoxCollider
DestroyImmediate(now);
}
else
{
Debug.LogWarning("must choose someone"); }}}Copy the code