PropertySpace attribute draws a space of 8 pixels before a field when the SpaceBefore property is 0.

Issue #481 resolved
Michael Ryan created an issue

Windows 10, Unity 2018.3.0, Odin 2.0.13, Editor Only mode is enabled.

From the Odin PropertySpaceAttribute documentation:

The PropertySpace attribute have the same function as Unity's existing Space attribute, but can be applied anywhere as opposed to just fields.

  • float SpaceAfter The space between properties in pixels.
  • float SpaceBefore The space between properties in pixels.

When specifying 0 for the SpaceBefore, it is expected there to be NO space drawn before the associated property.

One valuable use of the PropertySpace attribute is that we can use it to draw spaces after the associated property. It's not unusual, to want a space after a property, but not before it.

This should do the trick:

[PropertySpace(0, 8)]

Instead, although a SpaceBefore of 0 is specified, a space with an 8-pixel height is actually drawn.

Looking at the drawing code, it's clear that the code is drawing a standard 8-pixel space specifically when a height of 0 is specified.

protected override void DrawPropertyLayout(GUIContent label)
{
  if (this.drawSpace)
  {
    PropertySpaceAttribute attribute = this.Attribute;
    if ((double) attribute.SpaceBefore == 0.0)
      EditorGUILayout.Space();
    else
      GUILayout.Space(attribute.SpaceBefore);
  }
  this.CallNextDrawer(label);
  if ((double) this.Attribute.SpaceAfter == 0.0)
    return;
  GUILayout.Space(this.Attribute.SpaceAfter);
}

It seems to me, the method should look more like this:

protected override void DrawPropertyLayout(GUIContent label)
{
  if (this.drawSpace)
  {
    PropertySpaceAttribute attribute = this.Attribute;
    if ((double) attribute.SpaceBefore > 0.0)
      GUILayout.Space(attribute.SpaceBefore);
  }
  this.CallNextDrawer(label);
  if ((double) this.Attribute.SpaceAfter > 0.0)
    GUILayout.Space(this.Attribute.SpaceAfter);
}

At this time, we can work around this issue by specifying a positive near-zero value (i.e., 0.0001f), but that really shouldn't be necessary.

If the existing behavior is the intended design, the documentation (linked above) should be updated.

Honestly, if someone specifies a value of 0 for the SpaceBefore, it's seems highly unlikely that they actually want a space of 8 pixels.

This certainly seems like a bug.

For the record, Unity's SpaceAttribute also draws a space of 8 pixels when a height of 0 is specified, but that should also be considered a bug. There should be no reason for Odin to replicate Unity's broken handling of the SpaceAttribute when it comes to the PropertySpaceAttribute. Unity would be better off generating a warning when a value less than 1 is passed to SpaceAttribute, as it make no sense to use the attribute with a height of less than 1. Forcing it to 8 is just confusing (and undocumented).

With regards to PropertySpaceAttribute, this are very good reasons to observe the SpaceBefore value of 0 by not drawing the at all space before the property.

Comments (2)

  1. mSkull001

    As you seem to have guessed, yes, this was replicated by the PropertySpace attribute because it was the behaviour of Unity's Space attribute.

    But, yes, it is a rather silly thing to replicate, and thus I've removed it now. PropertySpace should now work as expected.

  2. Log in to comment