- Home /
How do I detect if a scene is being loaded during edit time?
I am using a script which executes during edit mode.(@script ExecuteInEditMode();) and I need to know when my user decides to load a new scene inside the editor. How do I do this?
Cheers
Answer by skalev · Oct 03, 2013 at 07:41 AM
I've ended up doing something similar. monitoring the "EditorApplication.currentScene" but instead of in updated, I listen to the EditorApplication.hierarchyWindowChanged.
[InitializeOnLoad]
public static class LatestScenes
{
private static string currentScene;
static LatestScenes()
{
currentScene = EditorApplication.currentScene;
EditorApplication.hierarchyWindowChanged += hierarchyWindowChanged;
}
private static void hierarchyWindowChanged()
{
if (currentScene != EditorApplication.currentScene)
{
//a scene change has happened
Debug.Log("Last Scene: " + currentScene);
currentScene = EditorApplication.currentScene;
}
}
}
by the way, i tested this one, and I think hierarchyWindowChanged
is called after the scene is fully loaded, which is great
Unfortunately, if the user is reloading the exact same scene, it won't trigger. I need to initialize some properties each time a scene is loading :( even if it is the same scene.
Actually it does trigger.
The issue is that the EditorApplication.currentScene value doesn't change, since it is loading the same scene.
Actually it does trigger. The issue is that the EditorApplication.currentScene value doesn't change, since it is loading the same scene.
Yeah I mispoke :) I was talking about triggering my initialization :)
Answer by Rs · Nov 19, 2017 at 11:55 AM
EditorSceneManager is the API you're looking for.
In a static class, create a callback method:
static void SceneOpenedCallback(
Scene _scene,
UnityEditor.SceneManagement.OpenSceneMode _mode)
{
Debug.Log("SCENE LOADED");
}
In your static class' constructor, add the following:
UnityEditor.SceneManagement.EditorSceneManager.sceneOpened += SceneOpenedCallback;
Also, you will need to mark your class with InitializeOnLoad attribute so that Unity triggers it when a new project is opened.
So, putting all together:
[InitializeOnLoad]
public static class MyEditorClass {
// constructor
static MyEditorClass() {
UnityEditor.SceneManagement.EditorSceneManager.sceneOpened +=
SceneOpenedCallback;
}
static void SceneOpenedCallback(
Scene _scene,
UnityEditor.SceneManagement.OpenSceneMode _mode)
{
Debug.Log("SCENE LOADED");
}
}
Do you know how to access the scene-open event when a project is first opened to its last open scene, rather than when the user opens a scene manually?
EditorApplication.delayCall might do what you need.
Answer by whydoidoit · Jul 12, 2012 at 06:33 AM
Application.isPlaying will be false when code executes in the editor and the game is not running.
I don't know who downvoted yours, but he must be stupid. I balance. Fairplay.
Answer by ZoltanErdokovy · Sep 25, 2012 at 09:35 AM
I'd like to know this too. My best idea right now is to check "EditorApplication.currentScene" in "EditorWindow.Update" to see if it has changed since last time but it's not a very elegant solution...
Answer by F-N · Feb 06, 2020 at 03:00 PM
In Unity 2018+ this works for me:
[InitializeOnLoad]
public static class LatestScene {
public delegate void ActiveSceneChangeDelegate(string name);
public static event ActiveSceneChangeDelegate OnChange;
private static string name;
static LatestScene() {
name = SceneManager.GetActiveScene().name;
EditorApplication.hierarchyChanged += hierarchyWindowChanged;
}
private static void hierarchyWindowChanged() {
string activeSceneName = SceneManager.GetActiveScene().name;
if (activeSceneName != name) {
name = activeSceneName;
OnChange?.Invoke(name);
}
}
}
Your answer
Follow this Question
Related Questions
Update Custom Editor on Load 0 Answers
heavy scene loading 1 Answer
pre-load multiple scenes but only activate one 0 Answers
Addressable for Oculus Go 2 Answers
Save & Load Game question 3 Answers