Changes to asset data persist - unable to discard changes
Hi All, I am writing an editor window which loads a scriptable object, allows you to modify the data and save it. This all works brilliantly however, if I change the data and discard the changes by closing the editor window - the data persists!
My loading method is...
// LevelData is a scriptableobject - being called from the Inspector via a button
public static void ShowWindow(LevelData levelData)
{
LevelBuilderWindow window = GetWindow<LevelBuilderWindow>(true, "Level Builder", true);
window.levelData = AssetDatabase.LoadAssetAtPath<LevelData>(AssetDatabase.GetAssetPath(levelData));
window.Show();
GUI.FocusWindow(window.GetInstanceID());
}
When the editor window is disabled, I try to discard the changes as much as possible, so when it opens again it reads the data from the asset file.
private void OnDisable()
{
chapterData = null;
levelData = null;
AssetDatabase.Refresh();
}
It's like the editor won't reference the asset from the file again until it closes and opens again. I've tried experimenting with SetDirty to no avail.
So my question is, how can I discard changes to this asset object from either the asset database or editor cache?
Thanks!
Answer by Twin-Stick · Jun 18, 2017 at 11:28 AM
Ah ha!
I have found by adding this to my OnDisable method, I'm able to get my desired outcome!
Resources.UnloadAsset(levelData);
Exactly my problem, and my solution!
Thank you very much!
Hey - I'm glad it helped! Thankyou for letting me know, I really appreciate it :)
Your answer
Follow this Question
Related Questions
How can I get a sub asset? 1 Answer
Get asset file from scriptableobject (Nested Scriptableobjects) 2 Answers
How to make my scene object reference survive the button play 1 Answer
How to add a component from a Custom Editor object field to a Prefab ? 1 Answer
How to "Lock" scriptable objects from losing their [SerializeField] values 0 Answers