Skip to content

Instantly share code, notes, and snippets.

@korinVR
Created April 20, 2022 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korinVR/0ccb632effc7b02385e5638fcdb1b425 to your computer and use it in GitHub Desktop.
Save korinVR/0ccb632effc7b02385e5638fcdb1b425 to your computer and use it in GitHub Desktop.
Unity: Generate a quick scene-opening menu from the scene list of the Build Settings
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
// Generate a quick scene-opening menu from the scene list of the Build Settings.
// Usage: Open "Scene -> Generate Scene Menu..."
public static class SceneMenuGenerator
{
// Customize it appropriately.
const string GenerateCodePath = "SceneMenu.cs";
const bool IncludeDisabledScenes = true;
[MenuItem("Scene/Generate Scene Menu...")]
public static void Generate()
{
var sb = new StringBuilder();
// Header
sb.Append(@"// Generated by SceneMenuGenerator.cs
using UnityEditor;
using UnityEditor.SceneManagement;
public static class SceneMenu
{
");
// Generate menu items
var index = 0;
foreach (var scene in EditorBuildSettings.scenes)
{
if (!IncludeDisabledScenes && !scene.enabled) continue;
var menuItemName = Path.GetFileNameWithoutExtension(scene.path);
sb.Append($@" [MenuItem(""Scene/{menuItemName}"", false, priority = {100 + index})]
public static void OpenScene{index}() => OpenScene(""{scene.path}"");
");
index++;
}
// Footer
sb.AppendLine(@"
static void OpenScene(string path)
{
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(path);
EditorSceneManager.OpenScene(path);
}
}");
File.WriteAllText(Path.Combine(Application.dataPath, GenerateCodePath), sb.ToString());
AssetDatabase.Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment