日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Unity命令行打包WebGL的示例代碼_C#教程

作者:Excel2016 ? 更新時(shí)間: 2022-04-18 編程語言

1.掃描所有場(chǎng)景,保存并添加到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()
    {
        // 設(shè)置場(chǎng)景 *.unity 路徑
        string resourcesPath = Application.dataPath;
        // 遍歷獲取目錄下所有 .unity 文件
        string[] absolutePaths = Directory.GetFiles(resourcesPath, "*.unity", SearchOption.AllDirectories);
        List<EditorBuildSettingsScene> list = new List<EditorBuildSettingsScene>();
        // 定義 場(chǎng)景數(shù)組     
        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));
        }
        // 設(shè)置 scene 數(shù)組
        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.暴露一個(gè)打包的方法,方便命令行調(diào)用

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);
        }
        // 第一個(gè)參數(shù)為所有場(chǎng)景路徑
        // 第二個(gè)參數(shù)是打包位置
        // 第三個(gè)參數(shù)是目標(biāo)平臺(tái)
        // 第四個(gè)參數(shù)是構(gòu)建選項(xiàng) None代表執(zhí)行指定的構(gòu)建,不包含任何特殊設(shè)置或額外任務(wù)
        BuildPipeline.BuildPlayer(levels.ToArray(), "Build", BuildTarget.WebGL,BuildOptions.None);
    }
#endif 
}

3.寫一個(gè).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

原文鏈接:https://www.cnblogs.com/skyvip/p/15666548.html

欄目分類
最近更新