DrawLine

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(JavaScript - DrawLine.js: Changed to use GUI.color rather than setting the color of the texture, which is faster and easier)
Line 42: Line 42:
 
//****************************************************************************************************
 
//****************************************************************************************************
  
static var lastColor : Color;
 
 
static var lineTex : Texture2D;
 
static var lineTex : Texture2D;
  
Line 56: Line 55:
 
     var matrix = GUI.matrix;
 
     var matrix = GUI.matrix;
 
      
 
      
     // Generate a single pixel texture with the designated color. This will be the color of the line.
+
     // Generate a single pixel texture if it doesn't exist
    // 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 (!lineTex) { lineTex = Texture2D(1, 1); }
     if (color != lastColor) {
+
      
        lineTex.SetPixel(0, 0, color);
+
    // Store current GUI color, so we can switch it back later,
        lineTex.Apply();
+
    // and set the GUI color to the color parameter
        lastColor = color;
+
    var savedColor = GUI.color;
    }
+
    GUI.color = color;
  
 
     // Determine the angle of the line.
 
     // Determine the angle of the line.
Line 90: Line 87:
 
     GUI.DrawTexture(Rect(pointA.x, pointA.y, 1, 1), lineTex);
 
     GUI.DrawTexture(Rect(pointA.x, pointA.y, 1, 1), lineTex);
 
      
 
      
     // We're done.  Restore the GUI matrix to whatever it was before.
+
     // We're done.  Restore the GUI matrix and GUI color to whatever they were before.
 
     GUI.matrix = matrix;
 
     GUI.matrix = matrix;
 +
    GUI.color = savedColor;
 
}</javascript>
 
}</javascript>

Revision as of 00:20, 7 March 2010

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 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 if it doesn't exist
   if (!lineTex) { lineTex = Texture2D(1, 1); }
   
   // Store current GUI color, so we can switch it back later,
   // and set the GUI color to the color parameter
   var savedColor = GUI.color;
   GUI.color = 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 and GUI color to whatever they were before.
   GUI.matrix = matrix;
   GUI.color = savedColor;

}</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox