1.扫描所有场景,保存并添加到Build Settings中
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneUtils
{
#if UNITY_EDITOR
public static void RefreshAllScene()
{
// 设置场景 *.unity 路径
string resourcesPath = Application.dataPath;
// 遍历获取目录下所有 .unity 文件
string[] absolutePaths = Directory.GetFiles(resourcesPath, "*.unity", SearchOption.AllDirectories);
List<EditorBuildSettingsScene> list = new List<EditorBuildSettingsScene>();
// 定义 场景数组
for (int i = 0; i < absolutePaths.Length; i++)
{
string path = "Assets" + absolutePaths[i].Remove(0, resourcesPath.Length);
path = path.Replace("\\", "/");
// 通过scene路径初始化
list.Add(new EditorBuildSettingsScene(path, true));
}
// 设置 scene 数组
EditorBuildSettings.scenes = list.ToArray();
}
public static void RefreshScene(params string[] tagetPaths)
foreach (string tagetPath in tagetPaths)
{
if (path.Contains(tagetPath))
{
// 通过scene路径初始化
list.Add(new EditorBuildSettingsScene(path, true));
}
}
#endif
}
2.暴露一个打包的方法,方便命令行调用
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 命令行批处理
/// </summary>
public class Batchmode
{
#if UNITY_EDITOR
static List<string> levels = new List<string>();
[MenuItem("FViteMVC/Build/BuildWebGL", false)]
public static void BuildWebGL()
{
// 打包前需要做的事情
FviteMvcEditor.RefreshAllScene();
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
{
if (!scene.enabled) continue;
levels.Add(scene.path);
}
// 第一个参数为所有场景路径
// 第二个参数是打包位置
// 第三个参数是目标平台
// 第四个参数是构建选项 None代表执行指定的构建,不包含任何特殊设置或额外任务
BuildPipeline.BuildPlayer(levels.ToArray(), "Build", BuildTarget.WebGL,BuildOptions.None);
}
#endif
}
3.写一个.bat文件
@echo off
echo lunch unity.exe ,please wait a moment...
"C:\Program Files\Unity\Hub\Editor\2020.3.18f1c1\Editor\Unity.exe" -quit -batchmode -projectPath "D:\Unity\Unity\FViteMVC" -executeMethod Batchmode.BuildWebGL
echo "Build WebGL done"
pause
到此这篇关于Unity命令行打包WebGL的文章就介绍到这了,更多相关Unity打包WebGL内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!