AssetBundleHelper.cs 9.8 KB

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