Hi guys, Google is polluted from topics on ScriptableObject ‘resetting on play’ (because you should set it dirty) - so I have a hard time finding good info on this:
I want to ‘reset’ a ScriptableObject to it’s default values - is that possible?
–background–
I am doing an Editor script and offer the user consistency through Play/stop/close/open Unity - by saving & loading a range of variables in a ScriptableObject in the background.
All is fine, but if my user would like to ‘reset’ the application, it’d be rather clean code if I could just ‘reset to default values’ - is that possible somehow? (I’m not including the ‘instancer’ in my build, but I can do a lot of hacks, but I just wonder if there’s a more clean way to simply ‘reset the object to default values’?
@Brankov
No, sorry, made the below - which appears to me to be an evil hack, but sort of does the job. One could also overwrite values one by one, but I like a ‘Reset’ to be as much of a ‘Reset’ as possible.
public static void resetToDefault()
{
AssetDatabase.DeleteAsset(refToSettingsFilePath);
CreateAsset<SettingsFile>();
Debug.Log("System X reset while running. This is fine, but will likely cause alerts from Unity above or below this line, they can just be ignored.");
}
You could put your data inside a serializable struct (inside the ScriptableObject) and when you want to trigger reset, reassign the struct with default construction.
[Serializable] struct Config {...}
// ...
m_Config = new Config();
I don;t think scriptable objects have a default value, I would just make a copy of it and name it default and then make your scriptable object = the default values, or put default values in the scriptableObject.