有时项目需要,某些情况下需要选择打包/不打包某些场景。如果手动去 设置选择 BuildSettings 中这些场景,过程将变得很枯燥,也很容易漏掉这个步骤!
那么,我们就用代码解决吧!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestEditor : EditorWindow
{
[MenuItem("Tools/测试BuildSettings")]
static void OpenWnd()
{
TestEditor wnd = EditorWindow.GetWindow<TestEditor>(false, "测试BuildSettings");
wnd.Show();
}
private void OnGUI()
{
if (GUILayout.Button("勾选/不勾选Logo场景"))
{
EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
foreach (EditorBuildSettingsScene scene in scenes)
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(scene.path);
Debug.LogError(fileInfo.Name);
if(fileInfo.Name == "Logo.unity")
{
scene.enabled = !scene.enabled;
}
}
// 这里重新赋值给 EditorBuildSettings.scenes ,否则设置失败!
EditorBuildSettings.scenes = scenes;
}
}
}
效果图参考如下