From 7ad8651ca0b8239ef63a1f84b50666c0c81c9185 Mon Sep 17 00:00:00 2001 From: MichaelO Date: Fri, 25 Aug 2023 19:48:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8A=A0=E5=85=A5=20ExtdBuildTasks=20?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=20SBP=20=E6=89=A9=E5=B1=95=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20Sprite=20=E6=89=93=E5=8C=85=E5=86=97=E9=A6=80?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BuildTasks/ExtdBuildTasks.cs | 70 +++++++++++++++++++ .../BuildTasks/ExtdBuildTasks.cs.meta | 11 +++ .../BuildTasks/SBPBuildTasks.cs | 5 +- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/ExtdBuildTasks.cs create mode 100644 Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/ExtdBuildTasks.cs.meta diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/ExtdBuildTasks.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/ExtdBuildTasks.cs new file mode 100644 index 0000000..282662c --- /dev/null +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/ExtdBuildTasks.cs @@ -0,0 +1,70 @@ +using System.Collections.Generic; +using UnityEditor.Build.Content; +using UnityEngine.U2D; +using UnityEditor.Build.Pipeline.Injector; +using UnityEditor.Build.Pipeline.Interfaces; +using UnityEngine; +using System.Linq; + +namespace UnityEditor.Build.Pipeline.Tasks +{ + /// + /// Ref https://zhuanlan.zhihu.com/p/586918159 + /// + public class RemoveSpriteAtlasRedundancy : IBuildTask + { + /// + public int Version => 1; + + [InjectContext] + IBundleWriteData writeDataParam; + + /// + public ReturnCode Run() + { + BundleWriteData writeData = (BundleWriteData)writeDataParam; + + // 所有图集散图的 guid 集合 + HashSet spriteGuids = new HashSet(); + + // 遍历资源包里的资源记录其中图集的散图 guid + foreach (var pair in writeData.FileToObjects) + { + foreach (ObjectIdentifier objectIdentifier in pair.Value) + { + string path = AssetDatabase.GUIDToAssetPath(objectIdentifier.guid); + Object asset = AssetDatabase.LoadAssetAtPath(path); + if (asset is SpriteAtlas) + { + List spritePaths = AssetDatabase.GetDependencies(path, false).ToList(); + foreach (string spritePath in spritePaths) + { + GUID spriteGuild = AssetDatabase.GUIDFromAssetPath(spritePath); + spriteGuids.Add(spriteGuild); + } + } + } + } + + // 将 writeData.FileToObjects 包含的图集散图的 texture 删掉避免冗余 + foreach (var pair in writeData.FileToObjects) + { + List objectIdentifiers = pair.Value; + for (int i = objectIdentifiers.Count - 1; i >= 0; i--) + { + ObjectIdentifier objectIdentifier = objectIdentifiers[i]; + if (spriteGuids.Contains(objectIdentifier.guid)) + { + if (objectIdentifier.localIdentifierInFile == 2800000) + { + // 删除图集散图的冗余 texture + objectIdentifiers.RemoveAt(i); + } + } + } + } + + return ReturnCode.Success; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/ExtdBuildTasks.cs.meta b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/ExtdBuildTasks.cs.meta new file mode 100644 index 0000000..015455c --- /dev/null +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/ExtdBuildTasks.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f01d5c82be95c8f4b93aeefc0454ae5c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/SBPBuildTasks.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/SBPBuildTasks.cs index ef864ec..71056e6 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/SBPBuildTasks.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/SBPBuildTasks.cs @@ -33,19 +33,20 @@ namespace UnityEditor.Build.Pipeline.Tasks // Packing buildTasks.Add(new GenerateBundlePacking()); + buildTasks.Add(new RemoveSpriteAtlasRedundancy()); // Fix for SpriteAtlas Redundancy buildTasks.Add(new UpdateBundleObjectLayout()); buildTasks.Add(new GenerateBundleCommands()); buildTasks.Add(new GenerateSubAssetPathMaps()); buildTasks.Add(new GenerateBundleMaps()); buildTasks.Add(new PostPackingCallback()); - + // Writing buildTasks.Add(new WriteSerializedFiles()); buildTasks.Add(new ArchiveAndCompressBundles()); buildTasks.Add(new AppendBundleHash()); buildTasks.Add(new GenerateLinkXml()); buildTasks.Add(new PostWritingCallback()); - + return buildTasks; } } From 070db961fc49c74890eb095fbdaeb4e3ca726b70 Mon Sep 17 00:00:00 2001 From: MichaelO Date: Sat, 26 Aug 2023 15:02:57 +0800 Subject: [PATCH 2/2] corrected typo error --- .../AssetBundleCollector/AssetBundleCollectorSetting.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/YooAsset/Editor/AssetBundleCollector/AssetBundleCollectorSetting.cs b/Assets/YooAsset/Editor/AssetBundleCollector/AssetBundleCollectorSetting.cs index 069f870..2871f94 100644 --- a/Assets/YooAsset/Editor/AssetBundleCollector/AssetBundleCollectorSetting.cs +++ b/Assets/YooAsset/Editor/AssetBundleCollector/AssetBundleCollectorSetting.cs @@ -111,7 +111,7 @@ namespace YooAsset.Editor public CollectResult GetPackageAssets(EBuildMode buildMode, string packageName) { if (string.IsNullOrEmpty(packageName)) - throw new Exception("Build package name is null or mepty !"); + throw new Exception("Build package name is null or empty !"); var package = GetPackage(packageName); CollectCommand command = new CollectCommand(buildMode, packageName, @@ -131,7 +131,7 @@ namespace YooAsset.Editor if (package.PackageName == packageName) return package; } - throw new Exception($"Not found pacakge : {packageName}"); + throw new Exception($"Not found package : {packageName}"); } } } \ No newline at end of file