CustomPropertyDrawer for a concrete class of a generic abstract
Hi!
So I in pursue of a reference system that allows me to not hardcode things in the inspector I created a "FloatReference" class that holds a scriptableObjet variable so I can drag values from my project to my scene.
public class FloatReference : MonoBehaviour
{
public bool useConstant = true;
public float constValue;
public FloatVariable variable;
public float value
{
get { return useConstant ? constValue : variable.value; }
}
}
I create a custom property for convenience:
[CustomPropertyDrawer(typeof(FloatReference))]
public class FloatReferenceDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
SerializedProperty useConstant = property.FindPropertyRelative("useConstant");
SerializedProperty constValue = property.FindPropertyRelative("constValue");
SerializedProperty variable = property.FindPropertyRelative("variable");
EditorGUI.BeginProperty(position, label, property);
useConstant.boolValue = EditorGUI.ToggleLeft(r, "Use Constant", useConstant.boolValue);
r.xMin += 95;
r.xMax = position.xMax;
r.height = EditorGUIUtility.singleLineHeight;
// EXCEPTION HERE!
if (useConstant.boolValue)
{
/*
more code that worked before I made it derive from a generic class...
*/
}
}
So far so good. Then trying to refine my system I thought it would be awesome to get FloatReference class to derive from a generic abstract Reference class as follows:
public class Reference : MonoBehaviour { public bool useConstant = true; public T constValue; public Variable variable;
public T value
{
get { return useConstant ? constValue : variable.value; }
}
}
so my FloatReference, IntegerReference, ColorReference, etc get simplified to:
[Serializable]
public class FloatReference : Reference<float>
{
}
And then my custom property throws a null reference exception and I don't understand why.
Any help? thanks
Answer by Adam-Mechtley · Apr 11, 2018 at 06:52 PM
It's a little unclear here, but assuming that FloatReference
in fact inherits Reference<float>
, the problem here is that a FloatReference
field on some object is serializing a reference to a UnityEngine.Object
(since Reference<T>
inherits MonoBehaviour
). Unless I'm missing something, it looks like Reference<T>
should instead inherit System.Object
.
Explanation:
In Unity, when you have a serialized field to of a type that inherits from UnityEngine.Object
, you are serializing a reference to some object. Serializing a struct or a class that does not inherit from UnityEngine.Object
will serialize a copy of the object. You access SerializedProperties of fields on a copy using e.g., SerializedProperty.FindRelative()
. In the case of a reference, you access the reference itself via SerializedProperty.objectReferenceValue
. In order to access any of the fields on this reference, you then need to wrap it in a wholly separate serialized data stream (e.g., var so = new SerializedObject(property.objectReferenceValue);
), in which case you also need to properly manage updating and dirtying this object's serialized data stream and so on.
See the serialization documentation, specifically the section When might the serializer behave unexpectedly?
Answer by Pablomon · Apr 12, 2018 at 09:19 AM
Thank you Adam, that did it. But why? Doesn't Monobehaviour inherit from Object?
I've update my answer above with more info. If that clears it up you can mark it as the accepted answer :)