1
votes

Add this code to an Editor Window script, add an object through script then try to create a new scene. Why does unity not ask to save the scene before opening a new one?

 Event e = Event.current;
         switch (e.type)
         {
             case EventType.keyDown:
                 {
                     if (e.keyCode == KeyCode.Escape)
                     {
                         GameObject b = new GameObject();
                         b.transform.position = new Vector3(0,0,0);
                     }
                     e.Use();    
                 }
                 break;
         }
4
Can't test here, but you probable need to tell Unity your scene has actually changed using EditorUtility.SetDirty(someObject); - Kamalen
Tried it man and it still didn't work. Will be playing with this option though now to see if anything can come from it. Thanks for the idea. - Paul Britton

4 Answers

3
votes

I don't know if this helps but regarding the dirty bit in Unity 5

You have to:

using UnityEditor.SceneManagement;

at the top of your code. Then use:

EditorSceneManager.MarkAllScenesDirty(); //or mark individual scenes as dirty

Absolutely necessary if you have custom editor scripts dynamically instantiating game objects. (I had one creating a tile based map. Looked great in the editor. I would save scene and play the game. It was gone...)

I can't take full credit for the find, however and old unity 4 post pointed me in the right direction.

1
votes
0
votes

after creating the object, (or doing anything to an object in the scene) you need to call EditorUtility.SetDirty(b); The same goes if you change a value on a script that you wrote, you will need to mark that dirty as well for the changes to persist.

0
votes

use this code

                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEngine.SceneManagement.SceneManager.GetActiveScene());

make current scene modefied