Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by Quatum1000, Aug 19, 2017.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    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!
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,590
    Code (CSharp):
    1. [UnityEditor.InitializeOnLoad]
    2. static class EditorSceneManagerSceneSavedExample
    3. {
    4.     static EditorSceneManagerSceneSavedExample()
    5.     {
    6.         UnityEditor.SceneManagement.EditorSceneManager.sceneSaved += OnSceneSaved;
    7.     }
    8.  
    9.     static void OnSceneSaved(UnityEngine.SceneManagement.Scene scene)
    10.     {
    11.         UnityEngine.Debug.LogFormat("Saved scene '{0}'", scene.name);
    12.     }
    13. }
     
    zhuchun, Wanderer13 and Quatum1000 like this.
  3. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    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.

    Code (CSharp):
    1. [InitializeOnLoad]
    2. static class EditorSceneManagerSceneSaved {
    3.     static EditorSceneManagerSceneSaved() {
    4.         UnityEditor.SceneManagement.EditorSceneManager.sceneSaved += OnSceneSaved;
    5.         UnityEditor.SceneManagement.EditorSceneManager.sceneSaving += OnSceneSaving; /// <-----------------
    6.     }
    7.      
    8.     static void OnSceneSaving(UnityEngine.SceneManagement.Scene scene) {
    9.         UnityEngine.Debug.LogFormat("Saving scene '{0}'", scene.name);
    10.     }
    11.                                                                                                          
    12.     static void OnSceneSaved(UnityEngine.SceneManagement.Scene scene) {
    13.         UnityEngine.Debug.LogFormat("Saved scene '{0}'", scene.name);
    14.     }
    15. }
    16.  
    Strange thing is, all other event result in the same error.
     
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    Quatum1000 likes this.
  5. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Thank you!
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hey, sorry to revive this old thread, but I'm trying something similar with newSceneCreated

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.SceneManagement;
    4. using UnityEngine.SceneManagement;
    5.  
    6. [InitializeOnLoad]
    7. public class NewSceneExtension
    8. {
    9.     static NewSceneExtension()
    10.     {
    11.         Debug.Log("Woke up in editor");
    12.         EditorSceneManager.newSceneCreated += AddSceneSettings;
    13.     }
    14.  
    15.     static void AddSceneSettings(Scene scene, NewSceneSetup setup, NewSceneMode mode)
    16.     {
    17.         Debug.Log("Added to scene");
    18.         GameObject sceneSettings = new GameObject("SceneSettings");
    19.         sceneSettings.AddComponent<SceneSettings>();
    20.     }
    21. }
    22.  
    but it never prints "Added to scene"?
     
  7. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889

    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.
     
    Last edited: Oct 29, 2019
  8. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    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.
     
  9. LucasNEmissive

    LucasNEmissive

    Joined:
    Sep 27, 2021
    Posts:
    1
    You'll do best to use the scenes templates for that kind of things, if it is intended for the editor