Skip to content

Instantly share code, notes, and snippets.

@Abbabon
Last active September 27, 2023 06:26
Show Gist options
  • Save Abbabon/64be2a3eed31a2d55bc531a7d28e35cd to your computer and use it in GitHub Desktop.
Save Abbabon/64be2a3eed31a2d55bc531a7d28e35cd to your computer and use it in GitHub Desktop.
Scene Inspector Window
#if UNITY_EDITOR
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
// Drop this in your project; Since this uses UnityEditor, make sure to place it under an Editor directory.
namespace Game.Editor.SceneInspectorTool
{
public class ScenesInspectorWindow : EditorWindow
{
private readonly Dictionary<string, string> _sceneNameToScenePathMap = new Dictionary<string, string>();
private static ScenesInspectorWindow _scenesInspectorWindow;
private const string BOOTSTRAP_SCENE_NAME = "BootstrapScene";
private static string _bootstrapScenePath;
[MenuItem("Tools/Scenes Inspector %g")]
private static void ToggleWindow()
{
if (_scenesInspectorWindow != null)
{
_scenesInspectorWindow.Close();
_scenesInspectorWindow = null;
}
else
{
_scenesInspectorWindow = GetWindow<ScenesInspectorWindow>("Scenes Inspector");
_scenesInspectorWindow.Init();
_scenesInspectorWindow.Show();
}
}
private void Init()
{
var scenePaths = EditorBuildSettings.scenes
.Where(s => s.enabled)
.Select(s => s.path)
.ToArray();
foreach (var scenePath in scenePaths)
{
var displayName = scenePath.Split('/').Last();
if (displayName.Contains(BOOTSTRAP_SCENE_NAME))
{
_bootstrapScenePath = scenePath;
}
_sceneNameToScenePathMap.Add(displayName, scenePath);
}
}
protected void OnGUI()
{
if (_sceneNameToScenePathMap == null)
{
return;
}
foreach (var keyValuePair in _sceneNameToScenePathMap)
{
var displayName = keyValuePair.Key;
var scenePath = keyValuePair.Value;
DrawOpenSceneButton(displayName, scenePath);
}
}
private static void DrawOpenSceneButton(string displayName, string scenePath)
{
if (GUILayout.Button(displayName))
{
var openingBootstrapScene = scenePath.Contains("Bootstrap");
EditorSceneManager.OpenScene(_bootstrapScenePath, OpenSceneMode.Single);
if (!openingBootstrapScene)
{
EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
}
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment