Jump to content
  • Advertisement
Sign in to follow this  
Adehm

Unity Unity C# EditorSceneManager Issue

This topic is 718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Unity C#

 

Trying to close a scene from the Hierarchy when a user clicks play in the editor. It returns True but the scene remains open.

[InitializeOnLoad]
class EditorClass : Editor{
    static EditorClass(){
        //user clicks play
        if (EditorApplication.isPlayingOrWillChangePlaymode) {
            //remove a scene
            Debug.Log(EditorSceneManager.CloseScene(EditorSceneManager.GetSceneByName(sceneName),false));
        }
    }
}
Edited by Adehm

Share this post


Link to post
Share on other sites
Advertisement
It might be an issue with trying to manipulate the scene during the edit->play transition. Does that code work if you run it from a MenuItem?

How do you define and initialize the sceneName variable? Try also Debug.Logging the sceneName variable to make sure it's still what you expect when that code runs.

Is there anything else happening when the game starts that could be instantly re-opening the scene? Edited by Nypyren

Share this post


Link to post
Share on other sites

How to run it from MenuItem?

 

Would return false if the scene name didn't exist.

 

No, starts paused for testing.

Share this post


Link to post
Share on other sites
MenuItem examples: https://docs.unity3d.com/ScriptReference/MenuItem.html

Basically, just put a [MenuItem("Menu/ItemName")] attribute on a static function. This will create an entry on the Unity editor's main menu that you can click to run the function.

This will let you test your scene closing code. This should let you find out if it's a problem specific to switching from edit->play mode or not.

Share this post


Link to post
Share on other sites

Yeah, the function works that way so I guess it can't be done off the play button because there doesn't appear to be a way to intercept it soon enough or override it altogether. I'll post a solution if I find one, thanks for the help.

Share this post


Link to post
Share on other sites
You might also see if the SceneManager (not EditorSceneManager) will work. It's the equivalent class that you can use while the game is running.

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html

I haven't personally used it for what you're trying to do, but UnloadScene sounds promising. (The documentation on that page says there is an UnloadSceneAsync function, but I don't actually see that one. Could be sketchy docs...)

Share this post


Link to post
Share on other sites

Got it working.

Put a C# script in the Editor folder and paste this code. This will close all scenes currently loaded in the Hierarchy when the play button is clicked.

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
[InitializeOnLoad]
class Editor
{
	static Editor ()
	{
		EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
	}
	private static void PlaymodeStateChanged(){
		if (!EditorApplication.isPlaying) {
			for (int i = 0; i < EditorSceneManager.sceneCount; i++) {
				Scene scene = EditorSceneManager.GetSceneAt (i);
				Debug.Log (scene.name);
				Debug.Log (EditorSceneManager.CloseScene (scene, false));
			}
		}
	}
}
Edited by Adehm

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!