Search Unity

Custom Editor: Inspector Alignment

Discussion in 'Immediate Mode GUI (IMGUI)' started by marnsmith, Mar 14, 2015.

  1. marnsmith

    marnsmith

    Joined:
    Feb 7, 2013
    Posts:
    29
    I'm writing a custom editor and I'd like to align some properties up alongside EditorGUILayout.TextField(string label, string value), which draws a label on the left and some value on the right.
    inspector_alignment.png

    To emulate that behavior I'm trying something like this (simplified):

    Code (CSharp):
    1. EditorGUILayout.BeginHorizontal();
    2.  
    3. EditorGUILayout.BeginVertical(GUILayout.Width(Screen.width * 0.35f));
    4. EditorGUILayout.EnumPopup(enum_value);
    5. EditorGUILayout.EndVertical();
    6.  
    7. EditorGUILayout.BeginVertical(GUILayout.Width(Screen.width * 0.65f));
    8. EditorGUILayout.TextField(str_value);
    9. EditorGUILayout.EndVertical();
    10.  
    11. EditorGUILayout.EndHorizontal();
    The problem is that EditorGUILayout.TextField(string label, string text) etc. uses some unknown alignment algorithm that doesn't seem to be a simple scalar (as above).

    So: is there a good way to emulate that behavior, or if not... who knows what that alignment function is? ;)
     
    Last edited: Mar 15, 2015
  2. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Hey there,

    This made me so mad at one point. I tried like 50 different math equations to get it to line up, they all failed. Turns out if you use

    Code (CSharp):
    1. EditorGUIUtility.labelWidth
    for the width of your first element the other one will stretch and be correct.
     
    Kamilche_ likes this.
  3. marnsmith

    marnsmith

    Joined:
    Feb 7, 2013
    Posts:
    29
    Hmm... I just tried this, and it's pretty close!

    It looks to be giving me a width that stretches with the labelWidth with a mostly constant pixel offset. I tried

    Code (CSharp):
    1. EditorGUIUtility.labelWidth - 26
    Which on my machine looks good until the inspector window becomes small and the right column sticks out again.

    But this is great info and much better than before. Thanks!
     
  4. RandomPoint

    RandomPoint

    Joined:
    Jul 21, 2017
    Posts:
    14
    Try this:

    Code (csharp):
    1. EditorGUILayout.PrefixLabel("label text");
     
  5. VLukianenko

    VLukianenko

    Joined:
    Mar 27, 2017
    Posts:
    30
    Label width and prefix label thingie give ALMOST right result. Second element in the horizontal layout is offset just a little bit, which triggers my OCD X_x