Skip to content

Instantly share code, notes, and snippets.

@BanksySan
Created April 25, 2020 11:56
Show Gist options
  • Save BanksySan/d54110d8dedd1dde3bf2f2bfa5fc6d5a to your computer and use it in GitHub Desktop.
Save BanksySan/d54110d8dedd1dde3bf2f2bfa5fc6d5a to your computer and use it in GitHub Desktop.
Blog: Draw Bounding Box
using UnityEngine;
using static UnityEditor.Handles;
using static UnityEngine.Color;
using static UnityEngine.Gizmos;
[ExecuteAlways]
[RequireComponent(typeof(Renderer))]
public class MarkCenterAndExtents : MonoBehaviour
{
private const float RADIUS = 0.02f;
private GUIStyle _labelStyle;
private Renderer _renderer;
private void Start()
{
_renderer = GetComponent<Renderer>();
}
private void OnDrawGizmos()
{
var bounds = _renderer.bounds;
Vector2 center = bounds.center;
DrawPoints(center, bounds);
}
private void DrawPoints(Vector3 center, Bounds bounds)
{
Gizmos.color = white;
DrawSphere(center, RADIUS);
Gizmos.color = red;
var extents = bounds.extents;
var topRight = center + extents;
var topLeft = center + new Vector3(-extents.x, extents.y);
var bottomRight = center + new Vector3(extents.x, -extents.y);
var bottomLeft = center - extents;
Gizmos.color = blue;
DrawSphere(topRight, RADIUS);
DrawSphere(topLeft, RADIUS);
DrawSphere(bottomRight, RADIUS);
DrawSphere(bottomLeft, RADIUS);
DrawLines(new[] {topRight, topLeft, bottomLeft, bottomRight}, new[] {0, 1, 1, 2, 2, 3, 3, 0});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment