Search Unity

[CodeSource]Simple draw line from GUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by IsGreen, Aug 9, 2014.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    This is the code where we can change their color and size:

    Code (CSharp):
    1. public class temp : MonoBehaviour {
    2.    
    3.     public Vector2 A   = Vector2.zero;
    4.     public Vector2 B   = Vector2.zero;
    5.     public Color color = Color.black;
    6.     public int size    = 1;
    7.        
    8.     void DrawLine(Vector2 pointA, Vector2 pointB,Color color,int size)
    9.     {
    10.         size = Mathf.Max(size,1);
    11.         Texture2D point = new Texture2D(1,1);
    12.         point.SetPixel(0,0,color);
    13.         point.Apply();
    14.         float distance = Vector2.Distance(pointA,pointB);
    15.         Rect rect = new Rect();
    16.         rect.size = new Vector2(size,size);
    17.         for(int i=0;i<=Mathf.RoundToInt(distance);i++){
    18.  
    19.             rect.position = Vector2.Lerp(pointA,pointB,i/distance);
    20.             GUI.DrawTexture(rect,point);
    21.  
    22.         }
    23.  
    24.     }
    25.    
    26.     void OnGUI(){
    27.        
    28.         DrawLine(A,B,color,size);
    29.        
    30.     }
    31.    
    32. }
    Open to any suggestions.