Search Unity

How to draw a line? (Using GUI texture)

Discussion in '2D' started by Gurunext, Oct 15, 2016.

  1. Gurunext

    Gurunext

    Joined:
    Jan 23, 2013
    Posts:
    9
    It has been one of those highly missing features that only now I've realized how simple of a solution exists. The script allows you to draw a line out of a texture - you can have a custom texture (but would have to extend the code yourself). Line can be modified by it's color and width. Pretty basic and useful, but NOT provided by Unity.

    To solve this
    I got the original script from HERE. But I had to slightly modify the code... it took me quite a bit of time to figure out, but GUI.ScaleAround is a bit too complicated and doesn't serve the purpose. I just got it out of the way and scaled the texture directly.


    This is the Drawing.cs:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Drawing {
    4.  
    5.     public static Texture2D lineTex = new Texture2D(1, 1); //Single pixel texture
    6.  
    7.     public static void DrawLine(Rect rect) { DrawLine(rect, GUI.contentColor, 1.0f); }
    8.     public static void DrawLine(Rect rect, Color color) { DrawLine(rect, color, 1.0f); }
    9.     public static void DrawLine(Rect rect, float width) { DrawLine(rect, GUI.contentColor, width); }
    10.     public static void DrawLine(Rect rect, Color color, float width) { DrawLine(new Vector2(rect.x, rect.y), new Vector2(rect.x + rect.width, rect.y + rect.height), color, width); }
    11.     public static void DrawLine(Vector2 pointA, Vector2 pointB) { DrawLine(pointA, pointB, GUI.contentColor, 1.0f); }
    12.     public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color) { DrawLine(pointA, pointB, color, 1.0f); }
    13.     public static void DrawLine(Vector2 pointA, Vector2 pointB, float width) { DrawLine(pointA, pointB, GUI.contentColor, width); }
    14.     public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width) {
    15.  
    16.         Matrix4x4 matrix = GUI.matrix; // Save current GUI matrix.
    17.         if ((pointB - pointA).magnitude < 0.001f) // Deny if Line too small
    18.             return;
    19.  
    20.         Color savedColor = GUI.color;// Save current GUI color,
    21.         GUI.color = color;// and set the GUI color to the color parameter
    22. pointB.y += width/2; //fixes missalignment from applying wrong angle
    23.  
    24.         float angle = Vector3.Angle(pointB - pointA, Vector2.right);// Determine the angle of the line.
    25.  
    26.         // Vector3.Angle always returns a positive number.
    27.         // If pointB is above pointA, then angle needs to be negative.
    28.         if (pointA.y > pointB.y) { angle = -angle; }
    29.  
    30.  
    31.         Vector3 pivot = new Vector2(pointA.x, pointA.y + width / 2); //Pivot is on pointA and shifted to the middle with + 0.5f
    32.         GUIUtility.RotateAroundPivot(angle, pivot);// Set the rotation for the line with pointA as the origin
    33.         GUI.DrawTexture(new Rect(pointA.x, pointA.y, (pointB - pointA).magnitude, width), lineTex); //Draws and scales the line
    34.  
    35.         GUI.matrix = matrix; // Restore the GUI matrix...
    36.         GUI.color = savedColor; // ...and GUI color to previous values
    37.     }
    38. }
    Edit: If you want to modify alpha of the texture you will need to use this code instead of modifying GUI.color.
    Code (CSharp):
    1. lineTex.setPixel(0, 0, color);
    2. lineTex.Apply();
    Important edit: added a line of code to fix missalignment.

    This is an example use - it moves an object to mouse position and draws a line to it:
    Highlighting Camera.main.ScreenToWorldPoint method as insanely useful along with draw lines method.
    Code (CSharp):
    1. void OnGUI() {    
    2.             transform.position = Camera.main.ScreenToWorldPoint(
    3.                 new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 1));
    4.             var pointB = new Vector2(Screen.width / 4, Screen.height / 3);
    5.             var pointA = Camera.main.WorldToScreenPoint(transform.position + Vector3.left/10);
    6.             Drawing.DrawLine(pointA, pointB, Color.white, 40);
    7.         }
     
    Last edited: Oct 16, 2016
    vedram and Txguug like this.