AssetBundleMap.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Security.Cryptography;
  9. /// <summary>
  10. /// 构建ab包
  11. /// </summary>
  12. public class AssetBundleMap : EditorWindow
  13. {
  14. /// <summary>
  15. /// shader都打入这个包
  16. /// </summary>
  17. private const string ShaderAbName = "shader.unity3d";
  18. /// <summary>
  19. /// 材质球都打入这个包
  20. /// </summary>
  21. private const string MatAbName = "mat.unity3d";
  22. /// <summary>
  23. /// 临时ab生成目录
  24. /// </summary>
  25. private static readonly string TempAssetBundlePath = Application.dataPath + "/../assetbundle";
  26. private static readonly string TempAssetBundleBakPath = Application.dataPath + "/../assetbundleBak";
  27. private static readonly string DefaultBakDirPath = "Default";
  28. private static readonly string ManifestFileExtension = ".manifest";
  29. /// <summary>
  30. /// 使用lz4压缩
  31. /// </summary>
  32. private static readonly BuildAssetBundleOptions BuildOptions = BuildAssetBundleOptions.DeterministicAssetBundle |
  33. BuildAssetBundleOptions.ChunkBasedCompression;
  34. /// <summary>
  35. /// 构建全部资源
  36. /// </summary>
  37. [MenuItem("AssetBundle/BuildMap/BuildAllAssetBundles")]
  38. public static void BuildAllAssetBundles(VersionCode resVersionCode)
  39. {
  40. Debug.Log("<color=green>================ BuildAllAssetBundles Start================</color>");
  41. AssetDatabase.Refresh();
  42. copyBakAssetsToAssetBundle();
  43. AssetBundleUtil.CleanUnusedAB();
  44. var buildMap = getAllBuildMap(true);
  45. var abm = BuildPipeline.BuildAssetBundles(TempAssetBundlePath, buildMap, BuildOptions,
  46. EditorUserBuildSettings.activeBuildTarget);
  47. if (abm != null)
  48. {
  49. AssetBundleUtil.GetAllAssetsNameInAssetBundle(abm, resVersionCode);
  50. copyAssetBundleToBakAssets();
  51. copyBakAssetsToStreamingAssets();
  52. Debug.Log("<color=green>================ BuildAllAssetBundles Success================</color>");
  53. }
  54. else
  55. {
  56. Debug.Log("<color=green>================ BuildAllAssetBundles Fail================</color>");
  57. throw new Pack.PackException("BuildAssetBundles 失败");
  58. }
  59. }
  60. /// <summary>
  61. /// 只打包lua相关的
  62. /// 在只修改了lua代码,但是未修改资源的情况下使用
  63. /// </summary>
  64. [MenuItem("AssetBundle/Build Map/Build Lua AssetBundles")]
  65. public static void BuildLuaAssetBundles(VersionCode resVersionCode)
  66. {
  67. AssetDatabase.Refresh();
  68. copyBakAssetsToAssetBundle();
  69. AssetBundleUtil.CleanUnusedAB();
  70. var buildMap = getLuaBuildMap(true);
  71. var abm = BuildPipeline.BuildAssetBundles(TempAssetBundlePath, buildMap, BuildOptions,
  72. EditorUserBuildSettings.activeBuildTarget);
  73. if (abm == null)
  74. {
  75. throw new Pack.PackException("BuildAssetBundles 失败");
  76. }
  77. AssetBundleUtil.GetAllAssetsNameInAssetBundle(abm, resVersionCode);
  78. copyAssetBundleToBakAssets();
  79. copyBakAssetsToStreamingAssets();
  80. }
  81. private static void copyBakAssetsToAssetBundle()
  82. {
  83. if (Directory.Exists(TempAssetBundlePath))
  84. {
  85. Directory.Delete(TempAssetBundlePath, true);
  86. }
  87. Directory.CreateDirectory(TempAssetBundlePath);
  88. string path = AssetsObscureUtil.GetUniqueValue();
  89. string dirPath;
  90. BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
  91. if (!s_AssetbundleSavePaths.ContainsKey(buildTarget))
  92. {
  93. return;
  94. }
  95. if (string.IsNullOrEmpty(path))
  96. {
  97. dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + DefaultBakDirPath;
  98. }
  99. else
  100. {
  101. dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + path;
  102. }
  103. if (!Directory.Exists(dirPath))
  104. {
  105. return;
  106. }
  107. foreach (string file in Directory.GetFiles(dirPath))
  108. {
  109. string fileName = Path.GetFileName(file);
  110. if (fileName == "assetbundle" || Path.GetExtension(file) == ".manifest")
  111. {
  112. string des = Path.Combine(TempAssetBundlePath, fileName);
  113. File.Copy(file, des, true);
  114. }
  115. }
  116. }
  117. private static void copyAssetBundleToBakAssets()
  118. {
  119. string path = AssetsObscureUtil.GetUniqueValue();
  120. string dirPath;
  121. BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
  122. if (!s_AssetbundleSavePaths.ContainsKey(buildTarget))
  123. {
  124. return;
  125. }
  126. if (string.IsNullOrEmpty(path))
  127. {
  128. dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + DefaultBakDirPath;
  129. }
  130. else
  131. {
  132. dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + path;
  133. }
  134. if (!Directory.Exists(dirPath))
  135. {
  136. Directory.CreateDirectory(dirPath);
  137. }
  138. foreach (string file in Directory.GetFiles(TempAssetBundlePath))
  139. {
  140. string fileName = Path.GetFileName(file);
  141. string des = Path.Combine(dirPath, fileName);
  142. File.Copy(file, des, true);
  143. }
  144. }
  145. /// <summary>
  146. /// 拷贝打包好的资源到目标目录
  147. /// </summary>
  148. private static void copyBakAssetsToStreamingAssets()
  149. {
  150. string path = string.Empty;
  151. if (Directory.Exists(Application.streamingAssetsPath))
  152. {
  153. foreach (var item in s_AssetbundleSavePaths)
  154. {
  155. path = Application.streamingAssetsPath + item.Value;
  156. DeleteDirectoryAssets(path, true);
  157. }
  158. }
  159. BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
  160. if (!s_AssetbundleSavePaths.ContainsKey(buildTarget))
  161. {
  162. return;
  163. }
  164. string assetBundlePath = Application.streamingAssetsPath + s_AssetbundleSavePaths[buildTarget];
  165. if (!Directory.Exists(assetBundlePath))
  166. {
  167. Directory.CreateDirectory(assetBundlePath);
  168. }
  169. path = AssetsObscureUtil.GetUniqueValue();
  170. string dirPath;
  171. if (string.IsNullOrEmpty(path))
  172. {
  173. dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + DefaultBakDirPath;
  174. }
  175. else
  176. {
  177. dirPath = TempAssetBundleBakPath + s_AssetbundleSavePaths[buildTarget] + path;
  178. }
  179. // 拷贝临时打包目录中的ab资源到对应的StreamingAssets目录下
  180. foreach (string file in Directory.GetFiles(dirPath))
  181. {
  182. if (Path.GetExtension(file) != ".manifest")
  183. {
  184. string fileName = Path.GetFileName(file);
  185. if (fileName == "assetbundle")
  186. {
  187. fileName = AssetsObscureUtil.GetABFileName(fileName);
  188. }
  189. else if (fileName == "assetsmapping.bytes")
  190. {
  191. EncryptAssetsmapping(file, Path.Combine(assetBundlePath, fileName));
  192. continue;
  193. }
  194. string des = Path.Combine(assetBundlePath, fileName);
  195. File.Copy(file, des, true);
  196. WriteMeaninglessDataToFile(des);
  197. }
  198. }
  199. AssetDatabase.Refresh();
  200. }
  201. private static Dictionary<BuildTarget, string> s_AssetbundleSavePaths = new Dictionary<BuildTarget, string>()
  202. {
  203. {BuildTarget.iOS, "/ios/"},
  204. {BuildTarget.Android, "/AssetsAndroid/"},
  205. {BuildTarget.StandaloneWindows64, "/AssetsPC/"},
  206. {BuildTarget.WebGL, "/AssetsPC/"},
  207. };
  208. private static void EncryptAssetsmapping(string destPath, string filePath)
  209. {
  210. byte[] bytes = File.ReadAllBytes(destPath);
  211. byte value;
  212. int length = bytes.Length;
  213. for (int i = 0, iMax = Mathf.FloorToInt(length * 0.5f); i < iMax; i += 2)
  214. {
  215. value = bytes[i];
  216. bytes[i] = bytes[length - i - 1];
  217. bytes[length - i - 1] = value;
  218. }
  219. using (var fs = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
  220. {
  221. fs.Write(bytes, 0, length);
  222. }
  223. }
  224. /// <summary>
  225. /// 这个函数不能适配所有文件大小,理论上来说需要文件小于 (int.MaxValue / 2) byte
  226. /// </summary>
  227. /// <param name="filePath"></param>
  228. private static void WriteMeaninglessDataToFile(string filePath)
  229. {
  230. if (!AssetsObscureUtil.IsObscure()) return;
  231. string fileName = Path.GetFileNameWithoutExtension(filePath);
  232. ulong offset = AssetsObscureUtil.GetABOffset(fileName);
  233. if (offset <= 0) return;
  234. uint offsetMain = (uint)AssetsObscureUtil.GetObscureOffsetMin();
  235. if (offset <= offsetMain) return;
  236. byte[] bytes = File.ReadAllBytes(filePath);
  237. int length = bytes.Length;
  238. if (length <= 0) return;
  239. byte[] offsetBytes = new byte[offset];
  240. Array.Copy(bytes, offsetBytes, offsetMain);
  241. byte[] md5Bytes;
  242. using(var md5 = new MD5CryptoServiceProvider())
  243. {
  244. UTF8Encoding encoding = new UTF8Encoding(false);
  245. md5Bytes = md5.ComputeHash(bytes);
  246. }
  247. uint md5Length = (uint)md5Bytes.Length;
  248. int idx = 0;
  249. for (ulong i = offsetMain + 1,
  250. iMax = (offset - offsetMain);
  251. i < iMax; i++)
  252. {
  253. idx = idx + md5Bytes[i % md5Length];
  254. if (idx >= length - 1)
  255. {
  256. idx = idx - length + 1;
  257. }
  258. offsetBytes[i] = bytes[idx];
  259. }
  260. using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite))
  261. {
  262. fs.Write(offsetBytes, 0, (int)offset);
  263. fs.Write(bytes, 0, length);
  264. }
  265. }
  266. private struct AssetMap
  267. {
  268. /// <summary>
  269. /// key => ab name
  270. /// value => asset names
  271. /// </summary>
  272. public Dictionary<string, HashSet<string>> Maps;
  273. /// <summary>
  274. /// 记录重复的情况
  275. /// </summary>
  276. public HashSet<string> AllAssets;
  277. }
  278. /// <summary>
  279. /// 构建全部资源
  280. /// </summary>
  281. /// <param name="showProcessBar"></param>
  282. /// <returns></returns>
  283. private static AssetBundleBuild[] getAllBuildMap(bool showProcessBar)
  284. {
  285. if (showProcessBar)
  286. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Start...", 0.1f);
  287. AssetMap maps = new AssetMap();
  288. maps.Maps = new Dictionary<string, HashSet<string>>(1024);
  289. maps.AllAssets = new HashSet<string>();
  290. // 增加公共shader
  291. addBuildAssetsCommon(maps, ShaderAbName, Constants.ShaderDir, "*.shader");
  292. addBuildAssetsCommon(maps, ShaderAbName, Constants.ShaderDir, "*.shadervariants");
  293. // font
  294. if (showProcessBar)
  295. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Font...", 0.2f);
  296. addBuildAssetsFonts(maps);
  297. addBuildAssetsCommon(maps, "commonmat.unity3d", Constants.CommonMaterialDir, "*.mat");
  298. // config
  299. if (showProcessBar)
  300. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Config...", 0.3f);
  301. addBuildAssetsCommon(maps, "config.unity3d", Constants.CsvConfig, "*.csv");
  302. addBuildAssetsCommon(maps, "xml.unity3d", Constants.XmlConfig, "*.xml");
  303. // audio
  304. if (showProcessBar)
  305. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Audio...", 0.4f);
  306. addBuildAssetsBGM(maps);
  307. addBuildAssetsCommon(maps, "UI_Audio.unity3d", Constants.UIAudioPath, "*.ogg");
  308. addBuildAssetsCommon(maps, "Fight_Audio.unity3d", Constants.FightAudioPath, "*.ogg");
  309. if (showProcessBar)
  310. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Icons...", 0.5f);
  311. addBuildAssetsIcons(maps);
  312. if (showProcessBar)
  313. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Camera...", 0.6f);
  314. addBuildAssetsCamera(maps);
  315. if (showProcessBar)
  316. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Animator...", 0.65f);
  317. addBuildAssetsAnimator(maps);
  318. if (showProcessBar)
  319. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Actor...", 0.7f);
  320. addBuildAssetsActor(maps);
  321. if (showProcessBar)
  322. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Effect...", 0.75f);
  323. addBuildAssetsEffect(maps);
  324. if (showProcessBar)
  325. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "UI...", 0.8f);
  326. addBuildAssetsUI(maps);
  327. if (showProcessBar)
  328. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Scene...", 0.85f);
  329. addBuildAssetsScene(maps);
  330. if (showProcessBar)
  331. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Lua...", 0.9f);
  332. addBuildAssetsLua(maps);
  333. List<AssetBundleBuild> bm = new List<AssetBundleBuild>(maps.Maps.Count);
  334. foreach (var items in maps.Maps)
  335. {
  336. AssetBundleBuild bundle = new AssetBundleBuild();
  337. bundle.assetBundleName = AssetsObscureUtil.GetABFileName(items.Key);
  338. bundle.assetNames = items.Value.ToArray();
  339. if (bundle.assetNames.Length == 0)
  340. {
  341. Debug.LogWarning(items.Key + " empty assetNames");
  342. continue;
  343. }
  344. bm.Add(bundle);
  345. }
  346. if (showProcessBar)
  347. {
  348. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Finish...", 1f);
  349. EditorUtility.ClearProgressBar();
  350. }
  351. return bm.ToArray();
  352. }
  353. /// <summary>
  354. /// 只构建lua相关的资源
  355. /// </summary>
  356. /// <param name="showProcessBar"></param>
  357. /// <returns></returns>
  358. private static AssetBundleBuild[] getLuaBuildMap(bool showProcessBar)
  359. {
  360. if (showProcessBar)
  361. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Start...", 0.1f);
  362. AssetMap maps = new AssetMap();
  363. maps.Maps = new Dictionary<string, HashSet<string>>();
  364. maps.AllAssets = new HashSet<string>();
  365. if (showProcessBar)
  366. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Lua...", 0.9f);
  367. addBuildAssetsLua(maps);
  368. List<AssetBundleBuild> bm = new List<AssetBundleBuild>(maps.Maps.Count);
  369. foreach (var items in maps.Maps)
  370. {
  371. AssetBundleBuild bundle = new AssetBundleBuild();
  372. bundle.assetBundleName = items.Key;
  373. bundle.assetNames = items.Value.ToArray();
  374. if (bundle.assetNames.Length == 0)
  375. {
  376. Debug.LogWarning(items.Key + " empty assetNames");
  377. continue;
  378. }
  379. bm.Add(bundle);
  380. }
  381. if (showProcessBar)
  382. {
  383. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Finish...", 1f);
  384. EditorUtility.ClearProgressBar();
  385. }
  386. bm.Sort(assetBundleBuildSort);
  387. return bm.ToArray();
  388. }
  389. private static AssetBundleBuildSort s_AssetBundleBuildSort = null;
  390. private static AssetBundleBuildSort assetBundleBuildSort
  391. {
  392. get
  393. {
  394. if (s_AssetBundleBuildSort == null)
  395. {
  396. s_AssetBundleBuildSort = new AssetBundleBuildSort();
  397. }
  398. return s_AssetBundleBuildSort;
  399. }
  400. }
  401. private class AssetBundleBuildSort : IComparer<AssetBundleBuild>
  402. {
  403. public int Compare(AssetBundleBuild x, AssetBundleBuild y)
  404. {
  405. return string.Compare(x.assetBundleName, y.assetBundleName);
  406. }
  407. }
  408. #region 处理各个模块的assetbundle
  409. private static void addBuildAssetsCommon(AssetMap maps, string abName, string dir,
  410. string filter)
  411. {
  412. string[] fileList = FileUtils.TraverseAllFiles(dir, filter);
  413. for (int idx = 0; idx < fileList.Length; idx++)
  414. {
  415. string fullPath = fileList[idx];
  416. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  417. if (relativePath.EndsWith(".meta")) continue;
  418. addData(maps, abName, relativePath);
  419. }
  420. }
  421. private static void addData(AssetMap maps, string assetBundleName, string assetName)
  422. {
  423. // 重复资源不能加入;按照资源初始化顺序写入
  424. if (maps.AllAssets.Contains(assetName))
  425. {
  426. return;
  427. }
  428. maps.AllAssets.Add(assetName);
  429. if (!maps.Maps.ContainsKey(assetBundleName))
  430. {
  431. maps.Maps[assetBundleName] = new HashSet<string>();
  432. }
  433. maps.Maps[assetBundleName].Add(assetName);
  434. }
  435. private static void addBuildAssetsFonts(AssetMap maps)
  436. {
  437. string[] fileList = FileUtils.TraverseFiles(Constants.FontDir, "*.*");
  438. for (int idx = 0; idx < fileList.Length; idx++)
  439. {
  440. string fullPath = fileList[idx];
  441. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  442. if (relativePath.Contains(".meta")) continue;
  443. addData(maps, "font.unity3d", relativePath);
  444. }
  445. string[] dirList = Directory.GetDirectories(Constants.FontDir);
  446. for (int idx = 0; idx < dirList.Length; idx++)
  447. {
  448. string fullPath = dirList[idx];
  449. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  450. string abName = relativePath.Replace("Assets/", "").Replace("/", "_") + ".unity3d";
  451. string[] files = FileUtils.TraverseFiles(relativePath, "*.*");
  452. for (int jdx = 0; jdx < files.Length; jdx++)
  453. {
  454. string filePathName = files[jdx];
  455. if (filePathName.Contains(".meta")) continue;
  456. string fileRelativePath = FileUtils.ExtractAssetRelativePath(filePathName);
  457. addData(maps, abName, fileRelativePath);
  458. }
  459. }
  460. }
  461. private static void addBuildAssetsBGM(AssetMap maps)
  462. {
  463. string[] files = FileUtils.TraverseAllFiles(Constants.BGMAudioPath, "*.ogg");
  464. for (int idx = 0; idx < files.Length; idx++)
  465. {
  466. string fullPath = files[idx];
  467. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  468. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + ".unity3d";
  469. addData(maps, abName, relativePath);
  470. }
  471. }
  472. private static void addBuildAssetsIcons(AssetMap maps)
  473. {
  474. string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
  475. string[] dirs = Directory.GetDirectories(Constants.IconDir, "*", SearchOption.AllDirectories);
  476. for (int idx = 0; idx < dirs.Length; idx++)
  477. {
  478. string dirName = dirs[idx];
  479. string abName = FileUtils.ExtractPureName(dirName) + "_icons.unity3d";
  480. if (allAbNames.Contains(abName))
  481. {
  482. AssetDatabase.RemoveAssetBundleName(abName, true);
  483. }
  484. string[] files = FileUtils.TraverseAllFiles(dirName, "*.png");
  485. for (int jdx = 0; jdx < files.Length; jdx++)
  486. {
  487. string fullPath = files[jdx];
  488. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  489. addData(maps, abName, relativePath);
  490. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  491. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  492. {
  493. string dependencyAssetName = dependencyAssets[kdx];
  494. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  495. fileType = fileType.ToLower();
  496. if (fileType == "shader")
  497. {
  498. addData(maps, ShaderAbName, dependencyAssetName);
  499. }
  500. else if (fileType == "mat")
  501. {
  502. addData(maps, MatAbName, dependencyAssetName);
  503. }
  504. else if (fileType != "cs")
  505. {
  506. addData(maps, abName, dependencyAssetName);
  507. }
  508. }
  509. }
  510. }
  511. }
  512. private static void addBuildAssetsAnimator(AssetMap maps)
  513. {
  514. string[] dirs = Directory.GetDirectories(Constants.AnimatorPath, "*", SearchOption.AllDirectories);
  515. for (int i = 0; i < dirs.Length; i++)
  516. {
  517. string dirName = dirs[i];
  518. string[] files = FileUtils.TraverseAllFiles(dirName, "*.controller");
  519. for (int idx = 0; idx < files.Length; idx++)
  520. {
  521. string fullPath = files[idx];
  522. string fileName = FileUtils.ExtractPureName(fullPath);
  523. string[] tempList = fileName.Split('_');
  524. string abName = tempList[0] + "_animator.unity3d";
  525. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  526. addData(maps, abName, relativePath);
  527. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  528. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  529. {
  530. string dependencyAssetName = dependencyAssets[jdx];
  531. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  532. fileType = fileType.ToLower();
  533. if (fileType == "shader")
  534. {
  535. addData(maps, ShaderAbName, dependencyAssetName);
  536. }
  537. else if (fileType == "mat")
  538. {
  539. addData(maps, MatAbName, dependencyAssetName);
  540. }
  541. else if (fileType != "cs")
  542. {
  543. addData(maps, abName, dependencyAssetName);
  544. }
  545. }
  546. }
  547. }
  548. }
  549. private static void addBuildAssetsActor(AssetMap maps)
  550. {
  551. string[] dirs = Directory.GetDirectories(Constants.ModelPath, "*", SearchOption.AllDirectories);
  552. for (int i = 0; i < dirs.Length; i++)
  553. {
  554. string dirName = dirs[i];
  555. string abName = FileUtils.ExtractPureName(dirName) + ".unity3d";
  556. string textureABName = FileUtils.ExtractPureName(dirName) + "_texture.unity3d";
  557. string fbxABName = FileUtils.ExtractPureName(dirName) + "_model.unity3d";
  558. string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  559. for (int idx = 0; idx < files.Length; idx++)
  560. {
  561. string fullPath = files[idx];
  562. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  563. addData(maps, abName, relativePath);
  564. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  565. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  566. {
  567. string dependencyAssetName = dependencyAssets[jdx];
  568. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  569. fileType = fileType.ToLower();
  570. if (fileType == "shader")
  571. {
  572. addData(maps, ShaderAbName, dependencyAssetName);
  573. }
  574. else if (fileType == "mat")
  575. {
  576. addData(maps, MatAbName, dependencyAssetName);
  577. }
  578. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
  579. fileType == "psd")
  580. {
  581. addData(maps, textureABName, dependencyAssetName);
  582. }
  583. else if (fileType == "fbx")
  584. {
  585. addData(maps, fbxABName, dependencyAssetName);
  586. }
  587. else if (fileType != "cs")
  588. {
  589. addData(maps, abName, dependencyAssetName);
  590. }
  591. }
  592. }
  593. }
  594. }
  595. private static void addBuildAssetsCamera(AssetMap maps)
  596. {
  597. string[] files = FileUtils.TraverseAllFiles("Assets/Content/Prefabs/Camera", "*.prefab");
  598. for (int idx = 0; idx < files.Length; idx++)
  599. {
  600. string fullPath = files[idx];
  601. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  602. string abName = "prefab_camera.unity3d";
  603. addData(maps, abName, relativePath);
  604. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  605. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  606. {
  607. string dependencyAssetName = dependencyAssets[jdx];
  608. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  609. fileType = fileType.ToLower();
  610. if (fileType == "shader")
  611. {
  612. addData(maps, ShaderAbName, dependencyAssetName);
  613. }
  614. else if (fileType == "mat")
  615. {
  616. addData(maps, MatAbName, dependencyAssetName);
  617. }
  618. else if (fileType != "cs")
  619. {
  620. addData(maps, abName, dependencyAssetName);
  621. }
  622. }
  623. }
  624. }
  625. private static void addBuildAssetsEffect(AssetMap maps)
  626. {
  627. string effectTextureABName = "effect_texture.unity3d";
  628. string effectAnimABName = "effect_dep.unity3d";
  629. string abName = "effect.unity3d";
  630. string[] dirs = Directory.GetDirectories(Constants.EffectPath, "*", SearchOption.AllDirectories);
  631. for (int idx = 0; idx < dirs.Length; idx++)
  632. {
  633. string dirName = dirs[idx];
  634. string[] fileList = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  635. for (int jdx = 0; jdx < fileList.Length; jdx++)
  636. {
  637. string fullPath = fileList[jdx];
  638. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  639. addData(maps, abName, relativePath);
  640. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  641. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  642. {
  643. string dependencyAssetName = dependencyAssets[kdx];
  644. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  645. fileType = fileType.ToLower();
  646. if (fileType == "shader")
  647. {
  648. addData(maps, ShaderAbName, dependencyAssetName);
  649. }
  650. else if (fileType == "mat")
  651. {
  652. addData(maps, MatAbName, dependencyAssetName);
  653. }
  654. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
  655. fileType == "psd")
  656. {
  657. addData(maps, effectTextureABName, dependencyAssetName);
  658. }
  659. else if (fileType != "cs")
  660. {
  661. addData(maps, effectAnimABName, dependencyAssetName);
  662. }
  663. }
  664. }
  665. }
  666. }
  667. private static void addBuildAssetsUI(AssetMap maps)
  668. {
  669. addBuildAssets3DUIPrefab(maps);
  670. addBuildAssetsUIPrefab(maps);
  671. }
  672. private static void addBuildAssets3DUIPrefab(AssetMap maps)
  673. {
  674. string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
  675. string[] dirs = Directory.GetDirectories(Constants.UI3DPath, "*", SearchOption.AllDirectories);
  676. for (int idx = 0; idx < dirs.Length; idx++)
  677. {
  678. string dirName = dirs[idx];
  679. string abName = FileUtils.ExtractPureName(dirName) + "_3duiprefab.unity3d";
  680. string textureABName = FileUtils.ExtractPureName(dirName) + "_3duiprefab_texture.unity3d";
  681. if (allAbNames.Contains(abName))
  682. {
  683. AssetDatabase.RemoveAssetBundleName(abName, true);
  684. }
  685. string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  686. for (int jdx = 0; jdx < files.Length; jdx++)
  687. {
  688. string fullPath = files[jdx];
  689. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  690. addData(maps, abName, relativePath);
  691. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  692. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  693. {
  694. string dependencyAssetName = dependencyAssets[kdx];
  695. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  696. fileType = fileType.ToLower();
  697. if (fileType == "shader")
  698. {
  699. addData(maps, ShaderAbName, dependencyAssetName);
  700. }
  701. else if (fileType == "mat")
  702. {
  703. addData(maps, MatAbName, dependencyAssetName);
  704. }
  705. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
  706. fileType == "psd")
  707. {
  708. addData(maps, textureABName, dependencyAssetName);
  709. }
  710. else if (fileType != "cs")
  711. {
  712. addData(maps, abName, dependencyAssetName);
  713. }
  714. }
  715. }
  716. }
  717. }
  718. private static void addBuildAssetsUIPrefab(AssetMap maps)
  719. {
  720. string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
  721. string[] dirs = Directory.GetDirectories(Constants.UIPath, "*", SearchOption.AllDirectories);
  722. for (int idx = 0; idx < dirs.Length; idx++)
  723. {
  724. string dirName = dirs[idx];
  725. string dirRelativeName = FileUtils.ExtractAssetRelativePath(dirName);
  726. string abName = "";
  727. if (dirRelativeName == Constants.UICommonPath)
  728. {
  729. abName = "prefabsuicommon.unity3d";
  730. }
  731. else
  732. {
  733. abName = FileUtils.ExtractPureName(dirName) + "_uiprefab.unity3d";
  734. }
  735. if (allAbNames.Contains(abName))
  736. {
  737. AssetDatabase.RemoveAssetBundleName(abName, true);
  738. }
  739. string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  740. for (int jdx = 0; jdx < files.Length; jdx++)
  741. {
  742. string fullPath = files[jdx];
  743. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  744. addData(maps, abName, relativePath);
  745. string rootParent;
  746. FileUtils.ExtractParent(relativePath, out rootParent);
  747. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  748. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  749. {
  750. string dependencyAssetName = dependencyAssets[kdx];
  751. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  752. fileType = fileType.ToLower();
  753. if (fileType == "shader")
  754. {
  755. addData(maps, ShaderAbName, dependencyAssetName);
  756. }
  757. else if (fileType == "mat")
  758. {
  759. addData(maps, MatAbName, dependencyAssetName);
  760. }
  761. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
  762. fileType == "psd")
  763. {
  764. string parent;
  765. FileUtils.ExtractParent(dependencyAssetName, out parent);
  766. parent = FileUtils.ExtractPureName(parent);
  767. string uiTextureABName = "UITexture_" + parent + ".unity3d";
  768. addData(maps, uiTextureABName, dependencyAssetName);
  769. }
  770. else if (fileType == "prefab")
  771. {
  772. string parent;
  773. FileUtils.ExtractParent(dependencyAssetName, out parent);
  774. if (parent.StartsWith(Constants.UIPath))
  775. {
  776. if (rootParent == parent)
  777. {
  778. addData(maps, abName, dependencyAssetName);
  779. }
  780. }
  781. else
  782. {
  783. addData(maps, abName, dependencyAssetName);
  784. }
  785. }
  786. else if (fileType != "cs")
  787. {
  788. addData(maps, abName, dependencyAssetName);
  789. }
  790. }
  791. }
  792. }
  793. }
  794. private static void addBuildAssetsScene(AssetMap maps)
  795. {
  796. string[] fileList = Directory.GetFiles(Constants.ScenePath, "*.unity", SearchOption.AllDirectories);
  797. for (int idx = 0; idx < fileList.Length; idx++)
  798. {
  799. string fullPath = fileList[idx];
  800. if (fullPath.Contains("UIScene") || fullPath.Contains("meta") || fullPath.Contains("SceneCG") ||
  801. fullPath.Replace('\\', '/').Contains("Scene/Other") || fullPath.Contains("WasteAsset") ||
  802. fullPath.Contains("Building"))
  803. continue;
  804. if (fullPath.Contains("game.unity") || fullPath.Contains("Loading.unity"))
  805. {
  806. continue;
  807. }
  808. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  809. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + ".unity3d";
  810. addData(maps, abName, relativePath);
  811. string scenePrefabABName =
  812. FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_prefab.unity3d";
  813. string sceneTextureABName =
  814. FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_texture.unity3d";
  815. string sceneOtherABName =
  816. FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_other.unity3d";
  817. string lightmapABName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_lm.unity3d";
  818. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  819. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  820. {
  821. string dependencyAssetName = dependencyAssets[kdx];
  822. if (dependencyAssetName.Contains("Lightmap-") ||
  823. dependencyAssetName.Contains("LightingData") ||
  824. dependencyAssetName.Contains("ReflectionProbe"))
  825. {
  826. addData(maps, lightmapABName, dependencyAssetName);
  827. }
  828. else if (dependencyAssetName.Contains("PostProcessing") && !dependencyAssetName.Contains(".cs"))
  829. {
  830. if (dependencyAssetName.Contains(".shader"))
  831. {
  832. addData(maps, ShaderAbName, dependencyAssetName);
  833. }
  834. else
  835. {
  836. addData(maps, "PostProcessing.unity3d", dependencyAssetName);
  837. }
  838. }
  839. else
  840. {
  841. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  842. fileType = fileType.ToLower();
  843. if (fileType == "shader")
  844. {
  845. addData(maps, ShaderAbName, dependencyAssetName);
  846. }
  847. else if (fileType == "mat")
  848. {
  849. addData(maps, MatAbName, dependencyAssetName);
  850. }
  851. else if (fileType == "prefab")
  852. {
  853. if (dependencyAssetName.Contains("Scenes/Scene_common"))
  854. {
  855. string sCommonAb = "scene_common_prefab.unity3d";
  856. addData(maps, sCommonAb, dependencyAssetName);
  857. }
  858. else
  859. {
  860. addData(maps, scenePrefabABName, dependencyAssetName);
  861. }
  862. }
  863. else if (fileType == "png" || fileType == "tga" || fileType == "jpg" || fileType == "tif" ||
  864. fileType == "psd")
  865. {
  866. if (dependencyAssetName.Contains("Scenes/Scene_common"))
  867. {
  868. string sCommonAb = "scene_common_texture.unity3d";
  869. addData(maps, sCommonAb, dependencyAssetName);
  870. }
  871. else
  872. {
  873. addData(maps, sceneTextureABName, dependencyAssetName);
  874. }
  875. }
  876. else if (fileType != "cs")
  877. {
  878. if (dependencyAssetName.Contains("Scenes/Scene_common"))
  879. {
  880. string sCommonAb = "scene_common_other.unity3d";
  881. addData(maps, sCommonAb, dependencyAssetName);
  882. }
  883. else
  884. {
  885. addData(maps, sceneOtherABName, dependencyAssetName);
  886. }
  887. }
  888. }
  889. }
  890. }
  891. }
  892. #endregion
  893. #region 处理lua文件
  894. private static void addBuildAssetsLua(AssetMap maps)
  895. {
  896. CopyLuaAssets(Constants.LuaDir, Constants.ABLuaDir);
  897. CopyLuaAssets(Constants.LuaLogicDir, Constants.ABLuaLogicDir, true);
  898. CopyLuaAssets(Constants.LuaPbDir, Constants.ABLuaPbDir);
  899. CopyLuaAssets(Constants.PubSec, Constants.ABPubsec);
  900. AssetDatabase.Refresh();
  901. ProcessConfigAndLua(maps);
  902. }
  903. private static void CopyLuaAssets(string sourceDir, string destDir, bool optimize = false)
  904. {
  905. string path = Application.dataPath;
  906. path = path.Replace("Assets", "");
  907. string sourceAbsDir = Path.Combine(path, sourceDir);
  908. string destAbsDir = Path.Combine(path, destDir);
  909. HashSet<string> sourceFiles = getAllFilesPathEX(sourceAbsDir);
  910. HashSet<string> destFiles = getAllFilesPathEX(destAbsDir);
  911. // 删除不再需要的文件
  912. foreach (var destFile in destFiles)
  913. {
  914. string sourceFile = destFile.Replace(destAbsDir, sourceAbsDir);
  915. string fileName = Path.GetExtension(sourceFile);
  916. if (fileName.ToLower() == ".txt")
  917. {
  918. string ext = Path.GetExtension(fileName);
  919. if (s_ValidExtMap.Contains(ext))
  920. {
  921. sourceFile = sourceFile.Remove(sourceFile.Length - 4);
  922. }
  923. }
  924. if (!sourceFiles.Contains(sourceFile))
  925. {
  926. File.Delete(destFile);
  927. string metaFilePath = destFile + ".meta";
  928. if (File.Exists(metaFilePath))
  929. {
  930. File.Delete(metaFilePath);
  931. }
  932. string folder = Path.GetDirectoryName(destFile);
  933. DeleteDirectoryAssets(folder);
  934. }
  935. }
  936. // 替换新文件
  937. foreach (var filePath in sourceFiles)
  938. {
  939. string destFilePath = filePath.Replace(sourceAbsDir, destAbsDir);
  940. string destFolder = Path.GetDirectoryName(destFilePath);
  941. if (!Directory.Exists(destFolder))
  942. {
  943. Directory.CreateDirectory(destFolder);
  944. }
  945. destFilePath = destFilePath + ".txt";
  946. if (!File.Exists(destFilePath))
  947. {
  948. File.Copy(filePath, destFilePath, true);
  949. if (optimize && destFilePath.Contains("_Generate"))
  950. {
  951. string[] lines = FileSystem.ReadFileLines(destFilePath);
  952. List<string> contents = new List<string>(lines);
  953. for (var i = contents.Count - 1; i >= 0; --i)
  954. {
  955. if (contents[i].StartsWith("---@") || contents[i].Equals(""))
  956. {
  957. contents.RemoveAt(i);
  958. }
  959. }
  960. File.WriteAllLines(destFilePath, contents.ToArray());
  961. }
  962. }
  963. else
  964. {
  965. if (optimize && destFilePath.Contains("_Generate"))
  966. {
  967. string[] lines1 = FileSystem.ReadFileLines(filePath);
  968. List<string> contents = new List<string>(lines1);
  969. for (var i = contents.Count - 1; i >= 0; --i)
  970. {
  971. if (contents[i].StartsWith("---@") || contents[i].Equals(""))
  972. {
  973. contents.RemoveAt(i);
  974. }
  975. }
  976. lines1 = contents.ToArray();
  977. string[] lines2 = FileSystem.ReadFileLines(destFilePath);
  978. bool diff = (lines1.Length != lines2.Length);
  979. if (!diff)
  980. {
  981. for (var i = lines1.Length - 1; i >= 0; i--)
  982. {
  983. if (lines1[i] != lines2[i])
  984. {
  985. diff = true;
  986. continue;
  987. }
  988. }
  989. }
  990. if (diff)
  991. File.WriteAllLines(destFilePath, lines1);
  992. }
  993. else
  994. {
  995. string content1 = File.ReadAllText(filePath);
  996. string content2 = File.ReadAllText(destFilePath);
  997. if (content1 != content2)
  998. {
  999. File.Copy(filePath, destFilePath, true);
  1000. }
  1001. }
  1002. }
  1003. }
  1004. }
  1005. private static void DeleteDirectoryAssets(string dir, bool forceDelete = false)
  1006. {
  1007. if (!Directory.Exists(dir)) return;
  1008. var ls = Directory.GetFileSystemEntries(dir);
  1009. if (forceDelete || ls == null || ls.Length <= 0)
  1010. {
  1011. Directory.Delete(dir, true);
  1012. if (dir.EndsWith("/") || dir.EndsWith("\\"))
  1013. {
  1014. dir = dir.Substring(0, dir.Length - 1);
  1015. }
  1016. string metaFilePath = dir + ".meta";
  1017. if (File.Exists(metaFilePath))
  1018. {
  1019. File.Delete(metaFilePath);
  1020. }
  1021. string folder = Path.GetDirectoryName(dir);
  1022. DeleteDirectoryAssets(folder);
  1023. }
  1024. }
  1025. private static void CopyLuaAsset(string dir)
  1026. {
  1027. string path = Application.dataPath;
  1028. path = Path.Combine(path.Replace("Assets", ""), dir);
  1029. List<string> files = FileSystem.getAllFilesPathEX(path);
  1030. foreach (string fileName in files)
  1031. {
  1032. string destName = fileName.Insert(fileName.IndexOf("Assets/") + 7, "Content/");
  1033. string destFolder = Path.GetDirectoryName(destName);
  1034. if (!Directory.Exists(destFolder))
  1035. {
  1036. Directory.CreateDirectory(destFolder);
  1037. }
  1038. File.Copy(fileName, destName + ".txt", true);
  1039. if (dir.CompareTo(Constants.LuaLogicDir) == 0 && destName.Contains("_Generate"))
  1040. {
  1041. string[] lines = FileSystem.ReadFileLines(destName + ".txt");
  1042. List<string> contents = new List<string>(lines);
  1043. for (var i = contents.Count - 1; i >= 0; --i)
  1044. {
  1045. if (contents[i].StartsWith("---@") || contents[i].Equals(""))
  1046. {
  1047. contents.RemoveAt(i);
  1048. }
  1049. }
  1050. File.WriteAllLines(destName + ".txt", contents.ToArray());
  1051. }
  1052. }
  1053. }
  1054. static void ProcessConfigAndLua(AssetMap maps)
  1055. {
  1056. string luaABName = Constants.LuaDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  1057. string luaLogicABName = Constants.LuaLogicDir.Replace("Assets", "").Replace("Content", "").Replace("/", "")
  1058. .ToLower();
  1059. string luaPbABName = Constants.LuaPbDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  1060. string pubSecABName = Constants.PubSec.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  1061. string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
  1062. if (allAbNames.Contains(luaABName))
  1063. AssetDatabase.RemoveAssetBundleName(luaABName, true);
  1064. if (allAbNames.Contains(luaLogicABName))
  1065. AssetDatabase.RemoveAssetBundleName(luaLogicABName, true);
  1066. if (allAbNames.Contains(luaPbABName))
  1067. AssetDatabase.RemoveAssetBundleName(luaPbABName, true);
  1068. if (allAbNames.Contains(pubSecABName))
  1069. AssetDatabase.RemoveAssetBundleName(pubSecABName, true);
  1070. ProcessConfigAndLua(maps, Constants.ABLuaPbDir, luaPbABName);
  1071. ProcessConfigAndLua(maps, Constants.ABLuaDir, luaABName);
  1072. ProcessConfigAndLua(maps, Constants.ABLuaLogicDir, luaLogicABName);
  1073. ProcessConfigAndLua(maps, Constants.ABPubsec, pubSecABName);
  1074. }
  1075. static void ProcessConfigAndLua(AssetMap maps, string dir, string abName)
  1076. {
  1077. List<string> fileList = FileSystem.getAllFilesPathEX(dir);
  1078. for (int idx = 0; idx < fileList.Count; idx++)
  1079. {
  1080. string fullPath = fileList[idx];
  1081. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  1082. if (relativePath.Contains("/Pb/") && dir != Constants.ABLuaPbDir) continue;
  1083. addData(maps, abName + ".unity3d", relativePath);
  1084. }
  1085. }
  1086. #endregion
  1087. private static HashSet<string> s_ValidExtMap = new HashSet<string>()
  1088. {
  1089. ".prefab", ".txt", ".xml", ".txt", ".lua", ".csv", ".ogg", ".wav", ".ttf", ".bytes", ".pb",
  1090. };
  1091. private static HashSet<string> getAllFilesPathEX(string path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
  1092. {
  1093. HashSet<string> all = new HashSet<string>();
  1094. if (!Directory.Exists(path))
  1095. {
  1096. return all;
  1097. }
  1098. string[] allFiles = Directory.GetFiles(path, searchPattern, searchOption);
  1099. for (int j = 0; j < allFiles.Length; ++j)
  1100. {
  1101. string fileName = allFiles[j];
  1102. string ext = Path.GetExtension(fileName);
  1103. if (string.IsNullOrEmpty(ext)) continue;
  1104. ext = ext.ToLower();
  1105. if (s_ValidExtMap.Contains(ext))
  1106. {
  1107. all.Add(fileName.Replace('\\', '/'));
  1108. }
  1109. }
  1110. return all;
  1111. }
  1112. }