阅读 173

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


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