AssetBundleUtil.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. public class AssetBundleUtil : EditorWindow
  7. {
  8. public const string TAG = "AssetBundleUtil";
  9. static string shaderABName = "shader.unity3d";
  10. static string matABName = "mat.unity3d";
  11. static string s_TempAssetBundlePath = Application.dataPath + "/../assetbundle";
  12. static string s_ManifestFileExtension = ".manifest";
  13. static BuildAssetBundleOptions buildOptions = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression;
  14. #region 处理模块assetbundle
  15. static void ProcessShaders(HashSet<string> resHash)
  16. {
  17. string[] fileList = FileUtils.TraverseAllFiles(Constants.ShaderDir, "*.shader");
  18. for(int idx =0; idx < fileList.Length; idx++)
  19. {
  20. string fullPath = fileList[idx];
  21. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  22. if (!resHash.Contains(relativePath))
  23. {
  24. resHash.Add(relativePath);
  25. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath));
  26. AssetImporter assetImporter = AssetImporter.GetAtPath(relativePath);
  27. assetImporter.assetBundleName = shaderABName;
  28. }
  29. }
  30. fileList = FileUtils.TraverseAllFiles(Constants.ShaderDir, "*.shadervariants");
  31. if(fileList != null)
  32. {
  33. for(int idx =0; idx < fileList.Length;idx++)
  34. {
  35. string fullPath = fileList[idx];
  36. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  37. if (!resHash.Contains(relativePath))
  38. {
  39. resHash.Add(relativePath);
  40. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath));
  41. AssetImporter assetImporter = AssetImporter.GetAtPath(relativePath);
  42. assetImporter.assetBundleName = shaderABName;
  43. }
  44. }
  45. }
  46. }
  47. static void ProcessMaterials(HashSet<string> resHash)
  48. {
  49. string[] fileList = FileUtils.TraverseAllFiles(Constants.CommonMaterialDir, "*.mat");
  50. for (int idx = 0; idx < fileList.Length; idx++)
  51. {
  52. string fullPath = fileList[idx];
  53. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  54. if (!resHash.Contains(relativePath))
  55. {
  56. resHash.Add(relativePath);
  57. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath));
  58. AssetImporter assetImporter = AssetImporter.GetAtPath(relativePath);
  59. assetImporter.assetBundleName = "commonmat.unity3d";
  60. }
  61. }
  62. }
  63. static void ProcessFont(HashSet<string> resHash)
  64. {
  65. string[] fileList = FileUtils.TraverseFiles(Constants.FontDir, "*.*");
  66. for (int idx =0; idx < fileList.Length; idx++)
  67. {
  68. string fullPath = fileList[idx];
  69. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  70. if (relativePath.Contains(".meta")) continue;
  71. if (!resHash.Contains(relativePath))
  72. {
  73. resHash.Add(relativePath);
  74. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath));
  75. AssetImporter assetImporter = AssetImporter.GetAtPath(relativePath);
  76. assetImporter.assetBundleName = "font.unity3d";
  77. }
  78. }
  79. string[] dirList = Directory.GetDirectories(Constants.FontDir);
  80. for(int idx =0; idx < dirList.Length; idx++)
  81. {
  82. string fullPath = dirList[idx];
  83. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  84. string abName = relativePath.Replace("Assets/", "").Replace("/", "_")+".unity3d";
  85. string[] files = FileUtils.TraverseFiles(relativePath,"*.*");
  86. for(int jdx = 0; jdx < files.Length;jdx++)
  87. {
  88. string filePathName = files[jdx];
  89. if (filePathName.Contains(".meta")) continue;
  90. string fileRelativePath = FileUtils.ExtractAssetRelativePath(filePathName);
  91. SetABName(resHash, fileRelativePath, abName);
  92. }
  93. }
  94. }
  95. static void ProcessConfig(HashSet<string> resHash)
  96. {
  97. string[] files = FileUtils.TraverseAllFiles(Constants.CsvConfig, "*.csv");
  98. for(int idx =0; idx < files.Length; idx++)
  99. {
  100. string fullPath = files[idx];
  101. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  102. SetABName(resHash, relativePath, "config.unity3d");
  103. }
  104. files = FileUtils.TraverseAllFiles(Constants.XmlConfig, "*.xml");
  105. for (int idx = 0; idx < files.Length; idx++)
  106. {
  107. string fullPath = files[idx];
  108. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  109. SetABName(resHash, relativePath, "xml.unity3d");
  110. }
  111. }
  112. static void ProcessAudio(HashSet<string> resHash)
  113. {
  114. ProcessBGM(resHash);
  115. ProcessUIAudio(resHash);
  116. ProcessFight(resHash);
  117. }
  118. static void ProcessBGM(HashSet<string> resHash)
  119. {
  120. string[] files = FileUtils.TraverseAllFiles(Constants.BGMAudioPath, "*.ogg");
  121. for(int idx =0; idx < files.Length;idx++)
  122. {
  123. string fullPath = files[idx];
  124. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  125. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + ".unity3d";
  126. SetABName(resHash, relativePath, abName);
  127. }
  128. }
  129. static void ProcessUIAudio(HashSet<string> resHash)
  130. {
  131. string[] files = FileUtils.TraverseAllFiles(Constants.UIAudioPath, "*.ogg");
  132. string abName = "UI_Audio.unity3d";
  133. for (int idx = 0; idx < files.Length; idx++)
  134. {
  135. string fullPath = files[idx];
  136. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  137. SetABName(resHash, relativePath, abName);
  138. }
  139. }
  140. static void ProcessFight(HashSet<string> resHash)
  141. {
  142. string[] files = FileUtils.TraverseAllFiles(Constants.FightAudioPath, "*.ogg");
  143. string abName = "Fight_Audio.unity3d";
  144. for (int idx = 0; idx < files.Length; idx++)
  145. {
  146. string fullPath = files[idx];
  147. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  148. SetABName(resHash, relativePath, abName);
  149. }
  150. }
  151. static void ProcessIcons(HashSet<string> resHash)
  152. {
  153. string[] dirs = Directory.GetDirectories(Constants.IconDir, "*", SearchOption.AllDirectories);
  154. for (int idx = 0; idx < dirs.Length; idx++)
  155. {
  156. string dirName = dirs[idx];
  157. string abName = FileUtils.ExtractPureName(dirName) + "_icons.unity3d";
  158. AssetDatabase.RemoveAssetBundleName(abName, true);
  159. string[] files = FileUtils.TraverseAllFiles(dirName, "*.png");
  160. for (int jdx = 0; jdx < files.Length; jdx++)
  161. {
  162. string fullPath = files[jdx];
  163. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  164. SetABName(resHash, relativePath, abName);
  165. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  166. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  167. {
  168. string dependencyAssetName = dependencyAssets[kdx];
  169. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  170. fileType = fileType.ToLower();
  171. if (fileType == "shader")
  172. {
  173. SetABName(resHash, dependencyAssetName, shaderABName);
  174. }
  175. else if(fileType == "mat")
  176. {
  177. SetABName(resHash, dependencyAssetName,matABName);
  178. }
  179. else if (fileType != "cs")
  180. {
  181. SetABName(resHash, dependencyAssetName, abName);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. static void ProcessAnimator(HashSet<string> resHash)
  188. {
  189. string[] dirs = Directory.GetDirectories(Constants.AnimatorPath, "*", SearchOption.AllDirectories);
  190. for (int i = 0; i < dirs.Length; i++)
  191. {
  192. string dirName = dirs[i];
  193. string[] files = FileUtils.TraverseAllFiles(dirName, "*.controller");
  194. for (int idx = 0; idx < files.Length; idx++)
  195. {
  196. string fullPath = files[idx];
  197. string fileName = FileUtils.ExtractPureName(fullPath);
  198. string[] tempList = fileName.Split('_');
  199. string abName = tempList[0] + "_animator.unity3d";
  200. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  201. SetABName(resHash, relativePath, abName);
  202. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  203. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  204. {
  205. string dependencyAssetName = dependencyAssets[jdx];
  206. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  207. fileType = fileType.ToLower();
  208. if (fileType == "shader")
  209. {
  210. SetABName(resHash, dependencyAssetName, shaderABName);
  211. }
  212. else if (fileType == "mat")
  213. {
  214. SetABName(resHash, dependencyAssetName, matABName);
  215. }
  216. else if (fileType != "cs")
  217. {
  218. SetABName(resHash, dependencyAssetName, abName);
  219. }
  220. }
  221. }
  222. }
  223. }
  224. static void ProcessActor(HashSet<string> resHash)
  225. {
  226. string[] dirs = Directory.GetDirectories(Constants.ModelPath, "*", SearchOption.AllDirectories);
  227. for (int i = 0; i < dirs.Length; i++)
  228. {
  229. string dirName = dirs[i];
  230. string abName = FileUtils.ExtractPureName(dirName) + ".unity3d";
  231. string textureABName = FileUtils.ExtractPureName(dirName) + "_texture.unity3d";
  232. string fbxABName = FileUtils.ExtractPureName(dirName) + "_model.unity3d";
  233. string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  234. for (int idx = 0; idx < files.Length; idx++)
  235. {
  236. string fullPath = files[idx];
  237. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  238. SetABName(resHash, relativePath, abName);
  239. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  240. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  241. {
  242. string dependencyAssetName = dependencyAssets[jdx];
  243. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  244. fileType = fileType.ToLower();
  245. if (fileType == "shader")
  246. {
  247. SetABName(resHash, dependencyAssetName, shaderABName);
  248. }
  249. else if (fileType == "mat")
  250. {
  251. SetABName(resHash, dependencyAssetName, matABName);
  252. }else if(fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" || fileType == "psd")
  253. {
  254. SetABName(resHash, dependencyAssetName, textureABName);
  255. }
  256. else if (fileType == "fbx")
  257. {
  258. SetABName(resHash, dependencyAssetName, fbxABName);
  259. }
  260. else if (fileType != "cs")
  261. {
  262. SetABName(resHash, dependencyAssetName, abName);
  263. }
  264. }
  265. }
  266. }
  267. }
  268. static void ProcessCamera(HashSet<string> resHash)
  269. {
  270. string[] files = FileUtils.TraverseAllFiles("Assets/Content/Prefabs/Camera", "*.prefab");
  271. for (int idx = 0; idx < files.Length; idx++)
  272. {
  273. string fullPath = files[idx];
  274. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  275. string abName = "prefab_camera.unity3d";
  276. SetABName(resHash, relativePath, abName);
  277. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  278. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  279. {
  280. string dependencyAssetName = dependencyAssets[jdx];
  281. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  282. fileType = fileType.ToLower();
  283. if (fileType == "shader")
  284. {
  285. SetABName(resHash, dependencyAssetName, shaderABName);
  286. }
  287. else if (fileType == "mat")
  288. {
  289. SetABName(resHash, dependencyAssetName, matABName);
  290. }
  291. else if (fileType != "cs")
  292. {
  293. SetABName(resHash, dependencyAssetName, abName);
  294. }
  295. }
  296. }
  297. }
  298. static void ProcessMonster(HashSet<string> resHash)
  299. {
  300. string[] files = FileUtils.TraverseAllFiles(Constants.MonsterPath, "*.prefab");
  301. for(int idx = 0; idx < files.Length;idx++)
  302. {
  303. string fullPath = files[idx];
  304. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  305. //string abName = FileUtils.ExtractPureName(FileUtils.RemoveExtension(relativePath)) + ".unity3d";
  306. string abName = "monster.unity3d";
  307. SetABName(resHash, relativePath, abName);
  308. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  309. for(int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  310. {
  311. string dependencyAssetName = dependencyAssets[jdx];
  312. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  313. fileType = fileType.ToLower();
  314. if(fileType == "shader")
  315. {
  316. SetABName(resHash, dependencyAssetName, shaderABName);
  317. }
  318. else if (fileType == "mat")
  319. {
  320. SetABName(resHash, dependencyAssetName, matABName);
  321. }
  322. else if(fileType != "cs")
  323. {
  324. SetABName(resHash, dependencyAssetName, abName);
  325. }
  326. }
  327. }
  328. }
  329. static void ProcessPartner(HashSet<string> resHash)
  330. {
  331. string[] files = FileUtils.TraverseAllFiles(Constants.ModelPath+ "/Parter", "*.prefab");
  332. for (int idx = 0; idx < files.Length; idx++)
  333. {
  334. string fullPath = files[idx];
  335. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  336. //string abName = FileUtils.ExtractPureName(FileUtils.RemoveExtension(relativePath)) + ".unity3d";
  337. string abName = "partner.unity3d";
  338. SetABName(resHash, relativePath, abName);
  339. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  340. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  341. {
  342. string dependencyAssetName = dependencyAssets[jdx];
  343. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  344. fileType = fileType.ToLower();
  345. if (fileType == "shader")
  346. {
  347. SetABName(resHash, dependencyAssetName, shaderABName);
  348. }
  349. else if (fileType == "mat")
  350. {
  351. SetABName(resHash, dependencyAssetName, matABName);
  352. }
  353. else if (fileType != "cs")
  354. {
  355. SetABName(resHash, dependencyAssetName, abName);
  356. }
  357. }
  358. }
  359. }
  360. static void ProcessHero(HashSet<string> resHash)
  361. {
  362. string[] files = FileUtils.TraverseAllFiles(Constants.HeroPath, "*.prefab");
  363. for (int idx = 0; idx < files.Length; idx++)
  364. {
  365. string fullPath = files[idx];
  366. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  367. //string abName = FileUtils.ExtractPureName(FileUtils.RemoveExtension(relativePath)) + ".unity3d";
  368. string abName = "hero.unity3d";
  369. SetABName(resHash, relativePath, abName);
  370. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  371. for (int jdx = 0; jdx < dependencyAssets.Length; jdx++)
  372. {
  373. string dependencyAssetName = dependencyAssets[jdx];
  374. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  375. fileType = fileType.ToLower();
  376. if (fileType == "shader")
  377. {
  378. SetABName(resHash, dependencyAssetName, shaderABName);
  379. }
  380. else if (fileType == "mat")
  381. {
  382. SetABName(resHash, dependencyAssetName, matABName);
  383. }
  384. else if (fileType != "cs")
  385. {
  386. SetABName(resHash, dependencyAssetName, abName);
  387. }
  388. }
  389. }
  390. }
  391. static void ProcessEffect(HashSet<string> resHash)
  392. {
  393. string effectTextureABName = "effect_texture.unity3d";
  394. string effectAnimABName = "effect_dep.unity3d";
  395. string abName = "effect.unity3d";
  396. string[] dirs = Directory.GetDirectories(Constants.EffectPath, "*", SearchOption.AllDirectories);
  397. for(int idx =0; idx < dirs.Length; idx++)
  398. {
  399. string dirName = dirs[idx];
  400. string[] fileList = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  401. for (int jdx =0; jdx < fileList.Length;jdx++)
  402. {
  403. string fullPath = fileList[jdx];
  404. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  405. SetABName(resHash, relativePath, abName);
  406. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  407. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  408. {
  409. string dependencyAssetName = dependencyAssets[kdx];
  410. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  411. fileType = fileType.ToLower();
  412. if (fileType == "shader")
  413. {
  414. SetABName(resHash, dependencyAssetName, shaderABName);
  415. }
  416. else if (fileType == "mat")
  417. {
  418. SetABName(resHash, dependencyAssetName, matABName);
  419. }
  420. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" || fileType == "psd")
  421. {
  422. SetABName(resHash, dependencyAssetName, effectTextureABName);
  423. }
  424. else if (fileType != "cs")
  425. {
  426. SetABName(resHash, dependencyAssetName, effectAnimABName);
  427. }
  428. }
  429. }
  430. }
  431. }
  432. static void ProcessUI(HashSet<string> resHash)
  433. {
  434. Process3DUIPrefab(resHash);
  435. ProcessUIPrefab(resHash);
  436. }
  437. static void Process3DUIPrefab(HashSet<string> resHash)
  438. {
  439. string[] dirs = Directory.GetDirectories(Constants.UI3DPath, "*", SearchOption.AllDirectories);
  440. for(int idx =0; idx < dirs.Length;idx++)
  441. {
  442. string dirName = dirs[idx];
  443. string abName = FileUtils.ExtractPureName(dirName) + "_3duiprefab.unity3d";
  444. string textureABName = FileUtils.ExtractPureName(dirName) + "_3duiprefab_texture.unity3d";
  445. AssetDatabase.RemoveAssetBundleName(abName, true);
  446. string[] files = FileUtils.TraverseAllFiles(dirName, "*.prefab");
  447. for(int jdx =0; jdx < files.Length;jdx++)
  448. {
  449. string fullPath = files[jdx];
  450. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  451. SetABName(resHash, relativePath, abName);
  452. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  453. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  454. {
  455. string dependencyAssetName = dependencyAssets[kdx];
  456. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  457. fileType = fileType.ToLower();
  458. if (fileType == "shader")
  459. {
  460. SetABName(resHash, dependencyAssetName, shaderABName);
  461. }
  462. else if (fileType == "mat")
  463. {
  464. SetABName(resHash, dependencyAssetName, matABName);
  465. }
  466. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" || fileType == "psd")
  467. {
  468. SetABName(resHash, dependencyAssetName, textureABName);
  469. }
  470. else if (fileType != "cs")
  471. {
  472. SetABName(resHash, dependencyAssetName, abName);
  473. }
  474. }
  475. }
  476. }
  477. }
  478. static void ProcessUIPrefab(HashSet<string> resHash)
  479. {
  480. string[] dirs = Directory.GetDirectories(Constants.UIPath, "*", SearchOption.AllDirectories);
  481. for(int idx =0; idx < dirs.Length;idx++)
  482. {
  483. string dirName = dirs[idx];
  484. string dirRelativeName = FileUtils.ExtractAssetRelativePath(dirName);
  485. string abName = "";
  486. if (dirRelativeName == Constants.UICommonPath)
  487. {
  488. abName = "prefabsuicommon.unity3d";
  489. }
  490. else
  491. {
  492. abName = FileUtils.ExtractPureName(dirName) + "_uiprefab.unity3d";
  493. }
  494. AssetDatabase.RemoveAssetBundleName(abName, true);
  495. string[] files = FileUtils.TraverseAllFiles(dirName,"*.prefab");
  496. for (int jdx = 0; jdx < files.Length; jdx++)
  497. {
  498. string fullPath = files[jdx];
  499. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  500. SetABName(resHash, relativePath, abName);
  501. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  502. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  503. {
  504. string dependencyAssetName = dependencyAssets[kdx];
  505. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  506. fileType = fileType.ToLower();
  507. if (fileType == "shader")
  508. {
  509. SetABName(resHash, dependencyAssetName, shaderABName);
  510. }
  511. else if (fileType == "mat")
  512. {
  513. SetABName(resHash, dependencyAssetName, matABName);
  514. }
  515. else if (fileType == "jpg" || fileType == "png" || fileType == "tga" || fileType == "tif" || fileType == "psd")
  516. {
  517. string parent;
  518. FileUtils.ExtractParent(dependencyAssetName, out parent);
  519. parent = FileUtils.ExtractPureName(parent);
  520. string uiTextureABName = "UITexture_" + parent + ".unity3d";
  521. SetABName(resHash, dependencyAssetName, uiTextureABName);
  522. }
  523. else if (fileType != "cs")
  524. {
  525. SetABName(resHash, dependencyAssetName, abName);
  526. }
  527. }
  528. }
  529. }
  530. }
  531. static void ProcessScene(HashSet<string> resHash)
  532. {
  533. string[] fileList = Directory.GetFiles(Constants.ScenePath, "*.unity", SearchOption.AllDirectories);
  534. for(int idx =0; idx < fileList.Length; idx++)
  535. {
  536. string fullPath = fileList[idx];
  537. if (fullPath.Contains("UIScene") || fullPath.Contains("meta") || fullPath.Contains("SceneCG") ||
  538. fullPath.Replace('\\', '/').Contains("Scene/Other") || fullPath.Contains("WasteAsset") || fullPath.Contains("Building"))
  539. continue;
  540. if (fullPath.Contains("game.unity") || fullPath.Contains("Loading.unity"))
  541. {
  542. continue;
  543. }
  544. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  545. string abName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + ".unity3d";
  546. if (!resHash.Contains(relativePath))
  547. {
  548. resHash.Add(relativePath);
  549. AssetImporter assetImporter = AssetImporter.GetAtPath(relativePath);
  550. assetImporter.assetBundleName = abName;
  551. }
  552. string scenePrefabABName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_prefab.unity3d";
  553. string sceneTextureABName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_texture.unity3d";
  554. string sceneOtherABName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_other.unity3d";
  555. string lightmapABName = FileUtils.RemoveExtension(FileUtils.ExtractPureName(relativePath)) + "_lm.unity3d";
  556. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  557. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  558. {
  559. string dependencyAssetName = dependencyAssets[kdx];
  560. if (dependencyAssetName.Contains("Lightmap-") ||
  561. dependencyAssetName.Contains("LightingData") ||
  562. dependencyAssetName.Contains("ReflectionProbe"))
  563. {
  564. SetABName(resHash, dependencyAssetName, lightmapABName);
  565. }else if(dependencyAssetName.Contains("PostProcessing") && !dependencyAssetName.Contains(".cs"))
  566. {
  567. if(dependencyAssetName.Contains(".shader"))
  568. {
  569. SetABName(resHash, dependencyAssetName, shaderABName);
  570. }
  571. else
  572. {
  573. SetABName(resHash, dependencyAssetName, "PostProcessing.unity3d");
  574. }
  575. }
  576. else
  577. {
  578. string fileType = dependencyAssetName.Substring(dependencyAssetName.LastIndexOf('.') + 1);
  579. fileType = fileType.ToLower();
  580. if (fileType == "shader")
  581. {
  582. SetABName(resHash, dependencyAssetName, shaderABName);
  583. }
  584. else if (fileType == "mat")
  585. {
  586. SetABName(resHash, dependencyAssetName, matABName);
  587. }
  588. else if (fileType == "prefab")
  589. {
  590. if(dependencyAssetName.Contains("Scenes/Scene_common"))
  591. {
  592. string sCommonAb = "scene_common_prefab.unity3d";
  593. SetABName(resHash, dependencyAssetName, sCommonAb);
  594. }
  595. else
  596. {
  597. SetABName(resHash, dependencyAssetName, scenePrefabABName);
  598. }
  599. }
  600. else if (fileType == "png" || fileType == "tga" || fileType == "jpg" || fileType == "tif" || fileType == "psd")
  601. {
  602. if(dependencyAssetName.Contains("Scenes/Scene_common"))
  603. {
  604. string sCommonAb = "scene_common_texture.unity3d";
  605. SetABName(resHash, dependencyAssetName, sCommonAb);
  606. }
  607. else
  608. {
  609. SetABName(resHash, dependencyAssetName, sceneTextureABName);
  610. }
  611. }
  612. else if (fileType != "cs")
  613. {
  614. if (dependencyAssetName.Contains("Scenes/Scene_common"))
  615. {
  616. string sCommonAb = "scene_common_other.unity3d";
  617. SetABName(resHash, dependencyAssetName, sCommonAb);
  618. }
  619. else
  620. {
  621. SetABName(resHash, dependencyAssetName, sceneOtherABName);
  622. }
  623. }
  624. }
  625. }
  626. }
  627. }
  628. static void SetABName(HashSet<string> resHash, string assetPath,string abName,bool print = false)
  629. {
  630. if (abName.Equals(shaderABName) && assetPath.Contains("/Resources/"))
  631. {
  632. //Debug.Log(assetPath);
  633. return;
  634. }
  635. if (!resHash.Contains(assetPath))
  636. {
  637. resHash.Add(assetPath);
  638. AssetImporter aiter = AssetImporter.GetAtPath(assetPath);
  639. aiter.assetBundleName = abName;
  640. }
  641. else
  642. {
  643. if (print) {
  644. AssetImporter aiter = AssetImporter.GetAtPath(assetPath);
  645. DebugHelper.LogError(assetPath + " exist:"+ aiter.assetBundleName + " not exist:"+abName);
  646. }
  647. }
  648. }
  649. #region lua_method
  650. static void CopyLua()
  651. {
  652. //EditorUtility.DisplayProgressBar("Copy Lua", "Copy Config...", 0);
  653. //CopyLuaAsset(Constants.LuaConfigDir);
  654. EditorUtility.DisplayProgressBar("Copy Lua", "Copy Lua...", 0.1f);
  655. CopyLuaAsset(Constants.LuaDir);
  656. EditorUtility.DisplayProgressBar("Copy Lua", "Copy Logic...", 0.2f);
  657. CopyLuaAsset(Constants.LuaLogicDir);
  658. CopyLuaAsset(Constants.LuaPbDir);
  659. //EditorUtility.DisplayProgressBar("Copy Lua", "Copy Opera...", 0.3f);
  660. //CopyLuaAsset(Constants.LuaOpera);
  661. //EditorUtility.DisplayProgressBar("Copy Lua", "Copy Battle...", 0.4f);
  662. //CopyLuaAsset(Constants.LuaBattle);
  663. EditorUtility.DisplayProgressBar("Copy Lua", "Copy PubSec...", 0.5f);
  664. CopyLuaAsset(Constants.PubSec);
  665. EditorUtility.DisplayProgressBar("Copy Lua", "Process AssetBundleCfg...", 0.6f);
  666. ProcessConfigAndLua();
  667. EditorUtility.ClearProgressBar();
  668. }
  669. static void CopyLuaAsset(string dir)
  670. {
  671. string path = Application.dataPath;
  672. path = Path.Combine(path.Replace("Assets", ""), dir);
  673. List<string> files = FileSystem.getAllFilesPathEX(path);
  674. foreach (string fileName in files)
  675. {
  676. string destName = fileName.Insert(fileName.IndexOf("Assets/") + 7, "Content/");
  677. string destFolder = Path.GetDirectoryName(destName);
  678. if (!Directory.Exists(destFolder))
  679. {
  680. Directory.CreateDirectory(destFolder);
  681. }
  682. else
  683. {
  684. if (File.Exists(destName + ".txt"))
  685. {
  686. File.Delete(destName + ".txt");
  687. }
  688. }
  689. File.Copy(fileName, destName + ".txt");
  690. if (dir.CompareTo(Constants.LuaLogicDir) == 0 && destName.Contains("_Generate"))
  691. {
  692. string[] lines = FileSystem.ReadFileLines(destName + ".txt");
  693. List<string> contents = new List<string>(lines);
  694. for (var i = contents.Count - 1; i >= 0; --i)
  695. {
  696. if (contents[i].StartsWith("---@") || contents[i].Equals(""))
  697. {
  698. contents.RemoveAt(i);
  699. }
  700. }
  701. File.WriteAllLines(destName + ".txt", contents.ToArray());
  702. }
  703. }
  704. AssetDatabase.Refresh();
  705. }
  706. static void ProcessConfigAndLua()
  707. {
  708. //string luaCfgABName = Constants.LuaConfigDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  709. string luaABName = Constants.LuaDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  710. string luaLogicABName = Constants.LuaLogicDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  711. string luaPbABName = Constants.LuaPbDir.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  712. //string luaOperaABName = Constants.LuaOpera.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  713. //string luaBattleABName = Constants.LuaBattle.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  714. string pubSecABName = Constants.PubSec.Replace("Assets", "").Replace("Content", "").Replace("/", "").ToLower();
  715. //AssetDatabase.RemoveAssetBundleName("config", true);
  716. //AssetDatabase.RemoveAssetBundleName(luaCfgABName, true);
  717. AssetDatabase.RemoveAssetBundleName(luaABName, true);
  718. AssetDatabase.RemoveAssetBundleName(luaLogicABName, true);
  719. AssetDatabase.RemoveAssetBundleName(luaPbABName, true);
  720. //AssetDatabase.RemoveAssetBundleName(luaOperaABName, true);
  721. //AssetDatabase.RemoveAssetBundleName(luaBattleABName, true);
  722. AssetDatabase.RemoveAssetBundleName(pubSecABName, true);
  723. //ProcessConfigAndLua(Constants.CsvConfig, "config");
  724. //ProcessConfigAndLua(Constants.ABLuaConfigDir, luaCfgABName);
  725. ProcessConfigAndLua(Constants.ABLuaPbDir, luaPbABName);
  726. ProcessConfigAndLua(Constants.ABLuaDir, luaABName);
  727. ProcessConfigAndLua(Constants.ABLuaLogicDir, luaLogicABName);
  728. //ProcessConfigAndLua(Constants.ABLuaOpera, luaOperaABName);
  729. //ProcessConfigAndLua(Constants.ABLuaBattle, luaBattleABName);
  730. ProcessConfigAndLua(Constants.ABPubsec, pubSecABName);
  731. }
  732. static void ProcessConfigAndLua(string dir, string abName)
  733. {
  734. List<string> fileList = FileSystem.getAllFilesPathEX(dir);
  735. for (int idx = 0; idx < fileList.Count; idx++)
  736. {
  737. string fullPath = fileList[idx];
  738. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  739. if (relativePath.Contains("/Pb/") && dir!= Constants.ABLuaPbDir) continue;
  740. AssetImporter assetImporter = AssetImporter.GetAtPath(relativePath);
  741. assetImporter.assetBundleName = abName + ".unity3d";
  742. }
  743. }
  744. #endregion
  745. #endregion
  746. [MenuItem("Assets/CheckSceneDep")]
  747. static void ParseScene()
  748. {
  749. if (Selection.activeObject == null) return;
  750. string fullPath = AssetDatabase.GetAssetPath(Selection.activeObject);
  751. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  752. string[] dependencyAssets = AssetDatabase.GetDependencies(relativePath);
  753. for (int kdx = 0; kdx < dependencyAssets.Length; kdx++)
  754. {
  755. if(!dependencyAssets[kdx].Contains(".cs"))
  756. Debug.LogError(dependencyAssets[kdx]);
  757. }
  758. }
  759. [MenuItem("AssetBundle/RemoveAssetBundle")]
  760. public static void RemoveUnusedAssetBundleName()
  761. {
  762. string[] abNames = AssetDatabase.GetAllAssetBundleNames();
  763. for(int idx =0; idx < abNames.Length; idx++)
  764. {
  765. AssetDatabase.RemoveAssetBundleName(abNames[idx], true);
  766. }
  767. AssetDatabase.Refresh();
  768. AssetDatabase.RemoveUnusedAssetBundleNames();
  769. }
  770. [MenuItem("AssetBundle/CleanUnusedAB")]
  771. public static void CleanUnusedAB()
  772. {
  773. string[] abNames = AssetDatabase.GetAllAssetBundleNames();
  774. string[] files = Directory.GetFiles(s_TempAssetBundlePath);
  775. for(int idx =0; idx < files.Length;idx++)
  776. {
  777. string filePath = files[idx];
  778. if (Path.GetExtension(filePath) == ".manifest") continue;
  779. string fileName = FileUtils.ExtractPureName(filePath);
  780. bool find = false;
  781. for(int jdx =0; jdx < abNames.Length; jdx++)
  782. {
  783. if(fileName == abNames[jdx])
  784. {
  785. find = true;
  786. break;
  787. }
  788. }
  789. if (!find)
  790. {
  791. File.Delete(filePath);
  792. }
  793. }
  794. }
  795. [MenuItem("AssetBundle/PrintAssetBundleName")]
  796. static void PrintAssetBundleName()
  797. {
  798. StringBuilder strBuilder = new StringBuilder();
  799. string[] abNames = AssetDatabase.GetAllAssetBundleNames();
  800. for (int idx = 0; idx < abNames.Length; idx++)
  801. {
  802. strBuilder.AppendLine(abNames[idx]);
  803. }
  804. Debug.Log(strBuilder.ToString());
  805. }
  806. /**
  807. [MenuItem("AssetBundle/GenAssetBundleConfig")]
  808. public static void GetAssetBundleConfig()
  809. {
  810. AssetDatabase.RemoveUnusedAssetBundleNames();
  811. HashSet<string> resHash = new HashSet<string>();
  812. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create Shader AssetBundle Config...", 0.1f);
  813. ProcessShaders(resHash);
  814. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create Font AssetBundle Config...", 0.2f);
  815. ProcessFont(resHash);
  816. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create Materials AssetBundle Config...", 0.2f);
  817. ProcessMaterials(resHash);
  818. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create Config AssetBundle Config...", 0.3f);
  819. ProcessConfig(resHash);
  820. EditorUtility.DisplayProgressBar("Generate Bundle Audio", "Create Config AssetBundle Config...", 0.35f);
  821. ProcessAudio(resHash);
  822. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create Icons AssetBundle Config...", 0.4f);
  823. ProcessIcons(resHash);
  824. ProcessCamera(resHash);
  825. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create Actor AssetBundle Config...", 0.5f);
  826. ProcessAnimator(resHash);
  827. ProcessActor(resHash);
  828. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create Fx AssetBundle Config...", 0.6f);
  829. ProcessEffect(resHash);
  830. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create UI AssetBundle Config...", 0.7f);
  831. ProcessUI(resHash);
  832. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create Scene AssetBundle Config...", 0.8f);
  833. ProcessScene(resHash);
  834. EditorUtility.DisplayProgressBar("Generate Bundle Config", "Create The First AssetBundle Config...", 1f);
  835. EditorUtility.ClearProgressBar();
  836. }
  837. [MenuItem("AssetBundle/Build All AssetBundles")]
  838. public static void BuildAllAssetBundles()
  839. {
  840. string assetBundlePath = BundleBuilderZ.GetAssetbundlesPath();
  841. AssetDatabase.Refresh();
  842. if (!Directory.Exists(assetBundlePath))
  843. Directory.CreateDirectory(assetBundlePath);
  844. if (!Directory.Exists(s_TempAssetBundlePath))
  845. Directory.CreateDirectory(s_TempAssetBundlePath);
  846. CleanUnusedAB();
  847. CopyLua();
  848. try
  849. {
  850. AssetBundleManifest abm = BuildPipeline.BuildAssetBundles(s_TempAssetBundlePath, buildOptions, EditorUserBuildSettings.activeBuildTarget);
  851. if (abm)
  852. {
  853. DebugHelper.Log("<color=green>================ Build All Assetbundles Success================</color>");
  854. }
  855. else
  856. {
  857. DebugHelper.Log("<color=green>================ Build All Assetbundles Fail================</color>");
  858. }
  859. GetAllAssetsNameInAssetBundle(abm);
  860. foreach (string file in Directory.GetFiles(s_TempAssetBundlePath))
  861. {
  862. if (Path.GetExtension(file) != ".manifest")
  863. {
  864. string fileName = Path.GetFileName(file);
  865. string des = Path.Combine(assetBundlePath, fileName);
  866. if (File.Exists(des))
  867. File.Delete(des);
  868. File.Copy(file, des);
  869. }
  870. }
  871. BundleBuilderZ.BundleBuidlerCopyToStreamingAssets();
  872. }catch(System.Exception e)
  873. {
  874. Debug.LogError(e.Message);
  875. }
  876. }
  877. /**/
  878. public static void GetAllAssetsNameInAssetBundle(AssetBundleManifest abm, VersionCode resVersionCode)
  879. {
  880. GetAllAssetsNameAndCRC(abm.GetAllAssetBundles(), resVersionCode);
  881. }
  882. static void GetAllAssetsNameAndCRC(string[] abNames, VersionCode resVersionCode)
  883. {
  884. Dictionary<string, List<string>> dicSet = new Dictionary<string, List<string>>();
  885. //Get all assets names in assetbundle
  886. int SkipCheck = 2;
  887. for (int i = 0; i < abNames.Length; i++)
  888. {
  889. string abName = abNames[i];
  890. if (string.IsNullOrEmpty(abName))
  891. continue;
  892. string abPath = s_TempAssetBundlePath + "/" + abName + s_ManifestFileExtension;
  893. if (SkipCheck > 0 && ((abName + s_ManifestFileExtension) == s_TempAssetBundlePath || abName == "assetsmapping"))
  894. {
  895. SkipCheck--;
  896. continue;
  897. }
  898. string[] assetNames = GetAssetsNamesExcludeFileExtension(abPath);
  899. for (int j = 0; j < assetNames.Length; j++)
  900. {
  901. string assetName = assetNames[j];
  902. if (dicSet.ContainsKey(assetName))
  903. {
  904. if (dicSet[assetName].Contains(abName))
  905. {
  906. Debug.LogError(string.Format("资源名重复!AssetName: {0} in AssetBundle: {1}", assetName, abName));
  907. }
  908. else
  909. {
  910. dicSet[assetName].Add(abName);
  911. }
  912. }
  913. else
  914. {
  915. dicSet.Add(assetName, new List<string>());
  916. dicSet[assetName].Add(abName);
  917. }
  918. }
  919. }
  920. foreach (KeyValuePair<string, List<string>> kv in dicSet)
  921. {
  922. if (kv.Value.Count > 1)
  923. {
  924. string strAbList = "";
  925. foreach (string ab in kv.Value)
  926. {
  927. if (strAbList.Length == 0)
  928. strAbList += ab;
  929. else
  930. strAbList += (", " + ab);
  931. }
  932. Debug.LogWarning(string.Format("资源{0}被打包到多个assetbundle中:{1}", kv.Key, strAbList));
  933. }
  934. }
  935. StringBuilder sb = new StringBuilder();
  936. sb.Append("resversioncode");
  937. sb.Append(",");
  938. sb.Append(resVersionCode.ToString());
  939. sb.Append("\r\n");
  940. AssetsObscureUtil.WriteInfoData(ref sb);
  941. foreach(var p in dicSet)
  942. {
  943. string assetName = p.Key;
  944. List<string> abList = p.Value;
  945. if(abList.Count == 0)
  946. {
  947. Debug.LogError(assetName + " 为什么没有在AB包中");
  948. continue;
  949. }
  950. sb.Append(assetName);
  951. sb.Append(",");
  952. sb.Append(abList[0]);
  953. sb.Append("\r\n");
  954. }
  955. //Create a text file
  956. UTF8Encoding encoding = new UTF8Encoding(false);
  957. byte[] bytes = encoding.GetBytes(sb.ToString());
  958. using (var fs = File.Open(s_TempAssetBundlePath +$"/{ GetAssetsMappingName()}.bytes", FileMode.OpenOrCreate, FileAccess.ReadWrite))
  959. {
  960. fs.Write(bytes, 0, bytes.Length);
  961. }
  962. }
  963. private static string GetAssetsMappingName()
  964. {
  965. #if UNITY_IOS
  966. return "afi";
  967. #else
  968. return "assetsmapping";
  969. #endif
  970. }
  971. static string[] GetAssetsNamesExcludeFileExtension(string path)
  972. {
  973. bool isAsset = false;
  974. List<string> abNames = new List<string>();
  975. string[] allLines = File.ReadAllLines(path);
  976. for (int i = 0; i < allLines.Length; i++)
  977. {
  978. if (allLines[i] == "Assets:")
  979. {
  980. isAsset = true;
  981. continue;
  982. }
  983. if (isAsset)
  984. {
  985. if (allLines[i][0] == '-')
  986. {
  987. string assetName = allLines[i].Replace("- ", "").Trim();
  988. abNames.Add(assetName);
  989. }
  990. else
  991. break;
  992. }
  993. }
  994. return abNames.ToArray();
  995. }
  996. #region Jenkins
  997. public static void BuildApp(bool debug = false)
  998. {
  999. AssetBundleConstant.IsDebug = debug;
  1000. BundleBuilderZ.DisableScene();
  1001. BundleBuilderZ.BuildApp(debug);
  1002. if(!debug)
  1003. BundleBuilderZ.EnableScene();
  1004. }
  1005. public static void SwitchIosPlatform()
  1006. {
  1007. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.iOS, BuildTarget.iOS);
  1008. }
  1009. public static void SwitchAndroidPlatform()
  1010. {
  1011. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
  1012. }
  1013. #endregion
  1014. }