AssetBundleHelper.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. namespace GFGEditor
  6. {
  7. public static class AssetBundleHelper
  8. {
  9. public static Action BuildHotUpdateDll;
  10. public const string RES_ROOT_DIR_PATH = "Assets/Res";
  11. public const int LimitByteSize = 1024*1024*4;
  12. private static List<string> excludeFileExtensions = new List<string>() { ".meta", ".moc3" , ".can3" };
  13. private static AssetBundleCollector assetBundleCollector;
  14. private static AssetBundleCollectRuler _ruler;
  15. private static AssetBundleCollectRuler ruler
  16. {
  17. get
  18. {
  19. if(_ruler == null)
  20. {
  21. _ruler = AssetBundleCollectRuler.GetData();
  22. }
  23. return _ruler;
  24. }
  25. }
  26. private static Dictionary<string, Dictionary<string, AssetBundleAssetListItem>> itemsSizeDicDic;
  27. //记录文件的bundle名
  28. private static Dictionary<string, string> fileBundleDic;
  29. public static void OnPreExecuteBuild()
  30. {
  31. EditorUtility.DisplayProgressBar("进度", "正在收集资源", 1);
  32. BuildHotUpdateDll();
  33. AssetDatabase.Refresh();
  34. itemsSizeDicDic = new Dictionary<string, Dictionary<string, AssetBundleAssetListItem>>();
  35. assetBundleCollector = AssetBundleCollector.GetData();
  36. foreach (var dir in ruler.PackBySeparately)
  37. {
  38. FileHelper.ForeachFileInDir(dir, null, (string filePath) =>
  39. {
  40. var path = filePath.Replace("\\", "/");
  41. var ext = Path.GetExtension(filePath);
  42. if(IsIgnoreFileExtension(ext))
  43. {
  44. return;
  45. }
  46. var dirPath = dir + "/";
  47. if (path.Contains(dirPath))
  48. {
  49. var indexLastDot = path.LastIndexOf('.');
  50. string key = path.Substring(0, indexLastDot);
  51. CollectDynamicFile(dir, key, path);
  52. }
  53. });
  54. }
  55. foreach (var dir in ruler.PackByTopDir)
  56. {
  57. FileHelper.ForeachFileInDir(dir, null, (string filePath) =>
  58. {
  59. var path = filePath.Replace("\\", "/");
  60. var ext = Path.GetExtension(filePath);
  61. if (IsIgnoreFileExtension(ext))
  62. {
  63. return;
  64. }
  65. var dirPath = dir + "/";
  66. if (path.Contains(dirPath))
  67. {
  68. var topPath = path.Replace(dir + "/", "");
  69. var arr = topPath.Split('/');
  70. string key = dir + "/" + arr[0];
  71. CollectDynamicFile(dir, key, path);
  72. }
  73. });
  74. }
  75. CollectNewItems();
  76. RecordFileBundle();
  77. EditorUtility.SetDirty(assetBundleCollector);
  78. AssetDatabase.SaveAssetIfDirty(assetBundleCollector);
  79. EditorUtility.ClearProgressBar();
  80. }
  81. public static void OnPostExecuteBuild()
  82. {
  83. if(fileBundleDic != null)
  84. {
  85. fileBundleDic.Clear();
  86. fileBundleDic = null;
  87. }
  88. if(itemsSizeDicDic != null)
  89. {
  90. itemsSizeDicDic.Clear();
  91. itemsSizeDicDic = null;
  92. }
  93. assetBundleCollector = null;
  94. _ruler = null;
  95. }
  96. public static void CollectDynamicFile(string dirPath, string itemKey, string assetPath)
  97. {
  98. AssetBundleAssetList assetList;
  99. AssetBundleAssetListItem newItem;
  100. Dictionary<string, AssetBundleAssetListItem> itemsSizeDic;
  101. for (var i = 0; i < assetBundleCollector.assetBundleAssetLists.Count; i++)
  102. {
  103. assetList = assetBundleCollector.assetBundleAssetLists[i];
  104. if (assetList.dir == dirPath)
  105. {
  106. foreach(var item in assetList.list)
  107. {
  108. if(item.key == itemKey)
  109. {
  110. //新增文件直接加进去
  111. if (!item.list.Contains(assetPath))
  112. {
  113. item.list.Add(assetPath);
  114. var fileSize = GetFileSize(assetPath);
  115. item.bytes += fileSize;
  116. assetList.bytes += fileSize;
  117. if (assetList.bytes >= LimitByteSize)
  118. {
  119. assetList.status = 1;
  120. }
  121. }
  122. return;
  123. }
  124. }
  125. }
  126. }
  127. //先计算每个item的大小缓存起来(item指必须打在一起的若干文件)
  128. itemsSizeDicDic.TryGetValue(dirPath, out itemsSizeDic);
  129. if (itemsSizeDic == null)
  130. {
  131. itemsSizeDic = new Dictionary<string, AssetBundleAssetListItem>();
  132. itemsSizeDicDic[dirPath] = itemsSizeDic;
  133. }
  134. itemsSizeDic.TryGetValue(itemKey, out newItem);
  135. if (newItem == null)
  136. {
  137. newItem = new AssetBundleAssetListItem();
  138. newItem.key = itemKey;
  139. newItem.list = new List<string>();
  140. itemsSizeDic[itemKey] = newItem;
  141. }
  142. newItem.list.Add(assetPath);
  143. newItem.bytes += GetFileSize(assetPath);
  144. }
  145. public static void CollectNewItems()
  146. {
  147. foreach(var t in itemsSizeDicDic)
  148. {
  149. CollectNewItems(t.Key, t.Value);
  150. }
  151. }
  152. public static void CollectNewItems(string dirPath, Dictionary<string, AssetBundleAssetListItem> itemsSizeDic)
  153. {
  154. foreach(var newItem in itemsSizeDic.Values)
  155. {
  156. bool toNew = true;
  157. AssetBundleAssetList assetList;
  158. //先找已存在的AB
  159. for (var i = 0; i < assetBundleCollector.assetBundleAssetLists.Count; i++)
  160. {
  161. assetList = assetBundleCollector.assetBundleAssetLists[i];
  162. if (assetList.dir == dirPath)
  163. {
  164. if (assetList.status <= 0)
  165. {
  166. if (assetList.bytes < LimitByteSize && newItem.bytes < LimitByteSize)
  167. {
  168. assetList.list.Add(newItem);
  169. assetList.bytes += newItem.bytes;
  170. if (assetList.bytes >= LimitByteSize)
  171. {
  172. assetList.status = 1;
  173. }
  174. toNew = false;
  175. }
  176. }
  177. }
  178. }
  179. if(toNew)
  180. {
  181. //新建一个AB
  182. assetList = new AssetBundleAssetList();
  183. assetList.dir = dirPath;
  184. assetList.list = new List<AssetBundleAssetListItem>();
  185. assetList.list.Add(newItem);
  186. assetList.bytes += newItem.bytes;
  187. if (assetList.bytes >= LimitByteSize)
  188. {
  189. assetList.status = 1;
  190. }
  191. assetBundleCollector.assetBundleAssetLists.Add(assetList);
  192. }
  193. }
  194. }
  195. private static void RecordFileBundle()
  196. {
  197. fileBundleDic = new Dictionary<string, string>();
  198. AssetBundleAssetList assetList;
  199. for (var i = 0; i < assetBundleCollector.assetBundleAssetLists.Count; i++)
  200. {
  201. assetList = assetBundleCollector.assetBundleAssetLists[i];
  202. foreach (var item in assetList.list)
  203. {
  204. foreach (var assetPath in item.list)
  205. {
  206. var bundleName = assetList.dir + "_" + i;
  207. fileBundleDic[assetPath] = bundleName;
  208. //UnityEngine.Debug.Log($"assetPath :{assetPath}");
  209. //UnityEngine.Debug.Log($"bundleName :{bundleName}");
  210. }
  211. }
  212. }
  213. }
  214. public static int GetFileSize(string filePath)
  215. {
  216. //var filePath = Path.Combine(Environment.CurrentDirectory, assetPath);
  217. //filePath = filePath.Replace("\\", "/");
  218. var ext = Path.GetExtension(filePath);
  219. byte[] bytes = File.ReadAllBytes(filePath);
  220. var fileSize = bytes.Length;
  221. if (ext == ".jpg")
  222. {
  223. //unity处理jpg后会变的很大
  224. fileSize *= 5;
  225. }
  226. return fileSize;
  227. }
  228. public static string GetBundleName(string assetPath)
  229. {
  230. var path = assetPath.Replace("\\", "/");
  231. foreach (var dir in ruler.PackByCollecter)
  232. {
  233. var dirPath = dir + "/";
  234. if (path.Contains(dirPath))
  235. {
  236. return dir;
  237. }
  238. }
  239. string bundleName = null;
  240. if (fileBundleDic != null)
  241. {
  242. fileBundleDic.TryGetValue(assetPath, out bundleName);
  243. }
  244. return bundleName;
  245. }
  246. public static bool IsIgnoreFileExtension(string extension)
  247. {
  248. return excludeFileExtensions.Contains(extension);
  249. }
  250. }
  251. }