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. addBuildAssetsCommon(maps, "config.unity3d", Constants.CsvConfig, "*.csv");
  390. addBuildAssetsLua(maps);
  391. List<AssetBundleBuild> bm = new List<AssetBundleBuild>(maps.Maps.Count);
  392. foreach (var items in maps.Maps)
  393. {
  394. AssetBundleBuild bundle = new AssetBundleBuild();
  395. bundle.assetBundleName = items.Key;
  396. bundle.assetNames = items.Value.ToArray();
  397. LogABName(items.Key, bundle.assetBundleName);
  398. if (bundle.assetNames.Length == 0)
  399. {
  400. Debug.LogWarning(items.Key + " empty assetNames");
  401. continue;
  402. }
  403. bm.Add(bundle);
  404. }
  405. if (showProcessBar)
  406. {
  407. EditorUtility.DisplayProgressBar("Generate Bundle Build Map", "Finish...", 1f);
  408. EditorUtility.ClearProgressBar();
  409. }
  410. bm.Sort(assetBundleBuildSort);
  411. return bm.ToArray();
  412. }
  413. private static AssetBundleBuildSort s_AssetBundleBuildSort = null;
  414. private static AssetBundleBuildSort assetBundleBuildSort
  415. {
  416. get
  417. {
  418. if (s_AssetBundleBuildSort == null)
  419. {
  420. s_AssetBundleBuildSort = new AssetBundleBuildSort();
  421. }
  422. return s_AssetBundleBuildSort;
  423. }
  424. }
  425. private class AssetBundleBuildSort : IComparer<AssetBundleBuild>
  426. {
  427. public int Compare(AssetBundleBuild x, AssetBundleBuild y)
  428. {
  429. return string.Compare(x.assetBundleName, y.assetBundleName);
  430. }
  431. }
  432. #region 处理各个模块的assetbundle
  433. private static void addBuildAssetsCommon(AssetMap maps, string abName, string dir,
  434. string filter)
  435. {
  436. string[] fileList = FileUtils.TraverseAllFiles(dir, filter);
  437. for (int idx = 0; idx < fileList.Length; idx++)
  438. {
  439. string fullPath = fileList[idx];
  440. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  441. if (relativePath.EndsWith(".meta")) continue;
  442. addData(maps, abName, relativePath);
  443. }
  444. }
  445. private static void addData(AssetMap maps, string assetBundleName, string assetName)
  446. {
  447. // 重复资源不能加入;按照资源初始化顺序写入
  448. if (maps.AllAssets.Contains(assetName))
  449. {
  450. return;
  451. }
  452. maps.AllAssets.Add(assetName);
  453. if (!maps.Maps.ContainsKey(assetBundleName))
  454. {
  455. maps.Maps[assetBundleName] = new HashSet<string>();
  456. }
  457. maps.Maps[assetBundleName].Add(assetName);
  458. }
  459. private static void addBuildAssetsFonts(AssetMap maps)
  460. {
  461. string[] fileList = FileUtils.TraverseFiles(Constants.FontDir, "*.*");
  462. for (int idx = 0; idx < fileList.Length; idx++)
  463. {
  464. string fullPath = fileList[idx];
  465. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  466. if (relativePath.Contains(".meta")) continue;
  467. addData(maps, "font.unity3d", relativePath);
  468. }
  469. string[] dirList = Directory.GetDirectories(Constants.FontDir);
  470. for (int idx = 0; idx < dirList.Length; idx++)
  471. {
  472. string fullPath = dirList[idx];
  473. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  474. string abName = relativePath.Replace("Assets/", "").Replace("/", "_") + ".unity3d";
  475. string[] files = FileUtils.TraverseFiles(relativePath, "*.*");
  476. for (int jdx = 0; jdx < files.Length; jdx++)
  477. {
  478. string filePathName = files[jdx];
  479. if (filePathName.Contains(".meta")) continue;
  480. string fileRelativePath = FileUtils.ExtractAssetRelativePath(filePathName);
  481. addData(maps, abName, fileRelativePath);
  482. }
  483. }
  484. }
  485. private static void addBuildAssetsBGM(AssetMap maps)
  486. {
  487. string[] files = FileUtils.TraverseAllFiles(Constants.BGMAudioPath, "*.ogg");
  488. for (int idx = 0; idx < files.Length; idx++)
  489. {
  490. string fullPath = files[idx];
  491. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  492. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + ".unity3d";
  493. addData(maps, abName, relativePath);
  494. }
  495. }
  496. private static void addBuildAssetsIcons(AssetMap maps)
  497. {
  498. string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
  499. string[] dirs = Directory.GetDirectories(Constants.IconDir, "*", SearchOption.AllDirectories);
  500. for (int idx = 0; idx < dirs.Length; idx++)
  501. {
  502. string dirName = dirs[idx];
  503. string abName = FileUtils.ExtractPureName(dirName) + "_icons.unity3d";
  504. if (allAbNames.Contains(abName))
  505. {
  506. AssetDatabase.RemoveAssetBundleName(abName, true);
  507. }
  508. string[] files = FileUtils.TraverseAllFiles(dirName, "*.png");
  509. for (int jdx = 0; jdx < files.Length; jdx++)
  510. {
  511. string fullPath = files[jdx];
  512. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  513. addData(maps, abName, relativePath);
  514. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  515. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  516. {
  517. string dependencyAssetName = dependencyAssets[kdx];
  518. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  519. fileType = fileType.ToLower();
  520. if (fileType == "shader")
  521. {
  522. addData(maps, ShaderAbName, dependencyAssetName);
  523. }
  524. else if (fileType == "mat")
  525. {
  526. addData(maps, MatAbName, dependencyAssetName);
  527. }
  528. else if (fileType != "cs")
  529. {
  530. addData(maps, abName, dependencyAssetName);
  531. }
  532. }
  533. }
  534. }
  535. }
  536. private static void addBuildAssetsAnimator(AssetMap maps)
  537. {
  538. string[] dirs = Directory.GetDirectories(Constants.AnimatorPath, "*", SearchOption.AllDirectories);
  539. for (int i = 0; i < dirs.Length; i++)
  540. {
  541. string dirName = dirs[i];
  542. string[] files = FileUtils.TraverseAllFiles(dirName, "*.controller");
  543. for (int idx = 0; idx < files.Length; idx++)
  544. {
  545. string fullPath = files[idx];
  546. string fileName = FileUtils.ExtractPureName(fullPath);
  547. string[] tempList = fileName.Split('_');
  548. string abName = tempList[0] + "_animator.unity3d";
  549. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  550. addData(maps, abName, relativePath);
  551. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  552. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  553. {
  554. string dependencyAssetName = dependencyAssets[jdx];
  555. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  556. fileType = fileType.ToLower();
  557. if (fileType == "shader")
  558. {
  559. addData(maps, ShaderAbName, dependencyAssetName);
  560. }
  561. else if (fileType == "mat")
  562. {
  563. addData(maps, MatAbName, dependencyAssetName);
  564. }
  565. else if (fileType != "cs")
  566. {
  567. addData(maps, abName, dependencyAssetName);
  568. }
  569. }
  570. }
  571. }
  572. }
  573. private static void addBuildAssetsActor(AssetMap maps)
  574. {
  575. string[] dirs = Directory.GetDirectories(Constants.ModelPath, "*", SearchOption.AllDirectories);
  576. for (int i = 0; i < dirs.Length; i++)
  577. {
  578. string dirName = dirs[i];
  579. string abName = FileUtils.ExtractPureName(dirName) + ".unity3d";
  580. string textureABName = FileUtils.ExtractPureName(dirName) + "_texture.unity3d";
  581. string fbxABName = FileUtils.ExtractPureName(dirName) + "_model.unity3d";
  582. string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  583. for (int idx = 0; idx < files.Length; idx++)
  584. {
  585. string fullPath = files[idx];
  586. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  587. addData(maps, abName, relativePath);
  588. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  589. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  590. {
  591. string dependencyAssetName = dependencyAssets[jdx];
  592. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  593. fileType = fileType.ToLower();
  594. if (fileType == "shader")
  595. {
  596. addData(maps, ShaderAbName, dependencyAssetName);
  597. }
  598. else if (fileType == "mat")
  599. {
  600. addData(maps, MatAbName, dependencyAssetName);
  601. }
  602. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
  603. fileType == "psd")
  604. {
  605. addData(maps, textureABName, dependencyAssetName);
  606. }
  607. else if (fileType == "fbx")
  608. {
  609. addData(maps, fbxABName, dependencyAssetName);
  610. }
  611. else if (fileType != "cs")
  612. {
  613. addData(maps, abName, dependencyAssetName);
  614. }
  615. }
  616. }
  617. }
  618. }
  619. private static void addBuildAssetsCamera(AssetMap maps)
  620. {
  621. string[] files = FileUtils.TraverseAllFiles("Assets/Content/Prefabs/Camera", "*.prefab");
  622. for (int idx = 0; idx < files.Length; idx++)
  623. {
  624. string fullPath = files[idx];
  625. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  626. string abName = "prefab_camera.unity3d";
  627. addData(maps, abName, relativePath);
  628. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  629. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  630. {
  631. string dependencyAssetName = dependencyAssets[jdx];
  632. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  633. fileType = fileType.ToLower();
  634. if (fileType == "shader")
  635. {
  636. addData(maps, ShaderAbName, dependencyAssetName);
  637. }
  638. else if (fileType == "mat")
  639. {
  640. addData(maps, MatAbName, dependencyAssetName);
  641. }
  642. else if (fileType != "cs")
  643. {
  644. addData(maps, abName, dependencyAssetName);
  645. }
  646. }
  647. }
  648. }
  649. private static void addBuildAssetsEffect(AssetMap maps)
  650. {
  651. string effectTextureABName = "effect_texture.unity3d";
  652. string effectAnimABName = "effect_dep.unity3d";
  653. string abName = "effect.unity3d";
  654. string[] dirs = Directory.GetDirectories(Constants.EffectPath, "*", SearchOption.AllDirectories);
  655. for (int idx = 0; idx < dirs.Length; idx++)
  656. {
  657. string dirName = dirs[idx];
  658. string[] fileList = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  659. for (int jdx = 0; jdx < fileList.Length; jdx++)
  660. {
  661. string fullPath = fileList[jdx];
  662. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  663. addData(maps, abName, relativePath);
  664. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  665. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  666. {
  667. string dependencyAssetName = dependencyAssets[kdx];
  668. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  669. fileType = fileType.ToLower();
  670. if (fileType == "shader")
  671. {
  672. addData(maps, ShaderAbName, dependencyAssetName);
  673. }
  674. else if (fileType == "mat")
  675. {
  676. addData(maps, MatAbName, dependencyAssetName);
  677. }
  678. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
  679. fileType == "psd")
  680. {
  681. addData(maps, effectTextureABName, dependencyAssetName);
  682. }
  683. else if (fileType != "cs")
  684. {
  685. addData(maps, effectAnimABName, dependencyAssetName);
  686. }
  687. }
  688. }
  689. }
  690. }
  691. private static void addBuildAssetsUI(AssetMap maps)
  692. {
  693. addBuildAssets3DUIPrefab(maps);
  694. addBuildAssetsUIPrefab(maps);
  695. }
  696. private static void addBuildAssets3DUIPrefab(AssetMap maps)
  697. {
  698. string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
  699. string[] dirs = Directory.GetDirectories(Constants.UI3DPath, "*", SearchOption.AllDirectories);
  700. for (int idx = 0; idx < dirs.Length; idx++)
  701. {
  702. string dirName = dirs[idx];
  703. string abName = FileUtils.ExtractPureName(dirName) + "_3duiprefab.unity3d";
  704. string textureABName = FileUtils.ExtractPureName(dirName) + "_3duiprefab_texture.unity3d";
  705. if (allAbNames.Contains(abName))
  706. {
  707. AssetDatabase.RemoveAssetBundleName(abName, true);
  708. }
  709. string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  710. for (int jdx = 0; jdx < files.Length; jdx++)
  711. {
  712. string fullPath = files[jdx];
  713. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  714. addData(maps, abName, relativePath);
  715. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  716. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  717. {
  718. string dependencyAssetName = dependencyAssets[kdx];
  719. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  720. fileType = fileType.ToLower();
  721. if (fileType == "shader")
  722. {
  723. addData(maps, ShaderAbName, dependencyAssetName);
  724. }
  725. else if (fileType == "mat")
  726. {
  727. addData(maps, MatAbName, dependencyAssetName);
  728. }
  729. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
  730. fileType == "psd")
  731. {
  732. addData(maps, textureABName, dependencyAssetName);
  733. }
  734. else if (fileType != "cs")
  735. {
  736. addData(maps, abName, dependencyAssetName);
  737. }
  738. }
  739. }
  740. }
  741. }
  742. private static void addBuildAssetsUIPrefab(AssetMap maps)
  743. {
  744. string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
  745. string[] dirs = Directory.GetDirectories(Constants.UIPath, "*", SearchOption.AllDirectories);
  746. for (int idx = 0; idx < dirs.Length; idx++)
  747. {
  748. string dirName = dirs[idx];
  749. string dirRelativeName = FileUtils.ExtractAssetRelativePath(dirName);
  750. string abName = "";
  751. if (dirRelativeName == Constants.UICommonPath)
  752. {
  753. abName = "prefabsuicommon.unity3d";
  754. }
  755. else
  756. {
  757. abName = FileUtils.ExtractPureName(dirName) + "_uiprefab.unity3d";
  758. }
  759. if (allAbNames.Contains(abName))
  760. {
  761. AssetDatabase.RemoveAssetBundleName(abName, true);
  762. }
  763. string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  764. for (int jdx = 0; jdx < files.Length; jdx++)
  765. {
  766. string fullPath = files[jdx];
  767. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  768. addData(maps, abName, relativePath);
  769. string rootParent;
  770. FileUtils.ExtractParent(relativePath, out rootParent);
  771. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  772. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  773. {
  774. string dependencyAssetName = dependencyAssets[kdx];
  775. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  776. fileType = fileType.ToLower();
  777. if (fileType == "shader")
  778. {
  779. addData(maps, ShaderAbName, dependencyAssetName);
  780. }
  781. else if (fileType == "mat")
  782. {
  783. addData(maps, MatAbName, dependencyAssetName);
  784. }
  785. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" ||
  786. fileType == "psd")
  787. {
  788. string parent;
  789. FileUtils.ExtractParent(dependencyAssetName, out parent);
  790. parent = FileUtils.ExtractPureName(parent);
  791. string uiTextureABName = "UITexture_" + parent + ".unity3d";
  792. addData(maps, uiTextureABName, dependencyAssetName);
  793. }
  794. else if (fileType == "prefab")
  795. {
  796. string parent;
  797. FileUtils.ExtractParent(dependencyAssetName, out parent);
  798. if (parent.StartsWith(Constants.UIPath))
  799. {
  800. if (rootParent == parent)
  801. {
  802. addData(maps, abName, dependencyAssetName);
  803. }
  804. }
  805. else
  806. {
  807. addData(maps, abName, dependencyAssetName);
  808. }
  809. }
  810. else if (fileType != "cs")
  811. {
  812. addData(maps, abName, dependencyAssetName);
  813. }
  814. }
  815. }
  816. }
  817. }
  818. private static void addBuildAssetsScene(AssetMap maps)
  819. {
  820. string[] fileList = Directory.GetFiles(Constants.ScenePath, "*.unity", SearchOption.AllDirectories);
  821. for (int idx = 0; idx < fileList.Length; idx++)
  822. {
  823. string fullPath = fileList[idx];
  824. if (fullPath.Contains("UIScene") || fullPath.Contains("meta") || fullPath.Contains("SceneCG") ||
  825. fullPath.Replace('\\', '/').Contains("Scene/Other") || fullPath.Contains("WasteAsset") ||
  826. fullPath.Contains("Building"))
  827. continue;
  828. if (fullPath.Contains("game.unity") || fullPath.Contains("Loading.unity"))
  829. {
  830. continue;
  831. }
  832. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  833. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + ".unity3d";
  834. addData(maps, abName, relativePath);
  835. string scenePrefabABName =
  836. FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_prefab.unity3d";
  837. string sceneTextureABName =
  838. FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_texture.unity3d";
  839. string sceneOtherABName =
  840. FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_other.unity3d";
  841. string lightmapABName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_lm.unity3d";
  842. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  843. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  844. {
  845. string dependencyAssetName = dependencyAssets[kdx];
  846. if (dependencyAssetName.Contains("Lightmap-") ||
  847. dependencyAssetName.Contains("LightingData") ||
  848. dependencyAssetName.Contains("ReflectionProbe"))
  849. {
  850. addData(maps, lightmapABName, dependencyAssetName);
  851. }
  852. else if (dependencyAssetName.Contains("PostProcessing") && !dependencyAssetName.Contains(".cs"))
  853. {
  854. if (dependencyAssetName.Contains(".shader"))
  855. {
  856. addData(maps, ShaderAbName, dependencyAssetName);
  857. }
  858. else
  859. {
  860. addData(maps, "PostProcessing.unity3d", dependencyAssetName);
  861. }
  862. }
  863. else
  864. {
  865. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  866. fileType = fileType.ToLower();
  867. if (fileType == "shader")
  868. {
  869. addData(maps, ShaderAbName, dependencyAssetName);
  870. }
  871. else if (fileType == "mat")
  872. {
  873. addData(maps, MatAbName, dependencyAssetName);
  874. }
  875. else if (fileType == "prefab")
  876. {
  877. if (dependencyAssetName.Contains("Scenes/Scene_common"))
  878. {
  879. string sCommonAb = "scene_common_prefab.unity3d";
  880. addData(maps, sCommonAb, dependencyAssetName);
  881. }
  882. else
  883. {
  884. addData(maps, scenePrefabABName, dependencyAssetName);
  885. }
  886. }
  887. else if (fileType == "png" || fileType == "tga" || fileType == "jpg" || fileType == "tif" ||
  888. fileType == "psd")
  889. {
  890. if (dependencyAssetName.Contains("Scenes/Scene_common"))
  891. {
  892. string sCommonAb = "scene_common_texture.unity3d";
  893. addData(maps, sCommonAb, dependencyAssetName);
  894. }
  895. else
  896. {
  897. addData(maps, sceneTextureABName, dependencyAssetName);
  898. }
  899. }
  900. else if (fileType != "cs")
  901. {
  902. if (dependencyAssetName.Contains("Scenes/Scene_common"))
  903. {
  904. string sCommonAb = "scene_common_other.unity3d";
  905. addData(maps, sCommonAb, dependencyAssetName);
  906. }
  907. else
  908. {
  909. addData(maps, sceneOtherABName, dependencyAssetName);
  910. }
  911. }
  912. }
  913. }
  914. }
  915. }
  916. #endregion
  917. #region 处理lua文件
  918. private static void addBuildAssetsLua(AssetMap maps)
  919. {
  920. CopyLuaAssets(Constants.LuaDir, Constants.ABLuaDir);
  921. CopyLuaAssets(Constants.LuaLogicDir, Constants.ABLuaLogicDir, true);
  922. CopyLuaAssets(Constants.LuaPbDir, Constants.ABLuaPbDir);
  923. CopyLuaAssets(Constants.PubSec, Constants.ABPubsec);
  924. AssetDatabase.Refresh();
  925. ProcessConfigAndLua(maps);
  926. }
  927. private static void CopyLuaAssets(string sourceDir, string destDir, bool optimize = false)
  928. {
  929. string path = Application.dataPath;
  930. path = path.Replace("Assets", "");
  931. string sourceAbsDir = Path.Combine(path, sourceDir);
  932. string destAbsDir = Path.Combine(path, destDir);
  933. HashSet<string> sourceFiles = getAllFilesPathEX(sourceAbsDir);
  934. HashSet<string> destFiles = getAllFilesPathEX(destAbsDir);
  935. // 删除不再需要的文件
  936. foreach (var destFile in destFiles)
  937. {
  938. string sourceFile = destFile.Replace(destAbsDir, sourceAbsDir);
  939. string fileName = Path.GetExtension(sourceFile);
  940. if (fileName.ToLower() == ".txt")
  941. {
  942. string ext = Path.GetExtension(fileName);
  943. if (s_ValidExtMap.Contains(ext))
  944. {
  945. sourceFile = sourceFile.Remove(sourceFile.Length - 4);
  946. }
  947. }
  948. if (!sourceFiles.Contains(sourceFile))
  949. {
  950. File.Delete(destFile);
  951. string metaFilePath = destFile + ".meta";
  952. if (File.Exists(metaFilePath))
  953. {
  954. File.Delete(metaFilePath);
  955. }
  956. string folder = Path.GetDirectoryName(destFile);
  957. DeleteDirectoryAssets(folder);
  958. }
  959. }
  960. // 替换新文件
  961. foreach (var filePath in sourceFiles)
  962. {
  963. string destFilePath = filePath.Replace(sourceAbsDir, destAbsDir);
  964. string destFolder = Path.GetDirectoryName(destFilePath);
  965. if (!Directory.Exists(destFolder))
  966. {
  967. Directory.CreateDirectory(destFolder);
  968. }
  969. destFilePath = destFilePath + ".txt";
  970. if (!File.Exists(destFilePath))
  971. {
  972. File.Copy(filePath, destFilePath, true);
  973. if (optimize && destFilePath.Contains("_Generate"))
  974. {
  975. string[] lines = FileSystem.ReadFileLines(destFilePath);
  976. List<string> contents = new List<string>(lines);
  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. File.WriteAllLines(destFilePath, contents.ToArray());
  985. }
  986. }
  987. else
  988. {
  989. if (optimize && destFilePath.Contains("_Generate"))
  990. {
  991. string[] lines1 = FileSystem.ReadFileLines(filePath);
  992. List<string> contents = new List<string>(lines1);
  993. for (var i = contents.Count - 1; i >= 0; --i)
  994. {
  995. if (contents[i].StartsWith("---@") || contents[i].Equals(""))
  996. {
  997. contents.RemoveAt(i);
  998. }
  999. }
  1000. lines1 = contents.ToArray();
  1001. string[] lines2 = FileSystem.ReadFileLines(destFilePath);
  1002. bool diff = (lines1.Length != lines2.Length);
  1003. if (!diff)
  1004. {
  1005. for (var i = lines1.Length - 1; i >= 0; i--)
  1006. {
  1007. if (lines1[i] != lines2[i])
  1008. {
  1009. diff = true;
  1010. continue;
  1011. }
  1012. }
  1013. }
  1014. if (diff)
  1015. File.WriteAllLines(destFilePath, lines1);
  1016. }
  1017. else
  1018. {
  1019. string content1 = File.ReadAllText(filePath);
  1020. string content2 = File.ReadAllText(destFilePath);
  1021. if (content1 != content2)
  1022. {
  1023. File.Copy(filePath, destFilePath, true);
  1024. }
  1025. }
  1026. }
  1027. }
  1028. }
  1029. private static void DeleteDirectoryAssets(string dir, bool forceDelete = false)
  1030. {
  1031. if (!Directory.Exists(dir)) return;
  1032. var ls = Directory.GetFileSystemEntries(dir);
  1033. if (forceDelete || ls == null || ls.Length <= 0)
  1034. {
  1035. Directory.Delete(dir, true);
  1036. if (dir.EndsWith("/") || dir.EndsWith("\\"))
  1037. {
  1038. dir = dir.Substring(0, dir.Length - 1);
  1039. }
  1040. string metaFilePath = dir + ".meta";
  1041. if (File.Exists(metaFilePath))
  1042. {
  1043. File.Delete(metaFilePath);
  1044. }
  1045. string folder = Path.GetDirectoryName(dir);
  1046. DeleteDirectoryAssets(folder);
  1047. }
  1048. }
  1049. private static void CopyLuaAsset(string dir)
  1050. {
  1051. string path = Application.dataPath;
  1052. path = Path.Combine(path.Replace("Assets", ""), dir);
  1053. List<string> files = FileSystem.getAllFilesPathEX(path);
  1054. foreach (string fileName in files)
  1055. {
  1056. string destName = fileName.Insert(fileName.IndexOf("Assets/") + 7, "Content/");
  1057. string destFolder = Path.GetDirectoryName(destName);
  1058. if (!Directory.Exists(destFolder))
  1059. {
  1060. Directory.CreateDirectory(destFolder);
  1061. }
  1062. File.Copy(fileName, destName + ".txt", true);
  1063. if (dir.CompareTo(Constants.LuaLogicDir) == 0 && destName.Contains("_Generate"))
  1064. {
  1065. string[] lines = FileSystem.ReadFileLines(destName + ".txt");
  1066. List<string> contents = new List<string>(lines);
  1067. for (var i = contents.Count - 1; i >= 0; --i)
  1068. {
  1069. if (contents[i].StartsWith("---@") || contents[i].Equals(""))
  1070. {
  1071. contents.RemoveAt(i);
  1072. }
  1073. }
  1074. File.WriteAllLines(destName + ".txt", contents.ToArray());
  1075. }
  1076. }
  1077. }
  1078. static void ProcessConfigAndLua(AssetMap maps)
  1079. {
  1080. string luaABName = Constants.LuaDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  1081. string luaLogicABName = Constants.LuaLogicDir.Replace("Assets", "").Replace("Content", "").Replace("/", "")
  1082. .ToLower();
  1083. string luaPbABName = Constants.LuaPbDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  1084. string pubSecABName = Constants.PubSec.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  1085. string[] allAbNames = AssetDatabase.GetAllAssetBundleNames();
  1086. if (allAbNames.Contains(luaABName))
  1087. AssetDatabase.RemoveAssetBundleName(luaABName, true);
  1088. if (allAbNames.Contains(luaLogicABName))
  1089. AssetDatabase.RemoveAssetBundleName(luaLogicABName, true);
  1090. if (allAbNames.Contains(luaPbABName))
  1091. AssetDatabase.RemoveAssetBundleName(luaPbABName, true);
  1092. if (allAbNames.Contains(pubSecABName))
  1093. AssetDatabase.RemoveAssetBundleName(pubSecABName, true);
  1094. ProcessConfigAndLua(maps, Constants.ABLuaPbDir, luaPbABName);
  1095. ProcessConfigAndLua(maps, Constants.ABLuaDir, luaABName);
  1096. ProcessConfigAndLua(maps, Constants.ABLuaLogicDir, luaLogicABName);
  1097. ProcessConfigAndLua(maps, Constants.ABPubsec, pubSecABName);
  1098. }
  1099. static void ProcessConfigAndLua(AssetMap maps, string dir, string abName)
  1100. {
  1101. List<string> fileList = FileSystem.getAllFilesPathEX(dir);
  1102. for (int idx = 0; idx < fileList.Count; idx++)
  1103. {
  1104. string fullPath = fileList[idx];
  1105. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  1106. if (relativePath.Contains("/Pb/") && dir != Constants.ABLuaPbDir) continue;
  1107. addData(maps, abName + ".unity3d", relativePath);
  1108. }
  1109. }
  1110. #endregion
  1111. private static HashSet<string> s_ValidExtMap = new HashSet<string>()
  1112. {
  1113. ".prefab", ".txt", ".xml", ".txt", ".lua", ".csv", ".ogg", ".wav", ".ttf", ".bytes", ".pb",
  1114. };
  1115. private static HashSet<string> getAllFilesPathEX(string path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
  1116. {
  1117. HashSet<string> all = new HashSet<string>();
  1118. if (!Directory.Exists(path))
  1119. {
  1120. return all;
  1121. }
  1122. string[] allFiles = Directory.GetFiles(path, searchPattern, searchOption);
  1123. for (int j = 0; j < allFiles.Length; ++j)
  1124. {
  1125. string fileName = allFiles[j];
  1126. string ext = Path.GetExtension(fileName);
  1127. if (string.IsNullOrEmpty(ext)) continue;
  1128. ext = ext.ToLower();
  1129. if (s_ValidExtMap.Contains(ext))
  1130. {
  1131. all.Add(fileName.Replace('\\', '/'));
  1132. }
  1133. }
  1134. return all;
  1135. }
  1136. }