How to clear Dirty state without saving the scene

I have a custom Prefab referencing system in the project I am working on where a given MonoBehavior has a reference to a prefab and it has Awake() logic to spawn the prefab instance but also destroy the instance before the scene saves. The issue is that when the scene opens, it will re-create the prefab instances and the scene becomes marked dirty. So the scene will be perpetually dirty.

What I am looking for is a way to internally flag the scene and its objects as NOT dirty after this load process is complete.

This sounds like you’re doing quite a few things wrong. First of all it seems you use ExecuteInEditMode as a utility to add editor only features. This makes no sense. “ExecuteInEditMode” is only ment to make runtime features (mostly visuals) to update while in edit mode. This is useful for particlesystems or some procedural animation.

Editor features should be implemented as editor script(s). It’s impossible to instantiate a prefab into the scene and not mark it as dirty. It also wouldn’t make much sense. If you want to just create helper gameobjects for an editor feature you should set it’s hideFlags to DontSave. Note that the scene will still be marked as dirty when you instantiate a prefab even when you set it to DontSave afterwards. The only way to create an editor helper object without marking the the scene dirty is through EditorUtility.CreateGameObjectWithHideFlags which is specifically made for this. It’s used internally to create all the SceneView helpers (sceneview camera, directional lights, …)

In general if “saved” objects in the scene are created, destroyed or modified the scene is getting marked as dirty since it is dirty. If you don’t want to save objects to the scene they need appropriate hide flags.

As i said currently there is no way (i know) to instantiate an object with dontsave hideflags. It’s difficult to give any further advice since we know pretty much nothing about what you’re trying to achieve.

Keep in mind that you can also use HideFlags.DontSaveInBuild. This will save the object into the scene when the scene is saved in the editor, but it will be removed when you actually build your game.

Excellent thank you for that very detailed information. It sounds like the CreateGameObjectWithHideFlags is EXACTLY what I am looking for. I am wanting the prefab to exist with references to prefabs but not actual instances of them, yet still represent what the scene will look like when the game runs while editing.