Hey folks,
I’ve got an editor script that makes some changes, then calls
EditorSceneManager.MarkSceneDirty( SceneManagement.SceneManager.GetActiveScene() );
That call works, because if I don’t attempt to save, the scene is, in fact, marked as dirty and then savable manually. If, however, I attempt to automate this…it’s as if the scene saving clears the dirty mark but doesn’t actually save the scene. Using this call:
EditorSceneManager.SaveScene( SceneManagement.SceneManager.GetActiveScene() );
I have no idea why, when I comment OUT the save call, the scene is marked dirty, but trying to save it clears the flag without saving.
ALSO to note…if I try to manually save the scene after it’s marked dirty (with my editor call commented out), it does the same thing. The dirty flag is removed as if it saved, but if I reload the scene, the changes are gone.
Ok, so I don’t know why…but I had to call EditorUtility.SetDirty() and pass in one of the objects in the scene. This really doesn’t make sense, it shouldn’t be necessary to mark an object dirty if I set the scene as dirty and save it. Either way, you only have to mark the one object dirty for it to work.
@MAWMatt
Docs says not to use EditorUtility.SetDirty for scene objects:
“From 5.3 onwards, with the introduction of Multi-Scene Editing, this function should no-longer be used for modifying objects in scenes”
It’s not needed (tested that - unity 2020.1.15f1). I just do changes (some variables on custom MonoBeh) and call SceneManager.SaveScene - and scene file is written correctly (checked with diff tool). BTW no need to mark dirty (however, docs recommends to do that https://support.unity.com/hc/en-us/articles/115002294603-How-do-I-make-a-scene-dirty-when-modifying-a-property-via-script-)
There was a bug though, that unity showed scene as dirty after saving that way. But this was coming from NaughtyAttributes.Button which is used in my project just to add clickable button.
I came here searching how can I get rid of my false-dirty bug. So, will leave everything found on the case here.
At first, we can use Undo.postprocessModifications to check what the changes are exactly. In my case, it was not triggering though.
And a workaround:
AssetDatabase.ImportAsset(scene.path, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.DontDownloadFromCacheServer);
It force rereads scene from file, so no dirty flag. Might be helpful to someone