- Home /
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 ?
Answer by Adam-Mechtley · Nov 09, 2017 at 07:24 PM
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.
Ahh, damn, waited too long to submit my answer ^^.
I always forget this hidden PropertyDrawer stack inside the "propertyDrawer" property of the PropertyHandler. I still think this is a bad implementation that a line like this:
if (this.propertyDrawer != null)
will evaluate to true one time and to false another time, even though we use exactly the same objects every time.
So yes, you can use GetPropertyHeight inside your GetPropertyHeight method ^^.
Answer by Bunny83 · Nov 09, 2017 at 07:46 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612142623im_/https://answers.unity.com/themes/thub/images/avi.jpg)