AssetBundleMap.cs 47 KB

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