Property Drawers In Serializable Classes - GetPropertyHeight

I love how unity allows us to specify PropertyDrawers in Serializable classes. For Example:

[Serializable] public class CustomClass
{
      [MyPropertyDrawer] public int A;
}

This works 100% and allows me to move part of editor validation logic and editor callbacks into the serializable classes instead the MonoBehaviour, resulting in more reusable code.

public class MonoDrawer : MonoBehaviour
{
    public CustomClass TestA; //works
    //implied usage of MyPropertyDrawer
    //for field TestA.A
}

Now imagine the following scenario, i have two [MyPropertyDrawer] nested as seen here.

public class StackDrawer : MonoBehaviour
{
    [MyPropertyDrawer]
    public CustomClass TestA;
    //+1 implied from CustomClass
}

Behaviour:

  1. All property drawers and validation logic 100% works even though it is nested.

  2. Bug: The Nested Property Drawers have wrong GetPropertyHeight(). I am using the default one.
    I think this must be an unintended effect from me “abusing” unity code in ways the designers didn’t foresee.

So my question is if there is a workaround for this issue. Since [MyPropertyDrawer] could be in any depth, nested unlimited times, it would be hard if not impossible to predict the final GetPropertyHeight(). Maybe using “static global variables” as a hack ?

There’s no bug here. If you have some custom property drawer that is intended to work with some type that has child properties (e.g., a custom struct or class) then you just need to implement GetPropertyHeight() correctly, as the default just returns a single line height. You may find my answer here helpful, but in short, it should suffice to simply use EditorGUI.GetPropertyHeight() inside your GetPropertyHeight() implementation.

I don’t quite understand your question. A property drawer is responsible for drawing “the one” property which it is related to. So if you want your property drawer to support custom classes you have to handle them properly. How is your PropertyDrawer actually implemented?

The “default” propertydrawer GetHeight method is implemented like this:

public virtual float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
	return 16f;
}

Of course you have to specify which height your property drawer requires. That means if you what to use it on a custom class and want that custom class to be displayed “normally” you have to calculate the required height inside GetPropertyHeight.