Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make always visible a BoxCollider2D in Unity Editor.
using UnityEngine;
[ExecuteInEditMode]
public class BoxCollider2DAlwaysVisible : MonoBehaviour
{
public Color color = Color.white;
private BoxCollider2D BoxCollider;
void Start()
{
BoxCollider = GetComponent<BoxCollider2D>();
}
void OnDrawGizmos()
{
Gizmos.color = color;
if (BoxCollider != null)
{
Vector3 Pos = transform.position;
Vector3 Size = new Vector3(BoxCollider.size.x * transform.lossyScale.x, BoxCollider.size.y * transform.lossyScale.y, 1f);
Size /= 2f;
Vector3[] Points = new Vector3[4];
Points[0].Set(Pos.x - Size.x, Pos.y + Size.y, Pos.z);
Points[1].Set(Pos.x + Size.x, Pos.y + Size.y, Pos.z);
Points[2].Set(Pos.x + Size.x, Pos.y - Size.y, Pos.z);
Points[3].Set(Pos.x - Size.x, Pos.y - Size.y, Pos.z);
float RotationInRad = transform.eulerAngles.z * Mathf.Deg2Rad;
for(int i=0; i<4; i++)
{
float x = Points[i].x - Pos.x, y = Points[i].y - Pos.y;
Points[i].x = x * Mathf.Cos(RotationInRad) - y * Mathf.Sin(RotationInRad);
Points[i].y = x * Mathf.Sin(RotationInRad) + y * Mathf.Cos(RotationInRad);
Points[i].x = Points[i].x + Pos.x;
Points[i].y = Points[i].y + Pos.y;
}
Gizmos.DrawLine(Points[0], Points[1]);
Gizmos.DrawLine(Points[1], Points[2]);
Gizmos.DrawLine(Points[2], Points[3]);
Gizmos.DrawLine(Points[3], Points[0]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.