AssetBundleHelper.cs 10 KB

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