3
\$\begingroup\$

How to draw a horizontal line in the inspector like on the reference image? Kind of separation line.

Horizontal line example

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$
public static class EditorGUILayoutUtility
{
    public static readonly Color DEFAULT_COLOR = new Color(0f, 0f, 0f, 0.3f);
    public static readonly Vector2 DEFAULT_LINE_MARGIN = new Vector2(2f, 2f);

    public const float DEFAULT_LINE_HEIGHT = 1f;

    public static void HorizontalLine(Color color, float height, Vector2 margin)
    {
        GUILayout.Space(margin.x);

        EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, height), color);

        GUILayout.Space(margin.y);
    }
    public static void HorizontalLine(Color color, float height) => EditorGUILayoutUtility.HorizontalLine(color, height, DEFAULT_LINE_MARGIN);
    public static void HorizontalLine(Color color, Vector2 margin) => EditorGUILayoutUtility.HorizontalLine(color, DEFAULT_LINE_HEIGHT, margin);
    public static void HorizontalLine(float height, Vector2 margin) => EditorGUILayoutUtility.HorizontalLine(DEFAULT_COLOR, height, margin);

    public static void HorizontalLine(Color color) => EditorGUILayoutUtility.HorizontalLine(color, DEFAULT_LINE_HEIGHT, DEFAULT_LINE_MARGIN);
    public static void HorizontalLine(float height) => EditorGUILayoutUtility.HorizontalLine(DEFAULT_COLOR, height, DEFAULT_LINE_MARGIN);
    public static void HorizontalLine(Vector2 margin) => EditorGUILayoutUtility.HorizontalLine(DEFAULT_COLOR, DEFAULT_LINE_HEIGHT, margin);

    public static void HorizontalLine() => EditorGUILayoutUtility.HorizontalLine(DEFAULT_COLOR, DEFAULT_LINE_HEIGHT, DEFAULT_LINE_MARGIN);

#if UNITY_EDITOR
#endif
}

Call:

public override void OnInspectorGUI()
{
    base.OnInspectorGUI();

    EditorGUILayout.BeginVertical();

    EditorGUILayoutUtility.HorizontalLine(new Vector2(20f, 20f));

    EditorGUILayout.EndVertical();
}

Will yield this result.

Draw Line Result

\$\endgroup\$
0
\$\begingroup\$

Another option is to use a slider like this (Source):

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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .