Unity命令行打包WebGL的示例代码
这篇文章主要介绍了Unity命令行打包WebGL的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
1.扫描所有场景,保存并添加到Build Settings中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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.暴露一个打包的方法,方便命令行调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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文件
1 2 3 4 5 | @ 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的文章就介绍到这了
原文链接:https://www.cnblogs.com/skyvip/p/15666548.html