- Home /
Property Drawer and Children
Hi guys,
I'm trying to fake a dictionary in the inspector using an enum and an array. I got to the point where it looks (mostly) correct but when I override GetPropertyHeight it covers the foldout's child and I can't edit their values. Does anyone know a solution to this? Thanks in advance.
ren
[CustomPropertyDrawer(typeof (NameListAttribute))]
public class NameListDrawer : PropertyDrawer
{
private NameListAttribute nameListAttribute { get { return ((NameListAttribute) attribute); }}
private bool foldout = false;
private const float ITEMSIZE = 15f;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (foldout)
{
return base.GetPropertyHeight(property, label) + ITEMSIZE * Enum.GetNames(nameListAttribute.names).Length;
}
else
{
return base.GetPropertyHeight(property, label);
}
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
foldout = EditorGUI.Foldout(position, foldout, label);
if (foldout)
{
string[] names = Enum.GetNames(nameListAttribute.names);
for (int i = 0; i < names.Length; i++)
{
Rect rect = EditorGUI.IndentedRect(position);
rect.y += 15*(i + 1);
rect.height = 15f;
EditorGUI.PropertyField(rect, property.GetArrayElementAtIndex(i), new GUIContent(names[i]));
}
}
}
}
Answer by OLP · Oct 02, 2013 at 06:41 PM
When overriding GetPropertyHeight, it sets the height
property of your Rect position
to the total height. All you have to do is reset that height before calling EditorGUI.Foldout.
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position.height = 16f;
foldout = EditorGUI.Foldout(position, foldout, label);
Answer by Deacon · Oct 31, 2013 at 05:28 PM
Just a side note, instead of using the foldout variable used the built in property.isExpanded variable to see if you have expanded the property you are working with. PropertyDrawers are not 1-to-1 with the properties they draw.
This helped me. Without this note, my drawers were working in such a way that if I had a list of instances of my class, expanding one instance expanded all of them. Looks like there is one drawer object that handles all instances of the class.
Thanks!
Answer by gardian06 · Sep 26, 2013 at 02:42 PM
you don't call GetPropertyHeight after foldout, and names are set (from what I have found if you want something really special to happen in GetPropertyHeight then you need to call it yourself in your PropertyDrawer as repainting is not done by default, or as often like with EditorWindows, or CustomInspectors
the other suggestion is instead of doing hard set increments of rect.y (and rect.height; why is this being set to 15 in every iteration of the for loop?)
//just right before your forloop do:
rect.height = GetPropertyHeight/names.Length;
//inside the for loop
rect.y += rect.height
Your answer
Follow this Question
Related Questions
Draw on Multiple Lines with EditorGUI 1 Answer
EditorGUI.Foldout consumes click so GUI.Button doesnt work when inside of foldout region. 1 Answer
What's the equivalent of EditorGUIUtility.currentViewWidth for a foldout property? 1 Answer
How to get the target object value from a PropertyDrawer for an array of objects? 1 Answer
EditorGUI.Foldout -- Cannot interact with contents! 1 Answer