Drawing a bounding box similar to box collider

I’m creating a script which spawns objects inside an area, and I’d like to be able to see the area inside the editor similar to how I can see the bounds on a box collider.

Is there a simple way of doing this? My parameters would be very much the same as the box collider’s as well, with Center:{x,y,z} and Size:{x,y,z} adjustable in the inspector.

Here is a code solution that shows the mesh bounding box. I’ve commented out a bit of code you can swap back in that displays the box collider bounds instead. It is tagged as “ExecuteInEditMode()” so you can see the lines while editing.

using UnityEngine;
using System.Collections;

[ExecuteInEditMode()] 

public class ShowMeshBounds : MonoBehaviour {
	public Color color = Color.green;
	
	private Vector3 v3FrontTopLeft;
	private Vector3 v3FrontTopRight;
	private Vector3 v3FrontBottomLeft;
	private Vector3 v3FrontBottomRight;
	private Vector3 v3BackTopLeft;
	private Vector3 v3BackTopRight;
	private Vector3 v3BackBottomLeft;
	private Vector3 v3BackBottomRight;	
	
void Update() {
	CalcPositons();
	DrawBox();
}
	
void CalcPositons(){
	Bounds bounds = GetComponent<MeshFilter>().mesh.bounds;
		
	//Bounds bounds;
	//BoxCollider bc = GetComponent<BoxCollider>();
	//if (bc != null)
	//	bounds = bc.bounds;
	//else
		//return;
		
    Vector3 v3Center = bounds.center;
    Vector3 v3Extents = bounds.extents;
 
    v3FrontTopLeft     = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
    v3FrontTopRight    = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
    v3FrontBottomLeft  = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
	v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
    v3BackTopLeft      = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
    v3BackTopRight 	   = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
    v3BackBottomLeft   = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
    v3BackBottomRight  = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner
		
  	v3FrontTopLeft     = transform.TransformPoint(v3FrontTopLeft);
	v3FrontTopRight    = transform.TransformPoint(v3FrontTopRight);
	v3FrontBottomLeft  = transform.TransformPoint(v3FrontBottomLeft);
	v3FrontBottomRight = transform.TransformPoint(v3FrontBottomRight);
	v3BackTopLeft      = transform.TransformPoint(v3BackTopLeft);
	v3BackTopRight     = transform.TransformPoint(v3BackTopRight);
	v3BackBottomLeft   = transform.TransformPoint(v3BackBottomLeft);
	v3BackBottomRight  = transform.TransformPoint(v3BackBottomRight);	
}
	
void DrawBox() {
	//if (Input.GetKey (KeyCode.S)) {
	Debug.DrawLine (v3FrontTopLeft, v3FrontTopRight, color);
	Debug.DrawLine (v3FrontTopRight, v3FrontBottomRight, color);
	Debug.DrawLine (v3FrontBottomRight, v3FrontBottomLeft, color);
	Debug.DrawLine (v3FrontBottomLeft, v3FrontTopLeft, color);
		
	Debug.DrawLine (v3BackTopLeft, v3BackTopRight, color);
	Debug.DrawLine (v3BackTopRight, v3BackBottomRight, color);
	Debug.DrawLine (v3BackBottomRight, v3BackBottomLeft, color);
	Debug.DrawLine (v3BackBottomLeft, v3BackTopLeft, color);
		
	Debug.DrawLine (v3FrontTopLeft, v3BackTopLeft, color);
	Debug.DrawLine (v3FrontTopRight, v3BackTopRight, color);
	Debug.DrawLine (v3FrontBottomRight, v3BackBottomRight, color);
	Debug.DrawLine (v3FrontBottomLeft, v3BackBottomLeft, color);
	//}
}
	
}

Read up about gizmos, particularly DrawWireCube :

I’ve used Gizmos to accomplish a Box2DCollider type of visualization.
In summary, you’ll set its width and height in the Inspector, and the box will update its size while changing the values.
Here’s the code:

public class MyClassExampleBehaviour : MonoBehaviour {
	[SerializeField]
	float width = 0;
	[SerializeField]
	float height = 0;

	void OnDrawGizmos() {
		Gizmos.color = Color.yellow;
		float wHalf = (width * .5f);
		float hHalf = (height * .5f);
		Vector3 topLeftCorner = new Vector3 (transform.position.x - wHalf, transform.position.y + hHalf, 1f);
		Vector3 topRightCorner = new Vector3 (transform.position.x + wHalf, transform.position.y + hHalf, 1f);
		Vector3 bottomLeftCorner = new Vector3 (transform.position.x - wHalf, transform.position.y - hHalf, 1f);
		Vector3 bottomRightCorner = new Vector3 (transform.position.x + wHalf, transform.position.y - hHalf, 1f);
		Gizmos.DrawLine (topLeftCorner, topRightCorner);
		Gizmos.DrawLine (topRightCorner, bottomRightCorner);
		Gizmos.DrawLine (bottomRightCorner, bottomLeftCorner);
		Gizmos.DrawLine (bottomLeftCorner, topLeftCorner);
	}
}

the most simplest way to display BB’s. Meshrender’s bb can be replaced with any BB…
=>

using UnityEngine;

[RequireComponent(typeof(MeshRenderer))]
public class DrawGizmoBB : MonoBehaviour
{
    void OnDrawGizmosSelected()
    {
        // Draw a yellow cube at the transform position
        Gizmos.color = Color.yellow;
        var bb = GetComponent<MeshRenderer>().bounds;
        Gizmos.DrawWireCube(bb.center, bb.size);
    }
}

You should try my solution in the asset store. It’s a lot less convoluted than the Gizmos version.