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
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
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
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.