- Home /
Editor: How to save changes to a ScriptableObject loaded from Resources?
I'm making a complex custom inspector and I want to be able to save its settings to different profiles. So I've made a ScriptableObject to store the profiles. I can load the ScriptableObject just fine:
var settings = Resources.FindObjectsOfTypeAll<SettingsProfiles>();
if (settings != null)
_savedSettings = settings[0];
With that, I can read in all the stored settings and I can make changes to them just fine. However, any changes I make are always lost. All of my searches seem to indicate that this is the answer:
EditorUtility.SetDirty(_savedSettings);
AssetDatabase.SaveAssets();
But that doesn't seem to do anything at all. I've been searching for hours but I can't find an answer.
Does anyone know how this is supposed to work?
Thanks!
I'm absolutely not sure, but I think this might have to do with not registering for Undo. How do you edit values of your ScriptableObject?
If you modify it using a SerializedProperty there really shouldn't be any problem as Undo is handled by Unity.
If you set properties by casting it to your ScriptableObject and modify its properties/variables directly, then I believe you need to call Undo.RecordObject before making your changes, in order to let Unity actually set it dirty whenever you call SetDirty after changing. (so call Undo.RecordObject before changing, and EditorUtility.SetDirty afterwards). Also, if this is true then if you don't change anything, make a change and then manually save your project. The change should actually persist already ;)
Answer by Bunny83 · Mar 13, 2016 at 03:18 PM
That should work. I recently made this ResourceDB which also uses those two lines to actually commit any changes. Keep in mind that when using SerializedObject / SerializedProperties (like the normal inspector does) it should commit the changes whenever you call ApplyModifiedProperties which you usually call at the end when done with the GUI that could possible change something. However if you don't use a SerializedObject you can ignore that.
Have you tried setting the asset serialization mode to "force text" and have a look at the actual serialized data? Are your changes visible there? When do you actually call SetDirty and SaveAssets? How do you actually change your class?
Ohh and finally the most important thing ^^: What data is it that you want to save? Keep in mind that ScriptableObjects are limited to the same serialization rules as any other object in Unity. So the type(s) you want to serialize have to be supported by Unity's serializer. A generic dictionary for example isn't supported.
Also keep in mind that assets can't reference things in the scene, only other assets. Things in scenes aren't always available, only assets are. So assets can only reference other assets but things in a scene can reference both, other things in the same scene as well as assets.
Your answer
Follow this Question
Related Questions
How should I serialize data that is also editable in the Inspector? 2 Answers
Prevent changes in ScriptableObject type Asset in Editor. Dont save it. 0 Answers
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers
Referencing / linking a .asset / .prefab file in another .asset / .prefab file programmatically. 2 Answers