首页 > 编程技术 > csharp

Unity命令行打包WebGL的示例代码

发布时间:2022-2-11 13:01 作者:Excel2016

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内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

原文出处:https://www.cnblogs.com/skyvip/p/15666548.html

标签:[!--infotagslink--]

您可能感兴趣的文章: