Horizontal Line

Is there a way to draw an horizontal line using EditorGUI / EditorGUILayout?

alt text

Simple and better solution:

        EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

85783-screenshot-2.png


Update 20.01.2020: Also you can use NaughtyAttributes’ updated version(v2.0.0). That library has an HorizontalLine attribute like in the example.

alt text

Here is the solution that I use in my editor scripts:

static class CustomGUI {

    public static readonly GUIStyle splitter;

    static CustomGUI() {
        GUISkin skin = GUI.skin;

        splitter = new GUIStyle();
        splitter.normal.background = EditorGUIUtility.whiteTexture;
        splitter.stretchWidth = true;
        splitter.margin = new RectOffset(0, 0, 7, 7);
    }

    private static readonly Color splitterColor = EditorGUIUtility.isProSkin ? new Color(0.157f, 0.157f, 0.157f) : new Color(0.5f, 0.5f, 0.5f);
    
    // GUILayout Style
    public static void Splitter(Color rgb, float thickness = 1) {
        Rect position = GUILayoutUtility.GetRect(GUIContent.none, splitter, GUILayout.Height(thickness));

        if (Event.current.type == EventType.Repaint) {
            Color restoreColor = GUI.color;
            GUI.color = rgb;
            splitter.Draw(position, false, false, false, false);
            GUI.color = restoreColor;
        }
    }

    public static void Splitter(float thickness, GUIStyle splitterStyle) {
        Rect position = GUILayoutUtility.GetRect(GUIContent.none, splitterStyle, GUILayout.Height(thickness));

        if (Event.current.type == EventType.Repaint) {
            Color restoreColor = GUI.color;
            GUI.color = splitterColor;
            splitterStyle.Draw(position, false, false, false, false);
            GUI.color = restoreColor;
        }
    }

    public static void Splitter(float thickness = 1) {
        Splitter(thickness, splitter);
    }

    // GUI Style
    public static void Splitter(Rect position) {
        if (Event.current.type == EventType.Repaint) {
            Color restoreColor = GUI.color;
            GUI.color = splitterColor;
            splitter.Draw(position, false, false, false, false);
            GUI.color = restoreColor;
        }
    }

}

I like using EditorGUILayout, this worked well…

EditorGUILayout.TextArea("",GUI.skin.horizontalSlider);

Ok so it might be a bit ugly but :

public void displaySeparator()
{
	GUILayout.Label("_________________________________________________________________________________________________________________________________________________________________________");
}

I just found the full solution to this.

Using @Steven 1 method, but creating a custom gui skin, that setts all paddings to 1.

So first create a class that holds the custom style

//Class to hold custom gui styles
public static class MyGUIStyles
{
    private static GUIStyle m_line = null;

    //constructor
    static LOGDGUIStyles()
    {

        m_line = new GUIStyle("box");
        m_line.border.top = m_line.border.bottom = 1;
        m_line.margin.top = m_line.margin.bottom = 1;
        m_line.padding.top = m_line.padding.bottom = 1;
    }
    
    public static GUIStyle EditorLine
    {
        get { return m_line; }
    }
}

Then just call:

GUILayout.Box(GUIContent.none, GUIStyles.EditorLine , GUILayout.ExpandWidth(true), GUILayout.Height(1f));

make sure you use EditorGUIUtility.LookLikeInspector(); before hand

Here’s an approach that seems a bit simpler than the ones already posted.
Creates a Horizontal Rule element 2 pixels high and as wide as whatever container it’s in.

  GUIStyle styleHR = new GUIStyle(GUI.skin.box);
  styleHR.stretchWidth = true;
  styleHR.fixedHeight = 2;
  GUILayout.Box("", styleHR);

http://forum.unity3d.com/threads/17066-How-to-draw-a-GUI-2D-quot-line-quot?p=230386#post230386

Something thats EditorGUI friendly and works:

EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 1f),"", EditorStyles.helpBox);

GUI.skin.GetStyle(“IN Title”).Draw(position,GUIContent.none,0,false)

draws separator line like in GameObjectInspector components title

I added padding to @sirjoan620 answer:

public void DrawLine(float padding)
    {
        GUILayout.Space(padding/2);
        EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
        GUILayout.Space(padding/2);
    }

Style:

public class GUIStyles
{
	private static GUIStyle _separatorStyle;
	public static GUIStyle SeparatorStyle
	{
		get
		{
			if (_separatorStyle == null)
			{
				_separatorStyle = new GUIStyle("box");
				_separatorStyle.normal.background = CreateColorPixel(Color.gray);
				_separatorStyle.stretchWidth = true;
				_separatorStyle.border = new RectOffset(0, 0, 0, 0);
				_separatorStyle.fixedHeight = 1f;
			}
			return _separatorStyle;
		}
	}

	public static Texture2D CreateColorPixel(Color color)
	{
		Texture2D texture = new Texture2D(1, 1);
		texture.SetPixel(0, 0, color);
		texture.Apply();
		return texture;
	}
}

Usage:

GUILayout.Space(10f);
GUILayout.Box("", GUIStyles.SeparatorStyle);
GUILayout.Space(10f);

Use Header

public int one;
public int two;
[Header("__________________________________")]
public int three;
public int four;

You also might like Space

public int one;
public int two;
[Space(10)]
public int three;
public int four;