Files
plugin-library/Assets/00.StaryEvoTools/Editor/Build/BuildApkWindow.cs

234 lines
8.0 KiB
C#
Raw Normal View History

2026-03-16 17:31:09 +08:00
using System.Collections.Generic;
2025-07-02 16:28:08 +08:00
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
2025-08-21 16:42:16 +08:00
namespace Stary.Evo.Editor
2025-07-02 16:28:08 +08:00
{
2025-08-21 16:42:16 +08:00
public class BuildApkWindow : OdinEditorWindow
2025-07-02 16:28:08 +08:00
{
2025-08-21 16:42:16 +08:00
public static OdinEditorWindow window;
2025-07-02 16:28:08 +08:00
2026-03-16 17:31:09 +08:00
[MenuItem("Evo/Dev/Apk打包工具", false, 4)]
2025-08-21 16:42:16 +08:00
static void ShowWindows()
2025-07-02 16:28:08 +08:00
{
2025-08-21 16:42:16 +08:00
window = (BuildApkWindow)EditorWindow.GetWindow(typeof(BuildApkWindow));
window.maxSize = new Vector2(400, 500);
window.Show();
2025-07-02 16:28:08 +08:00
}
2025-08-21 16:42:16 +08:00
protected override void Initialize()
2025-07-02 16:28:08 +08:00
{
2025-08-21 16:42:16 +08:00
base.Initialize();
buildApkLocalEntity = new BuildApkEntity(() => { BuildAndroid(false); }, null);
buildApkServerEntity = new BuildApkEntity(null, null);
buildApkWatermarkEntity = new BuildApkEntity(() => { BuildAndroid(true); }, null);
2025-07-02 16:28:08 +08:00
}
2025-08-21 16:42:16 +08:00
[EnumToggleButtons, HideLabel] public DeviceType deviceType;
2026-03-16 17:31:09 +08:00
[HideLabel] [Title("包裹列表", titleAlignment: TitleAlignments.Centered)] [ValueDropdown("GetBuildPackageNames")]
public string selectedPackageNames;
2025-08-21 16:42:16 +08:00
[BoxGroup("Build(清空打包缓存)", centerLabel: true, order: 1)]
[Button("清空打包缓存")]
void OnClearCache()
2025-07-02 16:28:08 +08:00
{
2025-08-21 16:42:16 +08:00
// 清理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}");
}
2025-07-02 16:28:08 +08:00
2025-08-21 16:42:16 +08:00
// 使用System.IO删除
try
{
System.IO.Directory.Delete(Application.dataPath + "/../Library/ScriptAssemblies", true);
Debug.Log("成功删除ScriptAssemblies");
}
catch (System.Exception e)
{
Debug.LogError($"删除失败: {e.Message}");
}
2025-07-02 16:28:08 +08:00
2025-08-21 16:42:16 +08:00
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("缓存清理", "已成功清除所有打包缓存文件", "确定");
}
2025-07-02 16:28:08 +08:00
2025-08-21 16:42:16 +08:00
[BoxGroup("Build(普通包\\服务器本地包)", centerLabel: true, order: 2), HideLabel]
public BuildApkEntity buildApkLocalEntity;
2025-07-02 16:28:08 +08:00
2025-08-21 16:42:16 +08:00
[BoxGroup("Build(服务器热更包)", centerLabel: true, order: 3), HideLabel]
public BuildApkEntity buildApkServerEntity;
2025-07-02 16:28:08 +08:00
2025-08-21 16:42:16 +08:00
[BoxGroup("Build(水印包)", centerLabel: true, order: 4), HideLabel]
public BuildApkEntity buildApkWatermarkEntity;
public void BuildAndroid(bool isWatermark)
2025-07-02 16:28:08 +08:00
{
2025-08-21 16:42:16 +08:00
HotfixMainResDomain hotfixMainResDomain = Resources.Load<HotfixMainResDomain>("HotfixMainResDomain");
if (hotfixMainResDomain == null)
{
Debug.LogError("HotfixMainResDomain 资源在Resources下不存在请检查");
return;
}
2026-03-16 17:33:27 +08:00
ChangePlayerSchema.SetPlayerMode(PLayerMode.HOST_PLAYMODE);
2026-03-16 17:31:09 +08:00
string packageName = "";
// 检测包名格式
if (!IsValidPackageName(selectedPackageNames))
{
// 不是有效包名格式,添加前缀 com.xosmo.
packageName = "com.xosmo." + selectedPackageNames;
// 设置包名和项目名
PlayerSettings.productName = selectedPackageNames;
PlayerSettings.applicationIdentifier = packageName;
}
else
{
// 分割包名字符串
string[] segments = selectedPackageNames.Split('.');
// 返回最后一个部分
packageName = segments[segments.Length - 1];
PlayerSettings.productName = packageName;
PlayerSettings.applicationIdentifier = selectedPackageNames;
}
2025-07-02 16:28:08 +08:00
2025-08-21 16:42:16 +08:00
string[] scenelist = default;
if (isWatermark)
2025-07-02 16:28:08 +08:00
{
2025-08-21 16:42:16 +08:00
scenelist = new[]
{
hotfixMainResDomain.projectInfo.loadingScenePath,
$"Assets/Main/main_{deviceType.ToString()}.unity"
};
2026-03-16 17:31:09 +08:00
PlayerSettings.productName += "_watermark";
2025-08-21 16:42:16 +08:00
PlayerSettings.applicationIdentifier += "_watermark";
2026-03-16 17:31:09 +08:00
Sprite xosmo = Resources.Load<Sprite>("logo");
PlayerSettings.virtualRealitySplashScreen = xosmo.texture;
PlayerSettings.SplashScreen.showUnityLogo = false;
PlayerSettings.SplashScreen.logos = new[]
{
PlayerSettings.SplashScreenLogo.Create(3f, xosmo),
};
2025-08-21 16:42:16 +08:00
}
else
2025-07-02 18:36:48 +08:00
{
2025-08-21 16:42:16 +08:00
scenelist = new[]
{
$"Assets/Main/main_{deviceType.ToString()}.unity"
};
2026-03-16 17:31:09 +08:00
PlayerSettings.virtualRealitySplashScreen = null;
PlayerSettings.SplashScreen.showUnityLogo = false;
PlayerSettings.SplashScreen.logos = null;
2025-08-21 16:42:16 +08:00
}
// 配置构建选项
BuildPlayerOptions buildOptions = new BuildPlayerOptions
{
scenes = scenelist,
2026-03-16 17:31:09 +08:00
locationPathName = $"Builds/Android/{PlayerSettings.productName}.apk",
2025-08-21 16:42:16 +08:00
target = BuildTarget.Android,
options = BuildOptions.None
2025-07-02 18:36:48 +08:00
};
2025-08-21 16:42:16 +08:00
// 执行打包
var report = BuildPipeline.BuildPlayer(buildOptions);
2025-07-02 16:28:08 +08:00
2025-08-21 16:42:16 +08:00
BuildSummary summary = report.summary;
if (summary.result == BuildResult.Succeeded)
{
Debug.Log("Build succeeded: " + summary.totalSize + " bytes");
}
2025-07-02 16:28:08 +08:00
2025-08-21 16:42:16 +08:00
if (summary.result == BuildResult.Failed)
{
Debug.LogError("Build failed");
}
2025-07-02 16:28:08 +08:00
}
2025-08-21 16:42:16 +08:00
public enum DeviceType
2025-07-02 16:28:08 +08:00
{
2025-08-21 16:42:16 +08:00
Xreal,
Rokid
2025-07-02 16:28:08 +08:00
}
2026-03-16 17:31:09 +08:00
/// <summary>
/// 检测当前所有包裹
/// </summary>
/// <returns></returns>
private List<string> GetBuildPackageNames()
{
List<string> result = new List<string>();
foreach (var name in CreatAssetWindow.GetCreatDomainAllName())
{
result.Add(name);
}
if (selectedPackageNames.IsNullOrEmpty())
{
if (result.Count > 0)
{
selectedPackageNames = result[0];
}
else
{
selectedPackageNames = "";
}
}
return result;
}
/// <summary>
/// 检测字符串是否是有效的包名格式
/// </summary>
/// <param name="packageName">要检测的字符串</param>
/// <returns>是否是有效的包名格式</returns>
private bool IsValidPackageName(string packageName)
{
if (string.IsNullOrEmpty(packageName))
return false;
// 包名只能包含小写字母、数字和点
foreach (char c in packageName)
{
if (!char.IsLower(c) && !char.IsDigit(c) && c != '.')
return false;
}
// 不能以点开头或结尾
if (packageName.StartsWith(".") || packageName.EndsWith("."))
return false;
// 不能包含连续的点
if (packageName.Contains(".."))
return false;
// 至少包含一个点
if (!packageName.Contains("."))
return false;
return true;
}
2025-07-02 16:28:08 +08:00
}
}