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