Unity动态添加场景到BuildSettings和删除BuildSettings已经存在的场景(不会出现deleted情况)

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

public class EditorBuildSceneTest : Editor
{
    //添加场景至BuildSettings面板中
    [MenuItem("Tool/AddScene")]
    static void RefreshAllScene()
    {
        //注意:1、定义场景数组(注意的是Demo01、Demo02、Demo03场景文件要放在Assets下的Scenes文件夹里面)
        //2、添加场景文件到BuildSettings面板中的时候,场景文件的路径一定要是相对的路径,不能是绝对路径
        EditorBuildSettingsScene[] scenes = new EditorBuildSettingsScene[3] {
            //EditorBuildSettingsScene构造方法的第二个参数为true代表着该场景在BuildSettings面板为激活状态。反之,为不激活状态。
            new EditorBuildSettingsScene("Assets/Scenes/Demo01.unity",true),  
            new EditorBuildSettingsScene("Assets/Scenes/Demo02.unity",false),
            new EditorBuildSettingsScene("Assets/Scenes/Demo03.unity",true)
        } ;

        //设置scene数组(这步一定要设置)
        EditorBuildSettings.scenes = scenes;
    }

    /// <summary>
    /// 删除已经在BuildSettings面板中的所有场景
    /// </summary>
    [MenuItem("Tool/DeleteScene")]
    static void DeleteScenesFormBuildSettings()
    {
        List<EditorBuildSettingsScene> scenes = new List<EditorBuildSettingsScene>();
        EditorBuildSettings.scenes = scenes.ToArray();
    }
}

保存该脚本后,回到Unity中,点击Unity导航栏上的Tool/AddScene选项,会出现如下图所示的结果。
在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity Editor下,可以通过以下代码实现将指定场景添加Build Settings并将其作为初始场景: ```csharp using UnityEditor; using UnityEngine.SceneManagement; public class BuildSettingsManager { [MenuItem("MyMenu/Add Scene To Build Settings")] static void AddSceneToBuildSettings() { // Get the path of the scene to add string scenePath = "Assets/Scenes/MyScene.unity"; // Get the current Build Settings EditorBuildSettingsScene[] buildScenes = EditorBuildSettings.scenes; // Check if the scene is already in the Build Settings bool isAlreadyAdded = false; foreach (EditorBuildSettingsScene buildScene in buildScenes) { if (buildScene.path == scenePath) { isAlreadyAdded = true; break; } } // If the scene is not in the Build Settings, add it if (!isAlreadyAdded) { EditorBuildSettingsScene[] newBuildScenes = new EditorBuildSettingsScene[buildScenes.Length + 1]; for (int i = 0; i < buildScenes.Length; i++) { newBuildScenes[i] = buildScenes[i]; } newBuildScenes[newBuildScenes.Length - 1] = new EditorBuildSettingsScene(scenePath, true); EditorBuildSettings.scenes = newBuildScenes; } // Set the added scene as the first scene in the Build Settings for (int i = 0; i < buildScenes.Length; i++) { if (buildScenes[i].path == scenePath) { EditorBuildSettings.scenes[i].enabled = true; EditorBuildSettings.scenes[i].moveToTop = true; break; } } // Set the added scene as the initial scene Scene scene = SceneManager.GetSceneByPath(scenePath); if (scene != null) { EditorSceneManager.playModeStartScene = scene; } } } ``` 这个脚本添加了一个菜单项"MyMenu/Add Scene To Build Settings",可以通过点击这个菜单项来执行将指定场景添加Build Settings并将其作为初始场景的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Defining the Future

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值