- Home /
Duplicate Question of http://answers.unity3d.com/questions/389986/expandable-property-with-propertydrawer.html
Statefull PropertyDrawer
Is there a way to have an PropertyDrawer in Unity 4.2 that has an serialized state?
For example, I try to make a custom PropertyDrawer that contains a simple Foldout. My first approach of just storing a member variable in the PropertyDrawer failed for two reasons: First the same drawer instance is shared between all drawers of the inspector (means, if I unfold one property, ALL are unfolded). And second, the bool is not persisted and reset on every assembly reload.
Example code:
[Serializable]
class Example
{
public string bla;
public string blub;
}
[CustomPropertyDrawer(typeof(Example))]
public class Example : PropertyDrawer
{
bool foldout;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position.height = 16;
if (foldout = EditorGUI.Foldout(position, foldout, new GUIContent(property.FindPropertyRelative("bla").stringValue)))
{
position.y += 16;
EditorGUI.LabelField(position, new GUIContent(property.FindPropertyRelative("blub").stringValue));
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{ return foldout ? 32 : 16; }
}
Of course, I could put the "bool foldout" into the class Example, but I rather like to separate Editor-GUI stuff from my game data.
Is there any way of having a PropertyDrawer that holds some meta information without putting this information in the inspected type itself?