- Home /
How to correctly use [SerializeReference] on generic types
Once again I'm working on yet another EditorTool for Unity which kinda makes me go insane (again).
So I have the following setup:
[Serializable]
public class OuterClass<T>
{
[SerializeReference] private InnerClass[] innerClasses;
[Serializable]
public class InnerClass
{ }
}
public class OuterClassDrawer<T, TClass>
where TClass : OuterClass<T>
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
SerializedProperty innerClassesProperty = property.FindPropertyRelative("innerClasses");
for (int i = 0; i < innerclassesProperty.arraySize; i++) // <-- NullReferenceException
...
}
}
Of course I also implement a version of something like "OuterClassBoolDrawer : OuterClassDrawer" and "OuterClassBool : OuterClass"
But obviously the property.FindPropertyRelative() does not work.
But when I now change the [SerializeReference] to [SerializeField] it suddenly works.
I've tried even to move the nested class outside (which needs the same generic argument tho), which reproduces the same behaviour. Sadly I do have to use the SerializeReference, because I need to access the "managedReferenceValue" within the PropertyDrawer at another point.
Any suggestions how I can properly serialize a generic class like that?