Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I get indentLevel to work with Buttons?

Discussion in 'Scripting' started by SmushyTaco, May 10, 2020.

  1. SmushyTaco

    SmushyTaco

    Joined:
    Apr 7, 2020
    Posts:
    88
    So I want to indent some buttons in my Editor class for my custom inspector but when I do:
    Code (CSharp):
    1. EditorGUI.indentLevel++;
    2. if (GUILayout.Button("Example"))) {
    3.     // Unrelated code
    4. }
    5. EditorGUI.indentLevel--;
    Nothing changes, I assume it's because the button is from the GUILayout class and the indentLevel is from the EditorGUI class so what exactly am I supposed to do to get the effect I'm looking for?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    starikcetin, sama-van and SmushyTaco like this.
  3. SmushyTaco

    SmushyTaco

    Joined:
    Apr 7, 2020
    Posts:
    88
    Got it working perfectly with this thanks! For anybody else needing this 15 pixels is how much each indent is.
     
    PraetorBlue likes this.
  4. pauldrummond

    pauldrummond

    Joined:
    Oct 30, 2014
    Posts:
    145
    You can use the current value of EditorGUI.indentLevel to set the left margin on a GUIStyle which is then applied to your button:

    Code (CSharp):
    1. GUIStyle buttonStyle = new GUIStyle(EditorStyles.miniButton);
    2. buttonStyle.margin = new RectOffset(EditorGUI.indentLevel * 15, 0, 0, 0);
    3. if (GUILayout.Button("Button Label", buttonStyle)) {...}