XDracam / unity-corelibrary

Collection of classes and extension methods that make life with Unity3D more comfortable
MIT License
15 stars 3 forks source link

MarkSceneDirty when creating LazySingleton #14

Closed Eregerog closed 5 years ago

Eregerog commented 5 years ago

If you want to use a lazy singeton in edit time and a new instance has to be created, the scene is modified because a new gameobject and component is added. To my knowledge, the scene has then be marked dirty with

using UnityEditor.SceneManagement;
// ...
#if UNITY_EDITOR
            if (!Application.isPlaying)
                EditorSceneManager.MarkAllScenesDirty();
#endif

This would allow lazy singletons to be used during edit time

XDracam commented 5 years ago

Going to use EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); instead.

XDracam commented 5 years ago

@Eregerog is there any reason not to use Undo.RegisterCreatedObjectUndo instead?

Eregerog commented 5 years ago

Those are two different things: Registering an undo has to be called before the modification is made and only allows "stores" the objects current state which can be re-applied by undoing the change. Mark(AllScenes)Dirty in general tells the editor that an asset has changed, and enables the option to save it.

Eregerog commented 5 years ago

Going to use EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); instead.

The things is, there may be multiple scenes active, and you don't know in which active scene the singleton is. Therefore simply mark all scenes dirty