Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by grimmy · Jul 12, 2012 at 06:12 AM · sceneloadingeditexecuteineditmodeloaded

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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;
             }
         }
     }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image marsonmao · Jun 16, 2015 at 03:43 AM 0
Share

this one really helps!

avatar image marsonmao · Jun 16, 2015 at 03:44 AM 0
Share

by the way, i tested this one, and I think hierarchyWindowChanged is called after the scene is fully loaded, which is great

avatar image Crazydadz · Jun 26, 2015 at 08:53 PM 0
Share

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.

avatar image skalev · Jun 26, 2015 at 09:00 PM 0
Share

Actually it does trigger.

The issue is that the EditorApplication.currentScene value doesn't change, since it is loading the same scene.

avatar image Crazydadz · Jun 29, 2015 at 08:31 PM 0
Share

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 :)

avatar image
9

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");
         }
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IMD · Apr 12, 2018 at 08:01 PM 1
Share

Thanks Riccardo - beers in London soon ;)

avatar image intrepidolivia · Jul 12, 2019 at 08:26 PM 0
Share

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?

avatar image Zenix intrepidolivia · Jul 24, 2019 at 01:59 AM 0
Share

EditorApplication.delayCall might do what you need.

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikilo · Aug 06, 2015 at 04:22 PM 0
Share

I don't know who downvoted yours, but he must be stupid. I balance. Fairplay.

avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
         }
     }
 
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges