com.alicizax.unity.editor.e.../Editor/Build/BuildCLI/AppBuildHelper.cs

86 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AlicizaX.Editor;
using AlicizaX;
using UnityEditor;
using UnityEngine;
public static class AppBuildHelper
{
public static void BuildApplication(AppBuildParameter appBuildParameter, bool showExplorer = false)
{
if (!Directory.Exists(appBuildParameter.OutPutPath))
{
Directory.CreateDirectory(appBuildParameter.OutPutPath);
}
Debug.Log("Starting build application");
BuilderGenerate.GeneratAppBuilderSetting(appBuildParameter.Language, appBuildParameter.ShowDebugWnd, appBuildParameter.ResMode);
Debug.Log("Generate AppBuilderSetting.bytes");
PlayerSettings.fullScreenMode = appBuildParameter.FullScreenMode;
if (appBuildParameter.FullScreenMode == FullScreenMode.Windowed)
{
PlayerSettings.defaultScreenWidth = appBuildParameter.WindowedScreenSize.x;
PlayerSettings.defaultScreenHeight = appBuildParameter.WindowedScreenSize.y;
}
if (!string.IsNullOrEmpty(appBuildParameter.Version))
{
}
try
{
var options = new BuildPlayerOptions
{
scenes = appBuildParameter.Scenes,
locationPathName = Path.Combine(appBuildParameter.OutPutPath, appBuildParameter.FileName),
target = appBuildParameter.BuildTarget,
options = appBuildParameter.DevelopBuild ? BuildOptions.Development : BuildOptions.None
};
var report = BuildPipeline.BuildPlayer(options);
HandleBuildReport(report);
if (showExplorer)
{
OpenFolder.Execute(report.summary.outputPath);
}
}
catch (Exception e)
{
Debug.Log($"构建过程中发生异常: {e.Message}");
}
}
private static void HandleBuildReport(UnityEditor.Build.Reporting.BuildReport report)
{
if (report.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded)
{
Debug.Log($"整包构建完成:${report.summary.outputPath}");
}
else
{
#if UNITY_2023_1_OR_NEWER
Debug.LogError(report.SummarizeErrors());
#else
var errors = new List<string>();
foreach (var step in report.steps)
{
foreach (var msg in step.messages)
{
if (msg.type == LogType.Error || msg.type == LogType.Exception)
{
errors.Add($"[Step: {step.name}] {msg.content}");
}
}
}
Debug.LogError($"构建失败,错误信息:\n{string.Join("\n", errors)}");
#endif
}
}
}