I’ve been experimenting with the new [SerializeReference] attribute and polymorphic serialization and wanted to make it possible to select a type from a dropdown for a field marked with [SerializeReference], since currently nothing is drawn unless a value is serialized in the field.
I’ve created a new attribute called [SelectType] with its own attribute drawer that succeeds in doing just that when added next to a [SerializeReference] attribute.
However I’ve an issue I don’t think I can solve without some Unity dev insight; I can switch between types but cannot figure out how to set the referenced object type back to null, which is also a valid value.
When setting the property.managedReferenceValue to a selected type I can do:
property.managedReferenceValue = FormatterServices.GetUninitializedObject(selectedType);
but doing:
property.managedReferenceValue = null;
throws a null reference exception.
What value can I set it to to have it become null again, is there some internal type that functions as null? Or is there some other way?
I’ve also still got the following error coming up, though the undo does actually work:
Not reporting since there’s already a thread.
I’ve attached a unity package with the required scripts in it.
Would it be helpful if I reported bugs I’m experiencing while implementing this? I don’t see them on the bug tracker yet.
Or is it still so much a work-in-progress that it makes little sense to report my issues as bugs?
I’ve accumulated a few, as can be experienced in the attached updated package:
Accessing serializedProperty.type creates an infinite loop
Adding an array or list element to a collection with an existing entry or duplicating the existing entry copies the entry’s reference but should make a deep copy of the referenced object instead
If a non-collection field is present that also has the [SerializeReference] attribute then elements added to an empty list or array will become references to that field
When the aforementioned makes a copy of a reference to an object that has non-default values Unity locks up
List and arrays marked with [SerializeReference] are drawn too far to the left, causing the foldout symbol to be cut-off
Manipulating a SerializeReference object in a custom inspector produces an error related to undo (‘Generating diff of this object for undo because the type tree changed.’), even though undo does work when implemented using:
Undo.RegisterCompleteObjectUndo(property.serializedObject.targetObject, “selected type change”);
As mentioned, cannot set SerializedProperty.managedReferenceValue to null, it produces a null ref exception
Very excited about this feature, keep up the good work!
PS: Apologies for using Unity 2020.1.0a3 while working on this even though I’m posting in the 2019.3 beta forum, if this post could be moved to the correct forum then please go ahead.
I started hacking a generic editor, similar to yours, the other day and hit a lot of these issues and several more.
Thanks for the feedback.
Do report the issues you hit, the more bug reports the better. There’s always a chance that two bug reports may sound the same but be different enough to need a different fix.