- Home /
Question by
rinfo · Jul 06, 2016 at 08:21 PM ·
inspectornullserializedpropertyserializableproperty drawer
Can a SerializedProperty have a null value in a CustomPropertyDrawer?
So, suppose I have a custom serializable class like this:
[System.Serializable]
public class MyClass {
public string someField, someOtherField; // just an example
} // note it's a class, not struct
And use it in a script:
public class MyScript : MonoBehaviour {
public MyClass testField = null;
public MyClass[] testArray = { null, new MyClass() };
} // note I want to differentiate null from empty instance
And I am making a custom property drawer for it:
[CustomPropertyDrawer(typeof(MyClass), true)] // true, because will have subclasses
public class MyClassDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
// property never seems to be null...
// also tried this:
var obj = base.fieldInfo.GetValue(property.serializedObject.targetObject);
Debug.Log(obj); // MyClass | MyClass[]
Debug.Log(obj==null); // False
}
}
How can I know when this property has a value of null, instead of empty?
I want to make a drawer which differentiates null (none) from instance with empty fields. I want to be able to set null in the inspector, or set a value if desired.
Is there a way to do this without changing implementation of MyClass?
Does the serialization accept null values even if you don't extend UnityEngine.Object (I would prefer to avoid that)?
Comment
It appears the serialization will not allow null values...
I just found out about ScriptableObject, maybe I'll change the implementation to use that ins$$anonymous$$d.