亲宝软件园·资讯

展开

Unity打包WebGL

Excel2016 人气:5

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

加载全部内容

相关教程
猜你喜欢
用户评论