Unity C# EditorSceneManager Issue

Started by
6 comments, last by Adehm 7 years, 6 months ago

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

How to run it from MenuItem?

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

No, starts paused for testing.

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.

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.

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

Only used for unloading the compiled scenes the game is running; not the editor Hierarchy.

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

This topic is closed to new replies.

Advertisement