DrawLine

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(New page: Author: capnbishop ==Description== This script allows you to draw a line between two points in the GUI system. ==Usage== Render a line in the GUI system by calling DrawLine and passing i...)
 
Line 1: Line 1:
 +
[[Category: JavaScript]]
 +
[[Category: GUI]]
 +
[[Category: Line]]
 
Author: capnbishop
 
Author: capnbishop
 
==Description==
 
==Description==
Line 90: Line 93:
 
     GUI.matrix = matrix;
 
     GUI.matrix = matrix;
 
}</javascript>
 
}</javascript>
[[Category:JavaScript]]
 

Revision as of 19:48, 24 October 2009

Author: capnbishop

Description

This script allows you to draw a line between two points in the GUI system.

Usage

Render a line in the GUI system by calling DrawLine and passing it a set of Vector2's for point A and point B of the line.

Optionally, you can pass DrawLine a color and width for the line, as well as using a Rect instead of Vector2's. If you do not pass a width, the line will have a width of 1. If you do not pass a color, the line will be rendered with the GUI.contentColor.

Example 1: <javascript>// Render a line from the center of the screen to the mouse position

var width = 1.0; var color = Color.black;

function OnGUI () {

   var pointA = Vector2(Screen.width/2, Screen.height/2);
   var pointB = Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
   DrawLine.DrawLine(pointA, pointB, color, width);

}</javascript>

JavaScript - DrawLine.js

<javascript>//**************************************************************************************************** // 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;

}</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox