Unity3D 生成编辑器场景列表,快速切换

平时开发中,Project窗口中可能会存在很多个scene,每次切换场景编辑的时候,需要到Project窗口中进行查找,非常麻烦,特别是引入的一些插件也自带了一些Scene。
那么有没有一种方式,可以快速的找到场景并进行切换呢?
今天我们就来解决下这个问题。
通过脚本遍历Project窗口中的所有scene 文件,并在菜单栏生成下拉列表,点击对应的场景就能快速的切换过去。
首先创建ScenesMenuBuild.cs 并将下面的代码复制进去

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;


/// Add a `Scenes` menu to Unity editor for quick access to all scenes in project.
///
/// Generates/updates a `ScenesMenu.cs` file with the required menu annotations.
public static class ScenesMenuBuild
{
     
    // Path on filesystem (relative to Assets directory) to write menu command scripts.
    // This can be inside any "Editor" folder.
    static readonly string ScenesMenuPath = "Scripts/Editor/ScenesMenu.cs";


    [MenuItem("Scenes/Update This List")]
    public static void UpdateList()
    {
     
        string scenesMenuPath = Path.Combine(Application.dataPath, ScenesMenuPath);
        var stringBuilder = new StringBuilder();
        stringBuilder.AppendLine("// Generated file");
        stringBuilder.AppendLine("using UnityEditor;");
        stringBuilder.AppendLine("using UnityEditor.SceneManagement;");
        stringBuilder.AppendLine("public static class ScenesMenu");
        stringBuilder.AppendLine("{");


        foreach (string sceneGuid in AssetDatabase.FindAssets("t:Scene", new string[] {
      "Assets" }))
        {
     
            string sceneFilename = AssetDatabase.GUIDToAssetPath(sceneGuid);
            string sceneName = Path.GetFileNameWithoutExtension(sceneFilename);
            string methodName = sceneFilename.Replace('/', '_').Replace('\\', '_').Replace('.', '_').Replace('-','_');
            stringBuilder.AppendLine(string.Format("    [MenuItem(\"Scenes/{0}\", priority = 10)]",sceneName));
            stringBuilder.AppendLine(string.Format("    public static void {0}() {
     { ScenesMenuBuild.OpenScene(\"{1}\"); }}",methodName,sceneFilename));
        }
        stringBuilder.AppendLine("}");
    Debug.LogError(Path.GetDirectoryName(scenesMenuPath)+">>>>>>>****");
        Directory.CreateDirectory(Path.GetDirectoryName(scenesMenuPath));
        File.WriteAllText(scenesMenuPath, stringBuilder.ToString());
        AssetDatabase.Refresh();
    }


    public static void OpenScene(string filename)
    {
     
        if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
            EditorSceneManager.OpenScene(filename);
    }
}

会在菜单栏出现Scenes选项,如下图
Unity3D 生成编辑器场景列表,快速切换_第1张图片

点击Update This List ,则会在Assets目录下生成Scripts/Editor/ScenesMenu.cs,该脚本的内容就是每个scene对应的MenuItem.此时就可以在Scenes下拉列表里面快速的切换场景进行编辑了。

你可能感兴趣的:(Unity3D 生成编辑器场景列表,快速切换)