PropertyDrawer: indent issue

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]

EditorGUI.Popup(new Rect(position.x, currentOffsetY, position.width / 3, position.height), idSelect, names);
EditorGUI.Popup(new Rect(position.x + position.width / 3, currentOffsetY, position.width / 3, position.height), predicateIndex, predicatesAvailable);
EditorGUI.PropertyField(new Rect(position.x + (2 * position.width / 3), currentOffsetY, position.width / 3, position.height), prop);

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.

Can you post the entire function which contains the piece of code you posted ?

Well it’s a bit heavy. The 3 code lines are at 51 / 67 / 73.

3024460–226037–GVConditionPropertyDrawer.cs (5.88 KB)

Try setting EditorGUI.indentLevel to 0 before drawing those properties.
Remember to record its value so you can set it back.

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;”

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:

//UnityEdtitor.dll
public static int Popup(Rect position, int selectedIndex, GUIContent[] displayedOptions, [DefaultValue("EditorStyles.popup")] GUIStyle style)
{
    return EditorGUI.DoPopup(EditorGUI.IndentedRect(position), GUIUtility.GetControlID(EditorGUI.s_PopupHash, FocusType.Keyboard, position), selectedIndex, displayedOptions, style);
}
1 Like

Hum it makes sense but why it can’t handle the width divided by 3 ? I’m not sure if I did something wrong :confused:

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.