Search Unity

Drawing lines in the editor

Discussion in 'Scripting' started by Linusmartensson, Dec 26, 2010.

  1. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    No, I don't have - and have no reason to get - 4.6.
     
  2. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    I confirm DrawLine is broken since unity 5. The lines have an offest on the x axis.

    hint: The offest increase with the line lenght, maybe something with the scaling...
     
    Last edited: May 9, 2015
  3. LTK

    LTK

    Joined:
    Jul 16, 2015
    Posts:
    24
    Thanks you very much ! :)
     
  4. Bovine

    Bovine

    Joined:
    Oct 13, 2010
    Posts:
    195

    Attached Files:

  5. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Very nice!
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Does that work in custom editor windows (OnGUI)? The docs make it sound like it's all about drawing in the scene view in 3D.
     
  7. TH_Unity

    TH_Unity

    Joined:
    Jul 9, 2013
    Posts:
    11
    I optimized the method further more. I think this should work. Tell me if this works properly :)

    Code (CSharp):
    1.  
    2. public static class EditorGUIx
    3. {
    4. private static Texture2D s_lineTex;
    5.  
    6. static EditorGUIx()
    7. {
    8. s_lineTex = new Texture2D( 1, 3, TextureFormat.ARGB32, true );
    9. s_lineTex.SetPixel( 0, 0, new Color( 1, 1, 1, 0 ) );
    10. s_lineTex.SetPixel( 0, 1, Color.white );
    11. s_lineTex.SetPixel( 0, 2, new Color( 1, 1, 1, 0 ) );
    12. s_lineTex.Apply();
    13. }
    14.  
    15. public static void DrawLine( Vector2 p_pointA, Vector2 p_pointB, float p_width )
    16. {
    17. Matrix4x4 _saveMatrix = GUI.matrix;
    18. Color _saveColor = GUI.color;
    19.  
    20. Vector2 _delta = p_pointB - p_pointA;
    21. GUIUtility.ScaleAroundPivot( new Vector2( _delta.magnitude, p_width ), Vector2.zero );
    22. GUIUtility.RotateAroundPivot( Vector2.Angle( _delta, Vector2.right ) * Mathf.Sign( _delta.y ), Vector2.zero );
    23. GUI.matrix = Matrix4x4.TRS( p_pointA, Quaternion.identity, Vector3.one ) * GUI.matrix;
    24.  
    25. GUI.DrawTexture( new Rect( Vector2.zero, Vector2.one ), s_lineTex );
    26.  
    27. GUI.matrix = _saveMatrix;
    28. GUI.color = _saveColor;
    29. }
    30. }
    31.  
     
    Last edited: Apr 16, 2016
  8. AndreiMarian

    AndreiMarian

    Joined:
    Jun 9, 2015
    Posts:
    77
    Can this be used for ingame lines? I've tried
    Code (CSharp):
    1. public Vector2[] points = { new Vector2(0.25f, 0.5f), new Vector2(0.33333f, 0.1f)};
    then calling
    Code (CSharp):
    1. EditorGUIx.DrawLine(points[0], points[1], 1f);
    from OnPostRender() and OnGUI() but nothing shows up.
    I know I can use `Handles` but I'm interested in drawing thousands of lines per frame so efficiency is my primary concern. The comparison chart is convincing :) I also know about Vectrosity but 1. I'm not willing to pay and 2. I want to use the simplest code that I can also understand.
     
  9. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    It's always cheaper to pay than to rewrite.
     
  10. AndreiMarian

    AndreiMarian

    Joined:
    Jun 9, 2015
    Posts:
    77
    Yeah but I guess this Drawing class is way better than Vectrosity.
    Damn, I accidentally moved the GO from under the canvas... Not it works. In fact both Drawing and EditorGUIx.
     
  11. rraallvv

    rraallvv

    Joined:
    Jul 30, 2013
    Posts:
    16
    Has anyone uploaded the source to a repo on github.com?
     
  12. AndreiMarian

    AndreiMarian

    Joined:
    Jun 9, 2015
    Posts:
    77
    Note that Drawing is broken in Unity 5. See few posts above.
     
  13. rraallvv

    rraallvv

    Joined:
    Jul 30, 2013
    Posts:
    16