OpenScenesFromMenu

From Unify Community Wiki
Jump to: navigation, search

Author: Peter Olsted, inspired by Oliver Eberlei's talk https://www.youtube.com/watch?v=s1o0gZwJS-4

Description

Generates a new script, by default at "Assets/Editor/OpenSceneNames.cs" that generates MenuItems each loads a scene from the build settings. It will ask you to save your scene, to be safe.

Place it inside the Editor folder. Then, you can click "Open Scene/Update Scene Names" and it will list, in the same menu, all scenes in the build settings.

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
 
namespace Utility
{
    public class SceneFind : Editor
    {
 
        [MenuItem("Open Scene/Update Scene Names", false, 0)]
        private static void UpdateNames()
        {
            string editorFolderPath = Application.dataPath + "/Editor";
 
            //Create the fill or overwrite the current at
            StreamWriter stream = File.CreateText(editorFolderPath + "/OpenSceneNames.cs");
 
            //Generated script header
            string classStart = "//Generated by SceneFind.cs \n" +
                                "using UnityEditor; \n" +
                                "using UnityEngine; \n" +
                                "namespace Utility { \n" +
                                "public class EditorMapOpener : Editor { \n"; 
 
            //Get the names from the build settings
            string[] sceneNames = ReadNames();
 
            //Generate [MenuItem("Scene Name Here", false)] headers for each menu item at the "Open Scene". 
            //Example: Open Scene/Scene Name Here
            string classContent = GenerateLoadSceneMenuItems(sceneNames, "Open Scene/");
 
            //A method needed to avoid duplicating these lines in every menu item
            string openIfMethod = FormatLines(
                "private static void OpenIf(string level) {",
                "\tif (EditorApplication.SaveCurrentSceneIfUserWantsTo()){",
                "\t\tEditorApplication.OpenScene(level);",
                "\t}",
                "}");
 
            //The end of the class & namespace
            string endClassContent = "}\n}";
 
            //Write to file
            stream.Write(classStart + classContent + openIfMethod + endClassContent);
            stream.Close();
 
            //Let Unity know a file has changed
            AssetDatabase.Refresh();
        }
 
        private static string GenerateLoadSceneMenuItems(string[] sceneNames, string menuPath)
        {
            string classContent = "";
            for (int i = 0; i < sceneNames.Length; i++)
            {
                string path = sceneNames[i] + ".unity";
                string sceneName = sceneNames[i].Substring(sceneNames[i].LastIndexOf('/') + 1); //Get scene name
 
                string menuItem = "[UnityEditor.MenuItem(" + menuPath + ObjectNames.NicifyVariableName(sceneName) +
                                  "\", false, " + 20 + ")]";
                string function = "private static void " + sceneName + "() {";
                string content = "\tOpenIf(\"" + path + "\");";
                const string functionEnd = "}";
 
                classContent += FormatLines(menuItem, function, content, functionEnd);
            }
            return classContent;
        }
 
        private static string FormatLines(params string[] lines)
        {
            string final = "";
            for (int i = 0; i < lines.Length; i++)
            {
                final += "\t" + lines[i] + "\n";
            }
            return final;
        }
 
        private static string[] ReadNames()
        {
            List<string> names = new List<string>();
 
            foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
            {
 
                if (scene.enabled)
                {
                    string name = scene.path; //.Substring(scene.path.LastIndexOf('/') + 1);
                    name = name.Substring(0, name.Length - 6);
                    names.Add(name);
                }
 
            }
            return names.ToArray();
        }
 
 
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox