Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by TruffelsAndOranges · Jan 28, 2016 at 07:26 PM · editorsceneeditor-scriptingscene-loadingscenes

Cannot close/unload a scene that is open in editor during playmode (using C# code)?

Basically I want to open a scene whenever the user is in the editor, and then close that scene whenever the user is in playmode. Opening the scene works by using:

 EditorSceneManager.OpenScene("Assets/Scenes/scenename.unity", OpenSceneMode.Additive);

However, it seems impossible to unload/close the scene whenever the user presses play. Currently I can listen to the user pressing play through:

 EditorApplication.hierarchyWindowChanged

And:

 EditorApplication.isPlayingOrWillChangePlaymode

I tried using the following calls to unload/close the scene before the game starts, but none of them works:

 UnityEngine.SceneManagement.SceneManager.UnloadScene(scenename);
 UnityEngine.SceneManagement.SceneManager.UnloadScene("Assets/Scenes/scenename.unity");
 Scene s = EditorSceneManager.GetSceneByName(scenename);
 EditorSceneManager.CloseScene(s, true);

To me it seems as if the SceneManagement in Unity is completeley broken, but maybe I'm just missing something? Any ideas/tips/help?

Comment
Add comment · Show 1
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 insominx · Oct 18, 2016 at 09:00 PM 0
Share

I would also like to know how to do this.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Alexander-Fedoseev · Oct 11, 2018 at 11:02 AM

Just add this script anywhere in your Assets folder (not inside Editor folder) and it will making what you want. No need to add it to gameobject as a component.

 using UnityEditor;
 using UnityEditor.SceneManagement;
 using UnityEngine.SceneManagement;
 
 [InitializeOnLoadAttribute]
 
 // Unload scenes except active scene before Play Mode
 // Load them back when enter Editor Mode 
 
 public static class EditorScenes 
 {
     static EditorScenes()
     {
         EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
     }
 
     private static void OnPlayModeStateChanged(PlayModeStateChange state)
     {
         if (state == PlayModeStateChange.EnteredEditMode) OpenScenes();
         if (state == PlayModeStateChange.ExitingEditMode) CloseScenes();     
     }
     
     // -----------------------------------------------------
 
     private static void OpenScenes()
     {
         for (int i = 0; i < SceneManager.sceneCount; i++)
         {
             Scene scene = SceneManager.GetSceneAt(i);
             if (!IsActiveScene(scene)) OpenScene(scene);
         }
     }
     
     private static void CloseScenes()
     {
         for (int i = 0; i < SceneManager.sceneCount; i++)
         {
             Scene scene = SceneManager.GetSceneAt(i);
             if (!IsActiveScene(scene)) CloseScene(scene);
         }
     }
     
     // -----------------------------------------------------
 
     private static void OpenScene(Scene scene)
     {
         EditorSceneManager.OpenScene(scene.path, OpenSceneMode.Additive);
     }
 
     private static void CloseScene(Scene scene)
     {
         EditorSceneManager.CloseScene(scene, false);
     }
     
     // -----------------------------------------------------
     
     private static Scene activeScene
     {
         get { return SceneManager.GetActiveScene(); }
     }
 
     private static bool IsActiveScene(Scene scene)
     {
         return scene == activeScene;
     }
 }
Comment
Add comment · Show 2 · 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 vorp · Sep 06, 2020 at 05:09 PM 0
Share

This works great for me. I slightly modified it to keep a specific named scene open while closing all the rest since I have a "title" scene that initializes a bunch of important game objects the other scenes rely on. Thanks!

avatar image andreiagmu · Apr 24, 2021 at 08:04 AM 0
Share

Important: As this script uses the UnityEditor namespace (which isn't included in game builds), you actually NEED to add this script inside an Editor folder, or Editor assembly (if you are using asmdefs), or else the game builds will always fail. After making that adjustment, this script worked perfectly! Thanks!

avatar image
0

Answer by TruffelsAndOranges · Jan 28, 2016 at 07:35 PM

Very interestingly those code lines work when the game IS running:

         Scene s = EditorSceneManager.GetSceneByName(scenename);
         EditorSceneManager.CloseScene(s, true);

But I want to close the scene BEFORE the game starts. So it's kind of broken right now since there doesn't seem to be a way to do that.

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 kblood · Mar 13, 2017 at 12:28 AM

This is annoying, that is sure, but its not exactly broken though, is it @TruffelsAndOranges?

You can right click each scene you have opened in the editor and unload them manually, before you actually hit play. Its meant to make it easier to work with multiple scenes in the editor, but right now, if you play while they are loaded and you have a system that loads these scenes then they will be loaded double.

I think there could be some different workarounds though. One would be to not load these scenes if you are in the editor, in the scripts. Like this:

 #if !UNITY_EDITOR
         Load("Player&UI");
         Load("Scene2");
         Load("Scene2.1");
 #endif

This will make sure that you do not load these scenes if you are in the editor. But then you have to make sure that you have loaded these scenes in the editor, and that you do not unload them, or at least if you unload them, then load them again before you hit play.

Those loads use the regular SceneManager. Another way to unload all the scenes is you can do a:

 // Check if in editor and do this
 #if !UNITY_EDITOR
         SceneManager.UnloadSceneAsync("insert scene name");
         SceneManager.UnloadSceneAsync("insert scene name");
         SceneManager.UnloadSceneAsync("insert scene name");
 #endif

That will unload all these scenes, and you can put this in an awake command, but I am guessing that will not be good enough for you because you need these scenes to not load in the first place, and this would then be too late? I have a similar problem, and my only solution seems to be to just unload all the scenes that I have open in the editor except the scene manager scene that loads all the other scenes. They will still be in the editor though, they just become grayed out., and you can load them again after running the test by right clicking them in the hierarchy view and load them. They will only stop being in the hierarchy view if you remove them with that right right click menu.

EDIT: Hmmm... not sure here. Seems like you cannot unload the.

Well, I have played around with this some more. The scenes just cannot be found on Awake because they are all still loading. So you can unload them after the updates begin. I made a lot of things that are in "#if UNITY_EDITOR" making a variable that is told what the Time.time is during Awake, and then in update I look for a time between 0.2f after awake and 2f after awake, and redo all the scene loading and variables and such.

Pretty annoying... I guess it is still easier to just select the levels in the editor and unload them before you hit play.

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

43 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Get the currently open scene name or file name 8 Answers

How to use DontDestroyOnLoad only once 1 Answer

Handle editor playmode exit 0 Answers

Is there a callback for when a scene gets unloaded in the Editor? 1 Answer

Creating AssetBundles from Editor Scripts 1 Answer


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