Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Horizontal Line in Editor Window

Discussion in 'Immediate Mode GUI (IMGUI)' started by echologin, Mar 7, 2018.

  1. echologin

    echologin

    Joined:
    Apr 11, 2010
    Posts:
    973
    How do you make Horizontal lines like in Unity's lighting window in an EditorWindow
    I found a few ways by searching but none look as good as the lighting window

    Screen Shot 2018-03-07 at 2.26.17 PM copy.png
     
  2. echologin

    echologin

    Joined:
    Apr 11, 2010
    Posts:
    973
    Well I found a solution if anyone has a better idea Im all ears



    //============================================================
    void GuiLine( int i_height = 1 )
    {
    Rect rect = EditorGUILayout.GetControlRect(false, i_height );
    rect.height = i_height;
    EditorGUI.DrawRect(rect, new Color ( 0.5f,0.5f,0.5f, 1 ) );
    }
     
  3. Knarhoi

    Knarhoi

    Joined:
    Apr 11, 2015
    Posts:
    9
    echologin likes this.
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    591
    I'm in favor of the GUIStyle method. Plays well with layouts and is highly configurable (adjusting margins or width is all done at the style level).

    Code (CSharp):
    1. // create your style
    2. GUIStyle horizontalLine;
    3. horizontalLine = new GUIStyle();
    4. horizontalLine.normal.background = EditorGUIUtility.whiteTexture;
    5. horizontalLine.margin = new RectOffset( 0, 0, 4, 4 );
    6. horizontalLine.fixedHeight = 1;
    7.  
    8. // utility method
    9. static void HorizontalLine ( Color color ) {
    10.     var c = GUI.color;
    11.     GUI.color = color;
    12.     GUILayout.Box( GUIContent.none, horizontalLine );
    13.     GUI.color = c;
    14. }
    15.  
    16. // use it!
    17. GUILayout.Label( "Some content above" );
    18.  
    19. MyGUI.HorizontalLine( Color.grey );
    20.  
    21. GUILayout.Label( "Some content below" );
     
    Last edited: May 31, 2018
    echologin likes this.
  5. StylishCoding

    StylishCoding

    Joined:
    Apr 21, 2018
    Posts:
    139
    Yumera and echologin like this.
  6. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    903
    @echologin I use this for my editor windows.

    Code (CSharp):
    1. public static void DrawUILine(Color color, int thickness = 2, int padding = 10)
    2. {
    3.     Rect r = EditorGUILayout.GetControlRect(GUILayout.Height(padding+thickness));
    4.     r.height = thickness;
    5.     r.y+=padding/2;
    6.     r.x-=2;
    7.     r.width +=6;
    8.     EditorGUI.DrawRect(r, color);
    9. }
    It looks pretty good.

     
  7. elseforty

    elseforty

    Joined:
    Apr 3, 2018
    Posts:
    129
    i use this , the 15 value is used to get the line to draw from the both extreme sides of the inspector ,

    Code (CSharp):
    1.         var rect = EditorGUILayout.BeginHorizontal();
    2.         Handles.color = Color.gray;
    3.         Handles.DrawLine(new Vector2(rect.x - 15, rect.y), new Vector2(rect.width + 15, rect.y));
    4.         EditorGUILayout.EndHorizontal();
    5.         EditorGUILayout.Space();
     
    echologin likes this.
  8. auleek_dev

    auleek_dev

    Joined:
    Feb 23, 2014
    Posts:
    2
    Very nice solution, Thanks. :D
     
    echologin and alexanderameye like this.