Search Unity

Bug EditorSceneManager.sceneOpened not called on Editor startup.

Discussion in 'Scripting' started by JasonC_, Mar 29, 2022.

  1. JasonC_

    JasonC_

    Joined:
    Oct 27, 2019
    Posts:
    66
    I have an editor script structured similar to this:

    Code (CSharp):
    1.  
    2. [InitializeOnLoad]
    3. class SomeClass {
    4.  
    5.     static SomeClass () {
    6.         // ...
    7.         UnityEditor.SceneManagement.EditorSceneManager.sceneOpened += (scene, _) => {
    8.             // ...
    9.         };
    10.     }
    11.  
    12. }
    13.  
    The sceneOpened callback works when I open a scene from within a running editor; but it is not called on the initially loaded scene on editor startup (e.g. right after opening a project from the hub). However, I can confirm that the static initialization code does run on editor startup (so the callback is registered at some point early on), it's just for whatever reason, the callback isn't called.

    How can I either a) get sceneOpened to also be called after the initial scene is loaded or b) hook into something else that's called after the initial scene is loaded (the GameObject tree must be fully loaded) on editor start?

    2020.3.31f1, Windows 10
     
  2. JasonC_

    JasonC_

    Joined:
    Oct 27, 2019
    Posts:
    66
    I was able to get it functioning as intended by adding an
    EditorApplication.delayCall
    callback, for now.

    In the mean time, I'm going to assume I'm using
    sceneOpened
    correctly, and I filed a bug report.
     
    JakubJWFA likes this.