当前位置:   article > 正文

[游戏开发][Unity]Assetbundle打包篇(3)打包资源收集_unity 资源打包 api

unity 资源打包 api

目录

打包与资源加载框架目录

前言

本篇文章是整个打包的核心,我们需要告诉打包API打包目标是什么,在整理目标的时候,可以定制化各种各样的收集策略,以及剔除策略、引用策略等。

正文开始

首先要明确,我们的最终目标是收集打包资源。但请注意,该步骤收集的不是最终调用打包接口的参数2。而是包含了我们自己自己需要的一些信息,当然,其中包含了参数2需要的信息。

AssetBundleManifest.BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);

下面是收集资源的代码,非常重要!!!仔细看,多看,该方法的返回值是List<AssetInfo>

  1. private List<AssetInfo> GetBuildMap()
  2. {
  3. int progressBarCount = 0;
  4. Dictionary<string, AssetInfo> allAsset = new Dictionary<string, AssetInfo>();
  5. // 获取所有的收集路径,该方法在上一篇文章里有代码
  6. List<string> collectPathList = CollectionSettingData.GetAllCollectPath();
  7. if (collectPathList.Count == 0)
  8. throw new Exception("[BuildPatch] 配置的打包路径列表为空");
  9. // 获取所有资源
  10. string[] guids = AssetDatabase.FindAssets(string.Empty, collectPathList.ToArray());
  11. foreach (string guid in guids)
  12. {
  13. string mainAssetPath = AssetDatabase.GUIDToAssetPath(guid);
  14. if (CollectionSettingData.IsIgnoreAsset(mainAssetPath))
  15. continue;
  16. if (ValidateAsset(mainAssetPath) == false)
  17. continue;
  18. List<AssetInfo> depends = GetDependencies(mainAssetPath);
  19. for (int i = 0; i < depends.Count; i++)
  20. {
  21. AssetInfo assetInfo = depends[i];
  22. if (assetInfo.AssetPath.Contains("Activity.RU"))
  23. {
  24. throw new Exception($"[BuildPatch] Contain: {assetInfo.AssetPath} by {mainAssetPath}");
  25. }
  26. if (allAsset.ContainsKey(assetInfo.AssetPath))
  27. {
  28. AssetInfo cacheInfo = allAsset[assetInfo.AssetPath];
  29. cacheInfo.DependCount++;
  30. }
  31. else
  32. {
  33. allAsset.Add(assetInfo.AssetPath, assetInfo);
  34. }
  35. }
  36. // 进度条
  37. progressBarCount++;
  38. EditorUtility.DisplayProgressBar("进度", $"依赖文件分析:{progressBarCount}/{guids.Length}", (float)progressBarCount / guids.Length);
  39. }
  40. EditorUtility.ClearProgressBar();
  41. progressBarCount = 0;
  42. // 移除零依赖的资源
  43. List<string> removeList = new List<string>();
  44. foreach (KeyValuePair<string, AssetInfo> pair in allAsset)
  45. {
  46. if (pair.Value.IsCollectAsset)
  47. continue;
  48. if (pair.Value.DependCount == 0)
  49. removeList.Add(pair.Value.AssetPath);
  50. else if (pair.Value.AssetPath.ToLower().Contains("assets/worksart/panel/atlas/"))
  51. removeList.Add(pair.Value.AssetPath);
  52. }
  53. for (int i = 0; i < removeList.Count; i++)
  54. {
  55. allAsset.Remove(removeList[i]);
  56. }
  57. // 设置AssetBundleLabel资源标签
  58. foreach (KeyValuePair<string, AssetInfo> pair in allAsset)
  59. {
  60. SetAssetBundleLabelAndVariant(pair.Value);
  61. SetAssetEncrypt(pair.Value);
  62. // 进度条
  63. progressBarCount++;
  64. EditorUtility.DisplayProgressBar("进度", $"设置资源标签:{progressBarCount}/{allAsset.Count}", (float)progressBarCount / allAsset.Count);
  65. }
  66. EditorUtility.ClearProgressBar();
  67. progressBarCount = 0;
  68. // Dictionary<string, long> dicSortedBySize = allAsset.OrderBy(o => o.Value.sizeKB).ToDictionary(p => p.Key, o => o.Value.sizeKB);
  69. // foreach (KeyValuePair<string, long> pair in dicSortedBySize)
  70. // {
  71. // Log($"AB asset: {pair.Key}, sizeKB: {pair.Value}");
  72. // }
  73. // 返回结果
  74. return allAsset.Values.ToList();
  75. }

