EditorSceneManager.sceneSaved example? No code examples in the docs.

Hi everyone,

does anyone can provide a small code example how to use EditorSceneManager.sceneSaved as an event?
I tried to build up an event manager but it fail completely.
I want to get an event if the user press CTRL+S simply.

Thanks a lot!

[UnityEditor.InitializeOnLoad]
static class EditorSceneManagerSceneSavedExample
{
    static EditorSceneManagerSceneSavedExample()
    {
        UnityEditor.SceneManagement.EditorSceneManager.sceneSaved += OnSceneSaved;
    }

    static void OnSceneSaved(UnityEngine.SceneManagement.Scene scene)
    {
        UnityEngine.Debug.LogFormat("Saved scene '{0}'", scene.name);
    }
}
3 Likes

Great, thank you! Works that far with EditorSceneManager.sceneSaved only.

I thought about to test EditorSceneManager.sceneSaving as well, or the other events but all others came up with a error:

A method or delegate `EditorSceneManagerSceneSaved.OnSceneSaving(UnityEngine.SceneManagement.Scene)’ parameters do not match delegate UnityEditor.SceneManagement.EditorSceneManager.SceneSavingCallback(UnityEngine.SceneManagement.Scene, string)’ parameters.

[InitializeOnLoad]
static class EditorSceneManagerSceneSaved {
    static EditorSceneManagerSceneSaved() {
        UnityEditor.SceneManagement.EditorSceneManager.sceneSaved += OnSceneSaved;
        UnityEditor.SceneManagement.EditorSceneManager.sceneSaving += OnSceneSaving; /// <-----------------
    }
      
    static void OnSceneSaving(UnityEngine.SceneManagement.Scene scene) {
        UnityEngine.Debug.LogFormat("Saving scene '{0}'", scene.name);
    }
                                                                                                          
    static void OnSceneSaved(UnityEngine.SceneManagement.Scene scene) {
        UnityEngine.Debug.LogFormat("Saved scene '{0}'", scene.name);
    }
}

Strange thing is, all other event result in the same error.

SceneSavingCallback has a second parameter.

1 Like

Thank you!

hey, sorry to revive this old thread, but I’m trying something similar with newSceneCreated

using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;

[InitializeOnLoad]
public class NewSceneExtension
{
    static NewSceneExtension()
    {
        Debug.Log("Woke up in editor");
        EditorSceneManager.newSceneCreated += AddSceneSettings;
    }

    static void AddSceneSettings(Scene scene, NewSceneSetup setup, NewSceneMode mode)
    {
        Debug.Log("Added to scene");
        GameObject sceneSettings = new GameObject("SceneSettings");
        sceneSettings.AddComponent<SceneSettings>();
    }
}

but it never prints “Added to scene”?

I tried your script and from another script
var scene = EditorSceneManager.CreateScene(“NEW SCENE”);

It displays : Woke up in editor" but does not call for AddSceneSettings; for any reason.
Seems to be a bug?!

I think that should work from a API only. And you can NOT use it in Build anyway.
So if you call scene = EditorSceneManager.CreateScene(“NEW SCENE”); you know scene was already created if scene != default. Then you can call any other function contains the functionality of AddSceneSettings().

Or you want to do anything special, then file a bug.

I’d like to add some default GameObjects to a newly created scene, just like the Directional Light and the Camera unity adds by default.

You’ll do best to use the scenes templates for that kind of things, if it is intended for the editor