- Home /
The question is answered, right answer was accepted
Asset variables needs [SerializeField] to be saved when reopening Unity.
Is this the intended behaviour? I've been creating my own custom Editor Windows, with the help of which I edit the ScriptableObject asset.
I do know about the EditorUtility.SetDirty(asset)
and I use it every single time the custom editor makes changes, but when I didn't have [SerializeField]
attribute in front of the variables (and [System.Serializable] on the class), it just refused to save.
Answer by Adam-Mechtley · Feb 24, 2017 at 08:16 AM
Yes this is expected. You may find the page in the docs on Script Serialization helpful.
Moreover, it is recommended that your custom editor display these fields using controls with the SerializedProperty class (via the Editor.serializedObject property) rather than directly modifying the field on the inspected target, as SerializedProperty will automatically take care of dirtying, undo/redo, multi-editing, and so on. You can search around this site for more info if you run into issues with it, but the basic pattern is something like this:
public override void OnInspectorGUI()
{
this.serializedObject.Update();
// your controls here, like e.g., EditorGUILayout.PropertyField(this.serializedObject.FindProperty("someField"));
this.serializedObject.ApplyModifiedProperties();
}
Gotcha. Will read up more on all of it, just started messing around with all this recently, got loads to learn. Thank you!
Follow this Question
Related Questions
EditorWindow: How to Serialize variables after PLay 1 Answer
Why does my Unity Serialization not work? Do i miss something? 1 Answer
How do I wait until after AssetDatabase creates an assets before carrying out another function ? 0 Answers
Editor Window Serialization not Working as Expected 2 Answers
Help with Missing Monobehaviours and Asset Serialization? 0 Answers