I’m really excited about the new [SerializeReference] feature, and I’m trying to write a custom property drawer for a serialised polymorphic field (the base class is abstract in fact, but I don’t think that makes a difference?). However, I’m not sure how to actually get the current value out. It seems that managedReferenceValue
only has a setter. How do I actually get access to the currently serialised object?
Another point of confusion: the new docs state that all of managedReferenceValue
, managedReferenceFullTypename
and managedReferenceFieldTypename
are write-only. However, it seems that actually only the value property is write-only, whereas the two type name properties are actually read-only? Is this a mistake in the docs?
The basic problem with managedReferenceValue is how to expose multiple different values if the serializedobject represents multiple targets.
What I did to check if the managedReferenceValue is null (so I can show a create entry of class A or B button) is to go through fieldInfo.
using
var value = fieldInfo.GetValue(property.serializedObject.targetObject)
Allows you to get the value of one targetObject, if you need to support targetObjects you will need to extend this and handle multiple values accordingly.
After the instance has been created you can just use FindPropertyRelative and the normal iteration stuff to access all fields of that serialized class.
1 Like
Awesome, thank you. I’m not really concerned about multi-object editing for now, so this should do. I’ll give this a shot later today.
Hmmm, one thing that’s slightly awkward with this solution is that my actual SerializedProperty is not a field of targetObject, but has a deeper propertyPath (i.e. it’s actually a field of one of the targetObject’s fields). Is there a clean way to get the actual object that contains the relevant field?
For now I’ll probably just split the propertyPath on periods and walk down the tree manually. Seems kinda clunky though.