Hi, I want to use my custom serializable class with custom property attributes, like this :
[TestProperty("hi")]
public TestSerializableClass test_class;
But if I do this, it seems like unity doesn’t call Custom propertydrawer for serialized class.
I made a test codes to simplify my problems -
- Test Serializable Class
[Serializable]
public class TestSerializableClass
{
public string hi;
public string hi_hidden;
}
- Custom PropertyDrawer for Test Class
[CustomPropertyDrawer(typeof(TestSerializableClass), true)]
public class TestSerializableClassDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
EditorGUI.PropertyField(position, property.FindPropertyRelative("hi"));
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return base.GetPropertyHeight(property, label) * 3f;
}
}
- Custom Property Attribute
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property |
AttributeTargets.Class | AttributeTargets.Struct, Inherited = true)]
public class TestPropertyAttribute : PropertyAttribute
{
public string hi;
public TestPropertyAttribute(string hi)
{
this.hi = hi;
}
}
- Custom Property Attribute Drawer
[CustomPropertyDrawer(typeof(TestPropertyAttribute))]
public class TestPropertyAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
TestPropertyAttribute attr = (TestPropertyAttribute)attribute;
EditorGUI.PropertyField(position, property, label, true);
}
//Also the height is messed up, anyway overriden to debug show what's going on.
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return base.GetPropertyHeight(property, label) * 3;
}
}
So there are two property drawers : one for the attribute, one for the serializable class.
And during the process, I realized that only custom drawer for attribute is called when I use both together. That’s the reason I used EditorGUI.PropertyField() in line 8, to use PropertyDrawer also for serializable class.
But it seems like this line doesn’t call my custom propertydrawer!
Here’s my test script :
[TestProperty("Hi")]
public TestSerializableClass test;
[Space(10)]
public TestSerializableClass test_two;
Here’s how it looks :
So, how can I use my custom propertydrawer for my serialized class, not the unity default propertydrawer?
Thanks for your answer in advance!