I’ve been struggling with the lack of a DrawLine function which is painfully vacant from the Unity GUI system (seriously, guys, what’s the holdup?), and I know that a number of people have had trouble with this also. Well, thanks to some inspiration from a few posts in this forum, I finally figured out a fast, effective way of doing it, and posted the script on the Unify wiki.
http://www.unifycommunity.com/wiki/index.php?title=DrawLine
For ease’s sake, here it is as well, with the example built in:
var width = 1.0;
var color = Color.black;
function OnGUI () {
// Render a line from the center of the screen to the mouse position
DrawLine(Vector2(Screen.width/2, Screen.height/2), Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y), color, width);
}
/****************************************************************************************************
static function DrawLine(rect : Rect) : void
static function DrawLine(rect : Rect, color : Color) : void
static function DrawLine(rect : Rect, width : float) : void
static function DrawLine(rect : Rect, color : Color, width : float) : void
static function DrawLine(pointA : Vector2, pointB : Vector2) : void
static function DrawLine(pointA : Vector2, pointB : Vector2, color : Color) : void
static function DrawLine(pointA : Vector2, pointB : Vector2, width : float) : void
static function DrawLine(pointA : Vector2, pointB : Vector2, color : Color, width : float) : void
Draws a GUI line on the screen.
DrawLine makes up for the severe lack of 2D line rendering in the Unity runtime GUI system.
This function works by drawing a 1x1 texture filled with a color, which is then scaled
and rotated by altering the GUI matrix. The matrix is restored afterwards.
****************************************************************************************************/
static var lastColor : Color;
static var lineTex : Texture2D;
static function DrawLine(rect : Rect) { DrawLine(rect, GUI.contentColor, 1.0); }
static function DrawLine(rect : Rect, color : Color) { DrawLine(rect, color, 1.0); }
static function DrawLine(rect : Rect, width : float) { DrawLine(rect, GUI.contentColor, width); }
static function DrawLine(rect : Rect, color : Color, width : float) { DrawLine(Vector2(rect.x, rect.y), Vector2(rect.x + rect.width, rect.y + rect.height), color, width); }
static function DrawLine(pointA : Vector2, pointB : Vector2) { DrawLine(pointA, pointB, GUI.contentColor, 1.0); }
static function DrawLine(pointA : Vector2, pointB : Vector2, color : Color) { DrawLine(pointA, pointB, color, 1.0); }
static function DrawLine(pointA : Vector2, pointB : Vector2, width : float) { DrawLine(pointA, pointB, GUI.contentColor, width); }
static function DrawLine(pointA : Vector2, pointB : Vector2, color : Color, width : float) {
// Save the current GUI matrix, since we're going to make changes to it.
var matrix = GUI.matrix;
// Generate a single pixel texture with the designated color. This will be the color of the line.
// This looks more complex then it needs to be for optimization.
// Instead of regenerating the texture every time, we only do so when the color has changed.
if (!lineTex) { lineTex = Texture2D(1, 1); }
if (color != lastColor) {
lineTex.SetPixel(0, 0, color);
lineTex.Apply();
lastColor = color;
}
// Determine the angle of the line.
var angle = Vector3.Angle(pointB-pointA, Vector2.right);
// Vector3.Angle always returns a positive number.
// If pointB is above pointA, then angle needs to be negative.
if (pointA.y > pointB.y) { angle = -angle; }
// Use ScaleAroundPivot to adjust the size of the line.
// We could do this when we draw the texture, but by scaling it here we can use
// non-integer values for the width and length (such as sub 1 pixel widths).
// Note that the pivot point is at +.5 from pointA.y, this is so that the width of the line
// is centered on the origin at pointA.
GUIUtility.ScaleAroundPivot(Vector2((pointB-pointA).magnitude, width), Vector2(pointA.x, pointA.y + 0.5));
// Set the rotation for the line.
// The angle was calculated with pointA as the origin.
GUIUtility.RotateAroundPivot(angle, pointA);
// Finally, draw the actual line.
// We're really only drawing a 1x1 texture from pointA.
// The matrix operations done with ScaleAroundPivot and RotateAroundPivot will make this
// render with the proper width, length, and angle.
GUI.DrawTexture(Rect(pointA.x, pointA.y, 1, 1), lineTex);
// We're done. Restore the GUI matrix to whatever it was before.
GUI.matrix = matrix;
}