Is it possible to update/serialize the values of the underlying class in a PropertyDrawer
without using BeginProperty
/EndProperty
or PropertyField
?
I am in the process of converting a portion of a CustomEditor
, which contained editor functionality for the class in question. I want my class to always use the same editor, but gave up on it a long time ago due to the complexity of converting to a property drawer. There are functions that are used at runtime and in the editor that I would like to avoid duplicating in the PropertyDrawer
ui.
Object reference:
var myObject = (MyClass)fieldInfo.GetValue(property.serializedObject.targetObject);
Attempt:
// Size Property
//
EditorGUI.BeginChangeCheck();
EditorGUI.PropertyField(position, sizeProperty);
if (EditorGUI.EndChangeCheck())
{
var newX = xProperty.intValue;
var newY = yProperty.intValue;
myObject.SetSize(newX, newY); // <- "myObject" is the object being drawn by the PropertyDrawer
}
I tried moving the function call to inside of BeginProperty
/EndProperty
of the class which the property drawer is for, with no change. This does not update the serialized data, even though I am directly modifying the reference. It seems like unity rebuilds the serialized data from the SerializedProperty
objects it creates for the object type. The function being called manipulates fields of the underlying object (fields being changed do not use PropertyField
or BeginProperty
/EndProperty
).
Is there a way to bypass that mechanism so that my logic does not need to be rewritten in the editor UI?
Calling Update()
on the serialized property resolved the issue.
// Size Property
//
EditorGUI.BeginChangeCheck();
EditorGUI.PropertyField(position, sizeProperty);
if (EditorGUI.EndChangeCheck())
{
var newX = xProperty.intValue;
var newY = yProperty.intValue;
myObject.SetSize(newX, newY);
property.serializedObject.Update(); // <- Update the underlying object after change
}
I had tried this before and it didn't work. The call to update was made after other property field(s), which may have caused the problem.