I was wondering what the "correct" way to save scriptable objects in Unity that are changed during Play mode. So far I have something that works:
// changes
PartyMember Lalala = PartyMemberDictionary.Find("Lalala");
Lalala.HP += 17;
print("Lalala's HP: " + Lalala.HP);
// then saves
AssetDatabase.Refresh();
EditorUtility.SetDirty(Lalala);
AssetDatabase.SaveAssets();
Note: Lalala is a direct reference to a PartyMember scriptable object in my Assets/Resources folder.
I'm still a little shaky on this approach, however. I got this directly from reading this question:
But the commentator did not explain what each of these three lines of code mean.
AssetDatabase.Refresh();
EditorUtility.SetDirty(Lalala);
AssetDatabase.SaveAssets();
Can someone explain these to me? I'm not sure I understand them fully. I assume "dirty" indicates that data changed; please correct me if I'm wrong.
In this link
https://docs.unity3d.com/ScriptReference/AssetDatabase.SaveAssets.html
it asserts that SaveAssets() saves all unchanged asset changes to disk, so why do I need the other Refresh and SetDirty Functions?
In this link
http://answers.unity3d.com/questions/11531/why-doesnt-my-scriptableobject-save-using-a-custom.html
The OP says "as I found out after some trial and error AssetDatabase.SaveAssets doesn't actually save the asset as expected." ... so confused at this point. That leaves me hesitant to continue until I understand what's going on. Perhaps the Refresh and SetDirty cement the job?
Also, in this link
https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html
it mentioned that Undo.RecordObject is preferred. Based on your experiences, is this true? Is EditorUtility.SetDirty(); obsolete in this day and age already? I don't want to use it if I have to change my code in the future.
At the end of the day I simply want something that works.
ScriptableObject
s automatically store information during play mode. They don't store information between game sessions. And they don't delete data when loading between scenes. \$\endgroup\$