| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using TMPro;
- using UnityEditor;
- using UnityEditor.Build;
- using UnityEngine;
- namespace HotPatch.Editor
- {
- public static class HotPatchBundleBuild
- {
- public static string AotDirectory => Path.Combine(Application.dataPath, "AotDlls");
- const string AotAssetDirectory = "Assets/AotDlls";
- const string DllAssetDirectory = "Assets/HotPatch/BundleInputs";
- public static string FindScenePath(string sceneName)
- {
- var paths = AssetDatabase.FindAssets("t:Scene", new[] { "Assets" }).Select(AssetDatabase.GUIDToAssetPath)
- .Where(p => Path.GetFileNameWithoutExtension(p) == sceneName).ToArray();
- if (paths.Length != 1) throw new InvalidOperationException("场景必须存在且名称唯一: " + sceneName);
- return paths[0];
- }
- public static void ConfigurePlayerScenes()
- {
- var launchPath = FindScenePath("Launch");
-
-
- if (EditorUserBuildSettings.buildScriptsOnly)
- {
- EditorUserBuildSettings.buildScriptsOnly = false;
- Debug.Log("[资源构建] 已关闭 Force skip data build,下一次安装包构建将正常更新场景和资源数据。");
- }
-
- if (!ContainsOnlyLaunch(EditorBuildSettings.scenes, launchPath))
- {
- EditorBuildSettings.scenes = new[] { new EditorBuildSettingsScene(launchPath, true) };
- Debug.Log("[资源构建] 已调整构建场景列表,仅保留并启用 Launch: " + launchPath);
- }
- var profile = UnityEditor.Build.Profile.BuildProfile.GetActiveBuildProfile();
- if (profile != null && profile.overrideGlobalScenes && !ContainsOnlyLaunch(profile.scenes, launchPath))
- {
- profile.scenes = new[] { new EditorBuildSettingsScene(launchPath, true) };
- EditorUtility.SetDirty(profile);
- AssetDatabase.SaveAssetIfDirty(profile);
- Debug.Log("[资源构建] 已调整当前构建配置的场景列表,仅保留并启用 Launch: " + launchPath);
- }
- }
- static bool ContainsOnlyLaunch(EditorBuildSettingsScene[] scenes, string launchPath)
- => scenes != null && scenes.Length == 1 && scenes[0].path == launchPath && scenes[0].enabled;
-
- public static void PrepareAotFiles(IReadOnlyDictionary<string, string> files)
- {
- if (!files.Keys.OrderBy(n => n).SequenceEqual(HotPatchBundleLoader.AotAssemblyNames.OrderBy(n => n)))
- throw new InvalidDataException("Assets/AotDlls 必须包含 mscorlib、Newtonsoft.Json、System.Core 和 UniTask 四个程序集。");
- foreach (var file in files)
- {
- if (!File.Exists(file.Value)) throw new FileNotFoundException("当前平台 AOT 基线缺失。", file.Value);
- if (System.Reflection.AssemblyName.GetAssemblyName(file.Value).Name + ".dll" != file.Key)
- throw new InvalidDataException("AOT 文件与程序集名称不一致: " + file.Key);
- }
- Directory.CreateDirectory(AotDirectory);
- foreach (var file in files) File.Copy(file.Value, Path.Combine(AotDirectory, file.Key + ".bytes"), true);
- AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
- }
- static void PrepareDynamicFonts()
- {
- var fonts = AssetDatabase.FindAssets("t:TMP_FontAsset")
- .Select(AssetDatabase.GUIDToAssetPath)
- .Select(AssetDatabase.LoadAssetAtPath<TMP_FontAsset>)
- .Where(font => font != null && new SerializedObject(font).FindProperty("m_ClearDynamicDataOnBuild").boolValue &&
- (font.atlasPopulationMode == AtlasPopulationMode.Dynamic ||
- font.atlasPopulationMode == AtlasPopulationMode.DynamicOS)).ToArray();
- if (fonts.Length == 0) return;
-
-
- new TMP_PreBuildProcessor().OnPreprocessBuild(null);
- foreach (var font in fonts)
- {
- EditorUtility.SetDirty(font);
- foreach (var texture in font.atlasTextures)
- if (texture != null) EditorUtility.SetDirty(texture);
-
- AssetDatabase.SaveAssetIfDirty(font);
- }
- Debug.Log("[资源构建] 已按 Clear Dynamic Data On Build 设置清理动态字体缓存: " +
- string.Join(", ", fonts.Select(AssetDatabase.GetAssetPath)));
- }
- public static HotPatchManifest Build(string destination, string dll, IReadOnlyDictionary<string, string> aotFiles,
- string assemblyName, string sceneName, string platform, string buildId, bool development, bool includeAot,
- HotPatchManifest original = null)
- {
- if (!includeAot && original?.aotBundle == null)
- throw new InvalidDataException("构建更新 Bundle 前必须提供原始 AOT 清单。");
- var target = (BuildTarget)Enum.Parse(typeof(BuildTarget), platform);
- if (target != EditorUserBuildSettings.activeBuildTarget)
- throw new InvalidOperationException("请先切换到目标平台再构建 Bundle。");
- var scenePath = FindScenePath(sceneName);
- if (includeAot) PrepareAotFiles(aotFiles);
- Directory.CreateDirectory(DllAssetDirectory);
- var dllAsset = DllAssetDirectory + "/" + assemblyName + ".dll.bytes";
- File.Copy(dll, dllAsset, true);
- AssetDatabase.ImportAsset(dllAsset, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
- var assemblyBundleName = HotPatchResource.AssemblyBundleName(assemblyName);
- var sceneBundleName = HotPatchResource.SceneBundleName(sceneName);
- var builds = new List<AssetBundleBuild>
- {
- new AssetBundleBuild { assetBundleName = assemblyBundleName, assetNames = new[] { dllAsset },
- addressableNames = new[] { assemblyName + ".dll.bytes" } },
- new AssetBundleBuild { assetBundleName = sceneBundleName, assetNames = new[] { scenePath } }
- };
- if (includeAot)
- {
- var paths = Directory.GetFiles(AotDirectory, "*.bytes", SearchOption.AllDirectories)
- .Select(p => AotAssetDirectory + "/" + p.Substring(AotDirectory.Length + 1).Replace('\\', '/')).OrderBy(p => p).ToArray();
- var expected = HotPatchBundleLoader.AotAssemblyNames.Select(n => n + ".bytes").OrderBy(n => n).ToArray();
- if (!paths.Select(Path.GetFileName).OrderBy(n => n).SequenceEqual(expected))
- throw new InvalidDataException("Assets/AotDlls 目录只能包含指定的四个 .dll.bytes 文件。");
- builds.Insert(0, new AssetBundleBuild { assetBundleName = "aot.bundle", assetNames = paths,
- addressableNames = paths.Select(Path.GetFileName).ToArray() });
- }
-
-
- var output = Path.GetFullPath("Build/HotUpdate/BundleCache/" + platform);
- Directory.CreateDirectory(output);
- var options = BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.StrictMode;
-
- PrepareDynamicFonts();
- AssetBundleManifest result;
- using (new HotPatchAndroidResolverScope())
- result = BuildPipeline.BuildAssetBundles(output, builds.ToArray(), options, target);
- if (result == null) throw new BuildFailedException("AssetBundle 构建失败,未发布资源。");
- foreach (var build in builds)
- if (result.GetAllDependencies(build.assetBundleName).Length != 0)
- throw new BuildFailedException("Bundle 出现未声明依赖: " + build.assetBundleName);
- return HotPatchResourcePackage.Write(destination, Path.Combine(output, assemblyBundleName), Path.Combine(output, sceneBundleName),
- includeAot ? Path.Combine(output, "aot.bundle") : null, includeAot ? aotFiles : new Dictionary<string, string>(),
- assemblyName, sceneName, scenePath, platform, buildId, development, original);
- }
- }
- }
|