Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

PropertyDrawer: indent issue

Discussion in 'Immediate Mode GUI (IMGUI)' started by xXApOXx, Apr 7, 2017.

  1. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    Hi,

    I'm currently struggling with a weird issue on my property drawer.

    I have this 3 lines of code to make an editor that looks like: [Variable][Predicates][Value]. For example I'll have on my inspector: [Apple][Equals][3]

    Code (csharp):
    1.  
    2. EditorGUI.Popup(new Rect(position.x, currentOffsetY, position.width / 3, position.height), idSelect, names);
    3. EditorGUI.Popup(new Rect(position.x + position.width / 3, currentOffsetY, position.width / 3, position.height), predicateIndex, predicatesAvailable);
    4. EditorGUI.PropertyField(new Rect(position.x + (2 * position.width / 3), currentOffsetY, position.width / 3, position.height), prop);
    5.  
    So I took the position.width and I divided by 3 to make that happen. Pretty simple but I can't figured out why it's getting buggy when the inspector indent my drawer.



    It works only for the last one when there is no indentation.
     
  2. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Can you post the entire function which contains the piece of code you posted ?
     
  3. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    Well it's a bit heavy. The 3 code lines are at 51 / 67 / 73.
     

    Attached Files:

  4. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,306
    Try setting EditorGUI.indentLevel to 0 before drawing those properties.
    Remember to record its value so you can set it back.
     
  5. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    Hum it fix the issue but the 3 fields are drawn like the last one on my screenshot, so it's less readable.

    I have a quick solution but I'm not really satisfied to not understand why it doesn't work in the first place. So I calculated myself the gap between fields and I "adjust" the position from my fields. Here it's the gap adjustement "float adjust = EditorGUI.indentLevel * 7.5f;"
     
    Last edited: Oct 1, 2021
  6. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,306
    Unity is Indenting the rect automatically for you. If you have access to a C# decompiler, like jetBrains dotPeek. You can see the internal method indents the supplied rectangle:

    Code (CSharp):
    1. //UnityEdtitor.dll
    2. public static int Popup(Rect position, int selectedIndex, GUIContent[] displayedOptions, [DefaultValue("EditorStyles.popup")] GUIStyle style)
    3. {
    4.     return EditorGUI.DoPopup(EditorGUI.IndentedRect(position), GUIUtility.GetControlID(EditorGUI.s_PopupHash, FocusType.Keyboard, position), selectedIndex, displayedOptions, style);
    5. }
     
    Tallek likes this.
  7. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    Hum it makes sense but why it can't handle the width divided by 3 ? I'm not sure if I did something wrong :/
     
  8. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Maybe when it divides by 3, the result is not a whole number, but you can't have a third of a pixel? Try casting to int? Just a guess.