Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Horizontal Line in Editor Window

Discussion in 'Extensions & OnGUI' started by echologin, Mar 7, 2018.

  1. echologin

    echologin

    Joined:
    Apr 11, 2010
    Posts:
    935
    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:
    935
    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:
    7
    echologin likes this.
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    456
    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:
    114
    echologin likes this.
  6. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    766
    @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.

     
    MadalinStanciu, Aurko and echologin like this.
  7. elseforty

    elseforty

    Joined:
    Apr 3, 2018
    Posts:
    77
    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. Aurko

    Aurko

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