AssetBundleMap.cs 48 KB

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