下面是获取所有依赖的方法

  1. private List<AssetInfo> GetDependencies(string assetPath)
  2. {
  3. List<AssetInfo> depends = new List<AssetInfo>();
  4. string[] dependArray = AssetDatabase.GetDependencies(assetPath, true);
  5. foreach (string dependPath in dependArray)
  6. {
  7. if (ValidateAsset(dependPath))
  8. {
  9. AssetInfo assetInfo = new AssetInfo(dependPath);
  10. depends.Add(assetInfo);
  11. }
  12. }
  13. return depends;
  14. }

下面是设置AssetBundleLabel的方法,请注意,CollectionSettingData.GetAssetBundleLabel在上一篇文章中有代码

variant变量就是ab包的后缀,一般.unity3d

还有一点非常重要,ab包的名字除了可以使用字符串,还可以使用MD5码

  1. private void SetAssetBundleLabelAndVariant(AssetInfo assetInfo)
  2. {
  3. string label = CollectionSettingData.GetAssetBundleLabel(assetInfo.AssetPath);
  4. string variant = VariantCollector.GetVariantByAssetPath(label);
  5. if (string.IsNullOrEmpty(variant))
  6. {
  7. variant = PatchDefine.AssetBundleDefaultVariant;
  8. }
  9. else
  10. {
  11. label = label.Replace(variant, "");
  12. variant = variant.Substring(1);
  13. }
  14. if (IsNameByHash)
  15. assetInfo.AssetBundleLabel = HashUtility.BytesMD5(Encoding.UTF8.GetBytes(label));
  16. else
  17. assetInfo.AssetBundleLabel = label;
  18. assetInfo.ReadableLabel = label;
  19. assetInfo.AssetBundleVariant = variant;
  20. assetInfo.bundlePos = CollectionSettingData.GetAssetBundlePos(assetInfo.AssetPath);
  21. // assetInfo.sizeKB = EditorTools.GetFileSize(assetInfo.AssetPath);
  22. }

下面是AssetInfo的代码

  1. public class AssetInfo
  2. {
  3. public string AssetPath { private set; get; }
  4. public bool IsCollectAsset { private set; get; }
  5. public bool IsSceneAsset { private set; get; }
  6. public bool IsVideoAsset { private set; get; }
  7. /// <summary>
  8. /// 被依赖次数
  9. /// </summary>
  10. public int DependCount = 0;
  11. /// <summary>
  12. /// AssetBundle标签
  13. /// </summary>
  14. public string AssetBundleLabel = null;
  15. /// <summary>
  16. /// AssetBundle变体
  17. /// </summary>
  18. public string AssetBundleVariant = null;
  19. /// <summary>
  20. /// AssetBundle存放位置
  21. /// </summary>
  22. public EBundlePos bundlePos = EBundlePos.buildin;
  23. public string ReadableLabel = "undefined";
  24. public EEncryptMethod EncryptMethod;
  25. public AssetInfo(string assetPath)
  26. {
  27. AssetPath = assetPath;
  28. IsCollectAsset = CollectionSettingData.IsCollectAsset(assetPath);
  29. IsSceneAsset = AssetDatabase.GetMainAssetTypeAtPath(assetPath) == typeof(SceneAsset);
  30. IsVideoAsset = AssetDatabase.GetMainAssetTypeAtPath(assetPath) == typeof(UnityEngine.Video.VideoClip);
  31. }
  32. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/82330
推荐阅读
相关标签
  

闽ICP备14008679号