mirror of https://github.com/tuyoogame/YooAsset
update asset bundle builder
parent
11386a7ec2
commit
bcb6443300
|
@ -28,6 +28,11 @@ namespace YooAsset.Editor
|
|||
/// 缓存服务器端口
|
||||
/// </summary>
|
||||
public int CacheServerPort;
|
||||
|
||||
/// <summary>
|
||||
/// 修复图集资源冗余问题
|
||||
/// </summary>
|
||||
public bool FixSpriteAtlasRedundancy = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -8,65 +8,61 @@ using System.Linq;
|
|||
|
||||
namespace UnityEditor.Build.Pipeline.Tasks
|
||||
{
|
||||
/// <summary>
|
||||
/// Ref https://zhuanlan.zhihu.com/p/586918159
|
||||
/// </summary>
|
||||
public class RemoveSpriteAtlasRedundancy : IBuildTask
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public int Version => 1;
|
||||
/// <summary>
|
||||
/// Ref https://zhuanlan.zhihu.com/p/586918159
|
||||
/// </summary>
|
||||
public class RemoveSpriteAtlasRedundancy : IBuildTask
|
||||
{
|
||||
public int Version => 1;
|
||||
|
||||
[InjectContext]
|
||||
IBundleWriteData writeDataParam;
|
||||
[InjectContext]
|
||||
IBundleWriteData writeDataParam;
|
||||
|
||||
/// <inheritdoc />
|
||||
public ReturnCode Run()
|
||||
{
|
||||
public ReturnCode Run()
|
||||
{
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
BundleWriteData writeData = (BundleWriteData)writeDataParam;
|
||||
BundleWriteData writeData = (BundleWriteData)writeDataParam;
|
||||
|
||||
// 所有图集散图的 guid 集合
|
||||
HashSet<GUID> spriteGuids = new HashSet<GUID>();
|
||||
// 图集引用的精灵图片集合
|
||||
HashSet<GUID> spriteGuids = new HashSet<GUID>();
|
||||
foreach (var pair in writeData.FileToObjects)
|
||||
{
|
||||
foreach (ObjectIdentifier objectIdentifier in pair.Value)
|
||||
{
|
||||
var assetPath = AssetDatabase.GUIDToAssetPath(objectIdentifier.guid);
|
||||
var assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
|
||||
if (assetType == typeof(SpriteAtlas))
|
||||
{
|
||||
var spritePaths = AssetDatabase.GetDependencies(assetPath, false);
|
||||
foreach (string spritePath in spritePaths)
|
||||
{
|
||||
GUID spriteGuild = AssetDatabase.GUIDFromAssetPath(spritePath);
|
||||
spriteGuids.Add(spriteGuild);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历资源包里的资源记录其中图集的散图 guid
|
||||
foreach (var pair in writeData.FileToObjects)
|
||||
{
|
||||
foreach (ObjectIdentifier objectIdentifier in pair.Value)
|
||||
{
|
||||
string path = AssetDatabase.GUIDToAssetPath(objectIdentifier.guid);
|
||||
Object asset = AssetDatabase.LoadAssetAtPath<Object>(path);
|
||||
if (asset is SpriteAtlas)
|
||||
{
|
||||
List<string> 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<ObjectIdentifier> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 移除图集引用的精力图片对象
|
||||
foreach (var pair in writeData.FileToObjects)
|
||||
{
|
||||
List<ObjectIdentifier> 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)
|
||||
{
|
||||
// 删除图集散图的冗余纹理
|
||||
objectIdentifiers.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return ReturnCode.Success;
|
||||
}
|
||||
}
|
||||
return ReturnCode.Success;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,10 +9,10 @@ namespace UnityEditor.Build.Pipeline.Tasks
|
|||
{
|
||||
public static class SBPBuildTasks
|
||||
{
|
||||
public static IList<IBuildTask> Create(string builtInShaderBundleName)
|
||||
public static IList<IBuildTask> Create(bool fixSpriteAtlasRedundancy, string builtInShaderBundleName)
|
||||
{
|
||||
var buildTasks = new List<IBuildTask>();
|
||||
|
||||
|
||||
// Setup
|
||||
buildTasks.Add(new SwitchToBuildPlatform());
|
||||
buildTasks.Add(new RebuildSpriteAtlasCache());
|
||||
|
@ -33,20 +33,21 @@ namespace UnityEditor.Build.Pipeline.Tasks
|
|||
|
||||
// Packing
|
||||
buildTasks.Add(new GenerateBundlePacking());
|
||||
buildTasks.Add(new RemoveSpriteAtlasRedundancy()); // Fix for SpriteAtlas Redundancy
|
||||
if (fixSpriteAtlasRedundancy)
|
||||
buildTasks.Add(new RemoveSpriteAtlasRedundancy());
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ namespace YooAsset.Editor
|
|||
// 开始构建
|
||||
IBundleBuildResults buildResults;
|
||||
var buildParameters = buildParametersContext.GetSBPBuildParameters();
|
||||
var taskList = SBPBuildTasks.Create(buildMapContext.Command.ShadersBundleName);
|
||||
var taskList = SBPBuildTasks.Create(buildParametersContext.Parameters.SBPParameters.FixSpriteAtlasRedundancy, buildMapContext.Command.ShadersBundleName);
|
||||
ReturnCode exitCode = ContentPipeline.BuildAssetBundles(buildParameters, buildContent, out buildResults, taskList);
|
||||
if (exitCode < 0)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue