ApplyModifiedProperties() ignored on nested serialized property w custom editor and custom property drawer
Hi, I am a bit lost:
I have written a custom editor which draws a serializable property sp, but all values I assign to sp vanish on play. Importantly, the serialized property is the first child of an array that itself is a serialized property. The serialized property sp is drawn with a custom property drawer.
I have added
sp.Update()
andsp.ApplyModifiedProperties()
before and after modifying the serialized property sp in the editor scriptThe custom editor script includes
EditorUtility.SetDirty(t)
at the end as well asUndo.RecordObject(t, "xxx")
on the target object t.
What could go wrong here?
Any help would be very much appreachiated!
The screenshot shows the custon editor. If I click play, the fields "nuts", "berries" (...) disappear. Anything else that is not part of the seralized property stays.
This is the Feature Class that I want to assign values to
[System.Serializable]
public struct Feature
{
public string Name;
public string Label;
public Sprite[] FeatureValues;
public string[] FeatureValueLabels;
}
It is the child of another class, "NestedFeatures"
[System.Serializable]
public class NestedFeature
{
[HideInInspector]
public string Name;
[HideInInspector]
public string Label;
[HideInInspector]
public GameObject Prefab;
public Feature[] Features;
}
I draw them in a custom editor for the "StimulusManager"
[CustomEditor(typeof(StimulusManager))]
public class StimulusManagerEditor : Editor
{
void OnEnable()
{
_target = (StimulusManager) target;
_targetSerial = new SerializedObject(target);
}
public override void OnInspectorGUI()
{
// save changes from the editor in the game
_targetSerial.Update();
Undo.RecordObject(_target, "Define stimulus");
// HERE THE NESTED PROPERtY IS DRAWN BUT THE VALUES ARE NOT KEPT
// Get the property field as child from the NestedFeatures property
_featuresProperty = _targetSerial.FindProperty("NestedFeatures.Array.data[0]"); //Get the property
EditorGUILayout.PropertyField(_featuresProperty, true); //draw the property
// Keep changes
_targetSerial.ApplyModifiedProperties();
EditorUtility.SetDirty(_target);
EditorApplication.update.Invoke();
}
The custom property drawer is this
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer (typeof (NestedFeature))]
public class NestedFeatureDrawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
// Get the array called Features in the NestedFeature class
SerializedProperty FeaturesProperty = property.FindPropertyRelative("Features");
EditorGUI.BeginProperty(position, label, property);
var indent = EditorGUI.indentLevel; //store old indent
EditorGUI.indentLevel = 0; //new indent
// HERE SOMETHING GOES WRONG, THE CONTENT OF THIS PART VANISHES ON PLAY
for (int j=0; j < FeaturesProperty.arraySize; j++)
{
EditorGUILayout.PropertyField(FeaturesProperty.GetArrayElementAtIndex(j), GUIContent.none, true);
}
EditorGUI.indentLevel = indent; //reset indent
EditorGUI.EndProperty();
}
}
Answer by TonyLi · Jun 21, 2017 at 04:10 PM
The Editor class already comes with a serializedObject reference, so you can simplify it to:
[CustomEditor(typeof(StimulusManager))]
public class StimulusManagerEditor : Editor
{
public override void OnInspectorGUI()
{
// save changes from the editor in the game
serializedObject.Update();
// HERE THE NESTED PROPERTY IS DRAWN BUT THE VALUES ARE NOT KEPT
// Get the property field as child from the NestedFeatures property
_featuresProperty = serializedObject.FindProperty("NestedFeatures.Array.data[0]"); //Get the property
EditorGUILayout.PropertyField(_featuresProperty, true); //draw the property
// Keep changes
serializedObject.ApplyModifiedProperties();
EditorApplication.update.Invoke();
}
}
If something else is going on in that script, this streamlining may resolve the problem.
If not, what happens if you disable your NestedFeatureDrawer class temporarily and let Unity use the default editor for NestedFeature? Does it work correctly?
Your answer
Follow this Question
Related Questions
SetDirty not working in Custom Editor with Nested Custom Property drawer 1 Answer
Inheritance, List and CustomPropertyDrawer 0 Answers
No type in ObjectField created in uxml (UIElements) 3 Answers
How can I modify 'SerializedProperty' that contain 'Array' in Custom inspector? 0 Answers
Inheritance, List and PropertyDrawer,CustomPropertyDrawer and Inheritance 0 Answers