BundleRefsController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using XGame.Editor.Asset;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace XGame.Editor.Build.AssetBundles
  9. {
  10. /// <summary>
  11. /// 收集bundle引用
  12. /// </summary>
  13. internal class BundleRefsController
  14. {
  15. private BundleContext _context;
  16. private AssetBundleCollector _collector;
  17. public BundleRefsController(BundleContext context, AssetBundleCollector collector)
  18. {
  19. _context = context;
  20. _collector = collector;
  21. }
  22. internal void CollectForResAddressable()
  23. {
  24. _collector.CollectBundleBuilds(false);
  25. var assetsMap = new Dictionary<string, long>();
  26. var dependenciesMap = new Dictionary<long, string[]>();
  27. var refBundlesMap = new Dictionary<string, HashSet<long>>();
  28. var tmpDependencies = new HashSet<string>();
  29. foreach (var item in _context.bundleDataMap)
  30. {
  31. var assetNames = item.Value.assetNames;
  32. if (assetNames.Length == 1)
  33. continue;
  34. var dependencies = AssetDatabase.GetDependencies(item.Value.assetNames, true);
  35. tmpDependencies.Clear();
  36. foreach (var dependency in dependencies)
  37. {
  38. if (dependency.Contains(PathDefine.ResAddressableName) == false ||
  39. Asset.FileUtil.IsFileIgnore(dependency) ||
  40. assetNames.Contains(dependency))
  41. {
  42. continue;
  43. }
  44. tmpDependencies.Add(dependency);
  45. }
  46. if (tmpDependencies.Count > 0)
  47. {
  48. var bundleId = item.Key;
  49. dependenciesMap.Add(bundleId, tmpDependencies.ToArray());
  50. foreach (var assetPath in assetNames)
  51. {
  52. assetsMap.Add(assetPath, bundleId);
  53. }
  54. tmpDependencies.Clear();
  55. }
  56. }
  57. foreach (var item in dependenciesMap)
  58. {
  59. var bundleId = item.Key;
  60. var dependencies = item.Value;
  61. foreach (var dependency in dependencies)
  62. {
  63. if (assetsMap.TryGetValue(dependency, out var depBundleId) && depBundleId != bundleId)
  64. {
  65. if (!refBundlesMap.TryGetValue(dependency, out var depBundleIds))
  66. {
  67. depBundleIds = new HashSet<long>();
  68. refBundlesMap.Add(dependency, depBundleIds);
  69. }
  70. depBundleIds.Add(bundleId);
  71. }
  72. }
  73. }
  74. var sb = new System.Text.StringBuilder();
  75. sb.Append($"被引用的Addressable资源数量:{refBundlesMap.Count}\n");
  76. foreach (var item in refBundlesMap)
  77. {
  78. //sb.Clear();
  79. sb.Append($"Asset:{item.Key} 被{item.Value.Count}个Assetbundle引用。 BundleNames:");
  80. foreach (var bundleId in item.Value)
  81. {
  82. sb.Append($" [{_context.bundleDataMap[bundleId].originBundleName}]");
  83. }
  84. sb.Append("\n");
  85. //Debug.Log(sb.ToString());
  86. }
  87. OutputText(sb.ToString());
  88. }
  89. private void OutputText(string text)
  90. {
  91. try
  92. {
  93. var path = Path.GetTempPath() + Path.GetRandomFileName();
  94. File.WriteAllText(path, text);
  95. #if UNITY_EDITOR_WIN
  96. var proc = System.Diagnostics.Process.Start("notepad.exe", path);
  97. #else
  98. Debug.Log($"Output:{path}");
  99. #endif
  100. }
  101. catch (Exception e)
  102. {
  103. Debug.LogException(e);
  104. }
  105. }
  106. }
  107. }