Files
plugin-library/Assets/00.StaryEvo/Editor/Build/BuildApkWindow.cs
2025-07-02 18:14:15 +08:00

128 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Stary.Evo;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
using BuildReport = UnityEditor.Build.Reporting.BuildReport;
public class BuildApkWindow : OdinEditorWindow
{
public static OdinEditorWindow window;
[MenuItem("Evo/Apk打包工具")]
static void ShowWindows()
{
window = (BuildApkWindow)EditorWindow.GetWindow(typeof(BuildApkWindow));
window.maxSize = new Vector2(400, 500);
window.Show();
}
protected override void Initialize()
{
base.Initialize();
buildApkLocalEntity = new BuildApkEntity(BuildAndroid, BuildAndroid);
buildApkServerEntity = new BuildApkEntity(BuildAndroid, BuildAndroid);
buildApkWatermarkEntity = new BuildApkEntity(BuildAndroid, BuildAndroid);
}
[EnumToggleButtons, HideLabel] public DeviceType deviceType;
[BoxGroup("Build(清空打包缓存)", centerLabel: true, order: 1)]
[Button("清空打包缓存")]
void OnClearCache()
{
// 清理Library缓存目录
// 使用System.IO删除
// try {
// System.IO.Directory.Delete(Application.dataPath + "/../Library/AssetImportState", true);
// Debug.Log("成功删除AssetImportState");
// } catch (System.Exception e) {
// Debug.LogError($"删除失败: {e.Message}");
// }
// 使用System.IO删除
try
{
System.IO.Directory.Delete(Application.dataPath + "/../Library/BuildCache", true);
Debug.Log("成功删除BuildCache");
}
catch (System.Exception e)
{
Debug.LogError($"删除失败: {e.Message}");
}
// 使用System.IO删除
try
{
System.IO.Directory.Delete(Application.dataPath + "/../Library/ScriptAssemblies", true);
Debug.Log("成功删除ScriptAssemblies");
}
catch (System.Exception e)
{
Debug.LogError($"删除失败: {e.Message}");
}
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("缓存清理", "已成功清除所有打包缓存文件", "确定");
}
[BoxGroup("Build(普通包\\服务器本地包)", centerLabel: true, order: 2), HideLabel]
public BuildApkEntity buildApkLocalEntity;
[BoxGroup("Build(服务器热更包)", centerLabel: true, order: 3), HideLabel]
public BuildApkEntity buildApkServerEntity;
[BoxGroup("Build(水印包)", centerLabel: true, order: 4), HideLabel]
public BuildApkEntity buildApkWatermarkEntity;
public void BuildAndroid()
{
HotfixMainResDomain hotfixMainResDomain = Resources.Load<HotfixMainResDomain>("HotfixMainResDomain");
if (hotfixMainResDomain == null)
{
Debug.LogError("HotfixMainResDomain 资源在Resources下不存在请检查");
return;
}
// 设置包名和项目名
PlayerSettings.productName = hotfixMainResDomain.projectInfo.projectName;
PlayerSettings.applicationIdentifier = hotfixMainResDomain.projectInfo.projectPackageName;
// 配置构建选项
BuildPlayerOptions buildOptions = new BuildPlayerOptions
{
scenes = new[]
{
hotfixMainResDomain.projectInfo.loadingScenePath,
$"Assets/Main/main_{deviceType.ToString()}.unity"
},
locationPathName = $"Builds/Android/{PlayerSettings.applicationIdentifier}.apk",
target = BuildTarget.Android,
options = BuildOptions.None
};
// 执行打包
BuildReport report = BuildPipeline.BuildPlayer(buildOptions);
BuildSummary summary = report.summary;
if (summary.result == BuildResult.Succeeded)
{
Debug.Log("Build succeeded: " + summary.totalSize + " bytes");
}
if (summary.result == BuildResult.Failed)
{
Debug.LogError("Build failed");
}
}
public enum DeviceType
{
Xreal,
Rokid
}
}