1.获取矩形4个顶点
public static void DrawWriteRect(Vector2 center, Vector2 size)
{
#if UNITY_EDITOR
var halfx = size.x / 2;
var halfy = size.y / 2;
var movedis = new Vector3(center.x, center.y, 0);
var leftDownPos = new Vector2(-halfx, -halfy) + movedis;
var rightDownPos = new Vector2(halfx, -halfy) + movedis;
var leftUpPos = new Vector2(-halfx, halfy) + movedis;
var rightUpPos = new Vector2(halfx, halfy) + movedis;
Gizmos.DrawLine(leftDownPos, rightDownPos);
Gizmos.DrawLine(leftDownPos, leftUpPos);
Gizmos.DrawLine(leftUpPos, rightUpPos);
Gizmos.DrawLine(rightUpPos, rightDownPos);
Label(leftDownPos, leftDownPos.ToString());
Label(rightDownPos, rightDownPos.ToString());
Label(leftUpPos, leftUpPos.ToString());
Label(rightUpPos, rightUpPos.ToString());
#endif
}
2.获取旋转后矩形的4个顶点
public static void DrawWriteRect(Vector2 center, Vector2 size, Quaternion rotation)
{
#if UNITY_EDITOR
var halfx = size.x / 2;
var halfy = size.y / 2;
var movedis = new Vector3(center.x, center.y, 0);
var leftDownPos = rotation * new Vector2(-halfx, -halfy) + movedis;
var rightDownPos = rotation * new Vector2(halfx, -halfy) + movedis;
var leftUpPos = rotation * new Vector2(-halfx, halfy) + movedis;
var rightUpPos = rotation * new Vector2(halfx, halfy) + movedis;
Gizmos.DrawLine(leftDownPos, rightDownPos);
Gizmos.DrawLine(leftDownPos, leftUpPos);
Gizmos.DrawLine(leftUpPos, rightUpPos);
Gizmos.DrawLine(rightUpPos, rightDownPos);
Label(leftDownPos, leftDownPos.ToString());
Label(rightDownPos, rightDownPos.ToString());
Label(leftUpPos, leftUpPos.ToString());
Label(rightUpPos, rightUpPos.ToString());
#endif
}
3.绘制文本
public static void Label(Vector3 position, string text)
{
#if UNITY_EDITOR
Handles.color = Gizmos.color;
Handles.Label(position, text);
#endif
}