Skip to content

Instantly share code, notes, and snippets.

@emptybraces
Last active January 15, 2025 10:22
Show Gist options
  • Save emptybraces/b9ea3f413f64d61652a12a2dcf1735f0 to your computer and use it in GitHub Desktop.
Save emptybraces/b9ea3f413f64d61652a12a2dcf1735f0 to your computer and use it in GitHub Desktop.
ヒエラルキービューで選択しているシーン以外を一括でオープン/クローズするエディタ拡張。
namespace Emptybraces.Editor
{
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
// Editor extension that toggles the open/close state of all scenes except the one currently selected in the Hierarchy view.
internal static class ContextMenuSceneLoadUnload
{
[InitializeOnLoadMethod]
static void Init()
{
SceneHierarchyHooks.addItemsToSceneHeaderContextMenu += __Menu;
void __Menu(GenericMenu genericMenu, Scene scene)
{
genericMenu.AddSeparator("");
genericMenu.AddItem(new GUIContent("Open All"), false, () => OpenAllExcept(scene, false));
genericMenu.AddItem(new GUIContent("Open All Except"), false, () => OpenAllExcept(scene, true));
genericMenu.AddItem(new GUIContent("Close All Except"), false, () => CloseAllExcept(scene));
}
}
static void OpenAllExcept(Scene exceptScene, bool isUnload)
{
for (int i = 0; i < SceneManager.sceneCount; ++i)
{
var scene = SceneManager.GetSceneAt(i);
if (!scene.isLoaded && scene != exceptScene)
{
EditorSceneManager.OpenScene(scene.path, OpenSceneMode.Additive);
}
}
if (exceptScene.isLoaded && isUnload)
{
if (exceptScene.isDirty && EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
EditorSceneManager.CloseScene(exceptScene, false);
}
else if (!exceptScene.isLoaded && !isUnload)
EditorSceneManager.OpenScene(exceptScene.path, OpenSceneMode.Additive);
}
static void CloseAllExcept(Scene exceptScene)
{
if (!exceptScene.isLoaded)
EditorSceneManager.OpenScene(exceptScene.path, OpenSceneMode.Additive);
for (int i = 0; i < SceneManager.sceneCount; ++i)
{
var scene = SceneManager.GetSceneAt(i);
if (scene.isLoaded && scene != exceptScene)
{
if (scene.isDirty && !EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
return;
EditorSceneManager.CloseScene(scene, false);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment