AssetBundlesExport.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEditor;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. using System.Xml;
  9. using System.Runtime.InteropServices;
  10. /// <summary>
  11. /// 打包Asset Bundle(包括场景和普通资源)
  12. /// By WJ 2017-12-1 10:35:48
  13. /// </summary>
  14. public class AssetBundlesExport
  15. {
  16. /// <summary>
  17. /// 资源包后缀
  18. /// </summary>
  19. private static string AssetBundleURE = ".unity3d";
  20. /// <summary>
  21. /// 创建安卓资源(自动路径)
  22. /// </summary>
  23. [MenuItem("Build/Android平台 CreateAssetBundles", false, 101)]
  24. private static void CreateAssetBunlde_Android()
  25. {
  26. CreateAssetBundle(BuildTarget.Android);
  27. }
  28. /// <summary>
  29. /// 创建安卓场景资源(自动路径)
  30. /// </summary>
  31. [MenuItem("Build/Android平台 CreateScenesAssetBunldes", false, 102)]
  32. private static void CreateScenesAssetBunldes_Android()
  33. {
  34. CreateScenesAssetBunldes(BuildTarget.Android);
  35. }
  36. private static void CreateAssetBundle(BuildTarget buildTarget)
  37. {
  38. // 获取在Project视图中选择的所有游戏对象
  39. Object[] SelectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  40. string path = Application.dataPath + "/StreamingAssets/NewRes";
  41. if (0 != path.Length)
  42. {
  43. // 遍历所有的游戏对象
  44. foreach (Object obj in SelectedAssets)
  45. {
  46. // 文件夹不打包
  47. if (obj.GetType().Name == "DefaultAsset")
  48. {
  49. continue;
  50. }
  51. string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
  52. #pragma warning disable
  53. if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
  54. BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression, buildTarget))
  55. {
  56. UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
  57. }
  58. else
  59. {
  60. UnityEngine.Debug.Log("资源打包失败:" + obj.name);
  61. }
  62. #pragma warning restore
  63. }
  64. }
  65. // 刷新编辑器
  66. AssetDatabase.Refresh();
  67. }
  68. // /// <summary>
  69. // /// 创建Windows资源(自动路径)
  70. // /// </summary>
  71. // [MenuItem("Build/Create AssetBunldes_Windows_ChoosePath")]
  72. // private static void CreateAssetBunlde_Windows_ChoosePath()
  73. // {
  74. // CreateAssetBunldes_Choose(BuildTarget.StandaloneWindows);
  75. // }
  76. // /// <summary>
  77. // /// 创建安卓资源(自动路径)
  78. // /// </summary>
  79. // [MenuItem("Build/Create AssetBunldes_Android_ChoosePath")]
  80. // private static void CreateAssetBunlde_Android_ChoosePath()
  81. // {
  82. // CreateAssetBunldes_Choose(BuildTarget.Android);
  83. // }
  84. // /// <summary>
  85. // /// 打包安卓资源 (自己选择路径)
  86. // /// </summary>
  87. // private static void CreateAssetBunldes_Choose(BuildTarget buildTarget)
  88. // {
  89. // string path = EditorUtility.SaveFolderPanel("save bundle", string.Empty, string.Empty);
  90. // if (0 != path.Length)
  91. // {
  92. // // 获取在Project视图中选择的所有游戏对象
  93. // Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  94. // // 遍历所有的游戏对象
  95. // foreach (Object obj in SelectedAsset)
  96. // {
  97. // // 文件夹不打包
  98. // if (obj.GetType().Name == "DefaultAsset")
  99. // {
  100. // continue;
  101. // }
  102. // string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
  103. //#pragma warning disable
  104. // if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
  105. // BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle,
  106. // buildTarget))
  107. // {
  108. // UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
  109. // }
  110. // else
  111. // {
  112. // UnityEngine.Debug.Log("资源打包失败:" + obj.name);
  113. // }
  114. //#pragma warning restore
  115. // }
  116. // }
  117. // // 刷新编辑器
  118. // AssetDatabase.Refresh();
  119. // }
  120. /// <summary>
  121. /// 创建Windows资源(自动路径)
  122. /// </summary>
  123. [MenuItem("Build/Windows平台 CreateAssetBundles", false, 201)]
  124. private static void CreateAssetBunlde_Windows()
  125. {
  126. CreateAssetBundle(BuildTarget.StandaloneWindows);
  127. }
  128. /// <summary>
  129. /// 创建Windows场景资源(自动路径)
  130. /// </summary>
  131. [MenuItem("Build/Windows平台 CreateScenesAssetBunldes", false, 202)]
  132. private static void CreateScenesAssetBunldes_Windows()
  133. {
  134. CreateScenesAssetBunldes(BuildTarget.StandaloneWindows);
  135. }
  136. /// <summary>
  137. /// 创建安卓场景资源(自动路径)
  138. /// </summary>
  139. private static void CreateScenesAssetBunldes(BuildTarget buildTarget)
  140. {
  141. string[] paths = GetScenePathFormSelectionFile();
  142. string[] names = new string[1];
  143. for (int i = 0; i < paths.Length; ++i)
  144. {
  145. string s = paths[i];
  146. // 分解出场景名字
  147. string SceneName = s.Substring(s.LastIndexOf('/') + 1, s.Length - s.LastIndexOf('/') - 1);
  148. //将要生成AssetBundle资源的路径及名称
  149. string targetPathName = Application.dataPath + "/StreamingAssets/NewRes/" + SceneName.ToLower().Substring(0, SceneName.LastIndexOf('.')) + AssetBundleURE;
  150. //场景文件路径及名称
  151. names[0] = paths[i];
  152. // 新版打包方式
  153. BuildPipeline.BuildPlayer(names, targetPathName, buildTarget, BuildOptions.BuildAdditionalStreamedScenes);
  154. UnityEngine.Debug.Log("场景打包成功:" + paths[i] + " 路径:" + targetPathName);
  155. }
  156. // 刷新编辑器
  157. AssetDatabase.Refresh();
  158. }
  159. //#if UNITY_IOS
  160. // /// <summary>
  161. // /// 打包IOS资源(自动路径 )
  162. // /// </summary>
  163. // [MenuItem("Build/Create AssetBunldes IOS")]
  164. // private static void CreateAssetBunldes_IOS()
  165. // {
  166. // // string path = EditorUtility.SaveFolderPanel("save bundle", "", "");
  167. // string path = Application.dataPath + "/StreamingAssets/NewRes";
  168. // if (0 != path.Length)
  169. // {
  170. // // 获取在Project视图中选择的所有游戏对象
  171. // Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  172. // // 遍历所有的游戏对象
  173. // foreach (Object obj in SelectedAsset)
  174. // {
  175. // // 文件夹不打包
  176. // if (obj.GetType().Name == "DefaultAsset")
  177. // {
  178. // continue;
  179. // }
  180. // string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
  181. //#pragma warning disable
  182. // if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
  183. // BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.iOS))
  184. // {
  185. // UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
  186. // }
  187. // else
  188. // {
  189. // UnityEngine.Debug.Log("资源打包失败:" + obj.name);
  190. // }
  191. //#pragma warning restore
  192. // }
  193. // }
  194. // // 刷新编辑器
  195. // AssetDatabase.Refresh();
  196. // }
  197. // /// <summary>
  198. // /// 打包IOS资源 (自己选择路径)
  199. // /// </summary>
  200. // [MenuItem("Build/Create AssetBunldes IOS - ChoosePath")]
  201. // private static void CreateAssetBunldes_IosChoose()
  202. // {
  203. // string path = EditorUtility.SaveFolderPanel("save bundle", string.Empty, string.Empty);
  204. // if (0 != path.Length)
  205. // {
  206. // // 获取在Project视图中选择的所有游戏对象
  207. // Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  208. // // 遍历所有的游戏对象
  209. // foreach (Object obj in SelectedAsset)
  210. // {
  211. // // 文件夹不打包
  212. // if (obj.GetType().Name == "DefaultAsset")
  213. // {
  214. // continue;
  215. // }
  216. // string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
  217. //#pragma warning disable
  218. // if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
  219. // BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle,
  220. // BuildTarget.iOS))
  221. // {
  222. // UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
  223. // }
  224. // else
  225. // {
  226. // UnityEngine.Debug.Log("资源打包失败:" + obj.name);
  227. // }
  228. //#pragma warning restore
  229. // }
  230. // }
  231. // // 刷新编辑器
  232. // AssetDatabase.Refresh();
  233. // }
  234. // /// <summary>
  235. // /// 创建IOS场景资源(自动路径)
  236. // /// </summary>
  237. // [MenuItem("Build/Create ScenesAssetBunldes IOS")]
  238. // private static void CreateScenesAssetBunldes_IOS()
  239. // {
  240. // string[] paths = GetScenePathFormSelectionFile();
  241. // string[] names = new string[1];
  242. // for (int i = 0; i < paths.Length; ++i)
  243. // {
  244. // string s = paths[i];
  245. // // 分解出场景名字
  246. // string SceneName = s.Substring(s.LastIndexOf('/') + 1, s.Length - s.LastIndexOf('/') - 1);
  247. // //将要生成AssetBundle资源的路径及名称
  248. // string targetPathName = Application.dataPath + "/StreamingAssets/NewRes/" + SceneName.Substring(0, SceneName.LastIndexOf('.')) + AssetBundleURE;
  249. // //场景文件路径及名称
  250. // names[0] = paths[i];
  251. // ////官方已弃用,但是新版用起来不如旧版方便,此处仍用旧版的场景打包方式
  252. // //BuildPipeline.BuildStreamedSceneAssetBundle(names, targetPathName, BuildTarget.iOS);
  253. // // 新版打包方式
  254. // BuildPipeline.BuildPlayer(names, targetPathName, BuildTarget.iOS, BuildOptions.BuildAdditionalStreamedScenes);
  255. // UnityEngine.Debug.Log("场景打包成功:" + paths[i] + " 路径:" + targetPathName);
  256. // }
  257. // // 刷新编辑器
  258. // AssetDatabase.Refresh();
  259. // }
  260. // /// <summary>
  261. // /// 分割符,无实际意义
  262. // /// </summary>
  263. // [MenuItem("Build/--------------------------")]
  264. // private static void LineFun()
  265. // {
  266. // }
  267. //#endif
  268. //#if PLATFORM_STANDALONE_WIN
  269. // /// <summary>
  270. // /// 创建Windows资源(自动路径)
  271. // /// </summary>
  272. // [MenuItem("Build/Create AssetBunldes Windows")]
  273. // private static void CreateAssetBunldes_Android()
  274. // {
  275. // // 获取在Project视图中选择的所有游戏对象
  276. // Object[] SelectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  277. // string path = Application.dataPath + "/StreamingAssets/NewRes";
  278. // if (0 != path.Length)
  279. // {
  280. // // 遍历所有的游戏对象
  281. // foreach (Object obj in SelectedAssets)
  282. // {
  283. // // 文件夹不打包
  284. // if (obj.GetType().Name == "DefaultAsset")
  285. // {
  286. // continue;
  287. // }
  288. // string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
  289. //#pragma warning disable
  290. // if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
  291. // BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows))
  292. // {
  293. // UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
  294. // }
  295. // else
  296. // {
  297. // UnityEngine.Debug.Log("资源打包失败:" + obj.name);
  298. // }
  299. //#pragma warning restore
  300. // }
  301. // }
  302. // // 刷新编辑器
  303. // AssetDatabase.Refresh();
  304. // }
  305. //#endif
  306. // /// <summary>
  307. // /// 打开本地沙盒
  308. // /// </summary>
  309. // [MenuItem("Build/打开本地沙盒")]
  310. // static void OpenSandbox()
  311. // {
  312. //#if UNITY_EDITOR_WIN
  313. // // 删除文件夹
  314. // string resPath = Application.persistentDataPath;
  315. // resPath = resPath.Replace("/", "\\");
  316. // //打开文件夹
  317. // System.Diagnostics.Process.Start("explorer.exe", resPath);
  318. //#else
  319. // Debug.Log("IOS 暂未支持此功能!");
  320. //#endif
  321. // }
  322. // /// <summary>
  323. // /// 清空本地版本信息
  324. // /// </summary>
  325. // [MenuItem("Build/清空本地版本信息")]
  326. // static void ClearLocalVerInfo()
  327. // {
  328. // // 删除 本地文件夹
  329. // string resPath = Application.persistentDataPath + "/NewRes";
  330. // if (Directory.Exists(resPath))
  331. // {
  332. // Directory.Delete(resPath, true);
  333. // }
  334. // // 删除 网络文件夹
  335. // string netPath = Application.persistentDataPath + "/NetRes";
  336. // if (Directory.Exists(netPath))
  337. // {
  338. // Directory.Delete(netPath, true);
  339. // }
  340. // PlayerPrefs.DeleteKey("YLSJ_LastUpdateInfo");
  341. // UnityEngine.Debug.Log("[Version] 本地版本信息已清空!");
  342. // }
  343. // /// <summary>
  344. // /// 创建一个vs用的项目xml
  345. // /// </summary>
  346. // [MenuItem("Tools/创建DLL项目用的xml")]
  347. // private static void CreateDllXml()
  348. // {
  349. // string savePath = Application.dataPath + "/../../YLSJShared/TestCreateDllXml/TestCreateDllXml";
  350. // XmlDocument XmlDoc = new XmlDocument();
  351. // XmlDoc.Load(savePath + "/Core.csproj");
  352. // XmlNodeList xnl = XmlDoc.ChildNodes[0].ChildNodes;
  353. // if (XmlDoc.ChildNodes[0].Name.ToLower() != "project")
  354. // xnl = XmlDoc.ChildNodes[1].ChildNodes;
  355. // XmlNode XmlRoot = xnl.Item(4);
  356. // DirectoryInfo diretoryInfo = new DirectoryInfo(Application.dataPath + "/Scripts/Core");
  357. // DirectoryInfo[] dirInfo = diretoryInfo.GetDirectories();
  358. // XmlRoot.RemoveAll();
  359. // //遍历文件夹
  360. // FileInfo[] _fileInfo = diretoryInfo.GetFiles("*.cs", SearchOption.AllDirectories);
  361. // foreach (FileInfo NextFile in _fileInfo) //遍历文件
  362. // {
  363. // if (NextFile.DirectoryName.Contains("Editor"))
  364. // {
  365. // continue;
  366. // }
  367. // XmlElement xmlCompile = XmlDoc.CreateElement("Compile", "http://schemas.microsoft.com/developer/msbuild/2003");
  368. // XmlRoot.AppendChild(xmlCompile);
  369. // xmlCompile.SetAttribute("Include", "Core" + NextFile.DirectoryName.Replace(diretoryInfo.FullName, "") + "\\" + NextFile.Name);
  370. // }
  371. // XmlDoc.Save(savePath + "/Core.csproj");
  372. // Debug.Log("已经完成");
  373. // }
  374. // /// <summary>
  375. // /// 创建一个vs用的项目xml
  376. // /// </summary>
  377. // [MenuItem("Tools/创建DLL项目用的xml(WJ)")]
  378. // private static void CreateDllXmlWJ()
  379. // {
  380. // string savePath = "E:/YLSJShared/TestCreateDllXml/TestCreateDllXml";
  381. // XmlDocument XmlDoc = new XmlDocument();
  382. // XmlDoc.Load(savePath + "/Core.csproj");
  383. // XmlNodeList xnl = XmlDoc.ChildNodes[0].ChildNodes;
  384. // if (XmlDoc.ChildNodes[0].Name.ToLower() != "project")
  385. // xnl = XmlDoc.ChildNodes[1].ChildNodes;
  386. // XmlNode XmlRoot = xnl.Item(4);
  387. // DirectoryInfo diretoryInfo = new DirectoryInfo(Application.dataPath + "/Scripts/Core");
  388. // DirectoryInfo[] dirInfo = diretoryInfo.GetDirectories();
  389. // XmlRoot.RemoveAll();
  390. // //遍历文件夹
  391. // FileInfo[] _fileInfo = diretoryInfo.GetFiles("*.cs", SearchOption.AllDirectories);
  392. // foreach (FileInfo NextFile in _fileInfo) //遍历文件
  393. // {
  394. // if (NextFile.DirectoryName.Contains("Editor"))
  395. // {
  396. // continue;
  397. // }
  398. // XmlElement xmlCompile = XmlDoc.CreateElement("Compile", "http://schemas.microsoft.com/developer/msbuild/2003");
  399. // XmlRoot.AppendChild(xmlCompile);
  400. // xmlCompile.SetAttribute("Include", "Core" + NextFile.DirectoryName.Replace(diretoryInfo.FullName, "") + "\\" + NextFile.Name);
  401. // }
  402. // XmlDoc.Save(savePath + "/Core.csproj");
  403. // Debug.Log("已经完成");
  404. // }
  405. ///// <summary>
  406. ///// 创建安卓资源(自动路径 新版)
  407. ///// </summary>
  408. //[MenuItem("Build/Create AssetBunldes AndroidNew")]
  409. //private static void CreateAssetBunldes_AndroidNew()
  410. //{
  411. // //// 获取在Project视图中选择的所有游戏对象
  412. // //Object[] SelectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  413. // //foreach (Object selected in SelectedAssets)
  414. // //{
  415. // // //返回所有对象相对于工程目录的存储路径如Assets/_Scenes/Main.unity
  416. // // string path = AssetDatabase.GetAssetPath(selected);
  417. // // //把一个目录的对象检索为AssetImporter
  418. // // AssetImporter asset = AssetImporter.GetAtPath(path);
  419. // // asset.assetBundleName = selected.name; //设置Bundle文件的名称
  420. // // asset.assetBundleVariant = "unity3d";//设置Bundle文件的扩展名
  421. // // asset.SaveAndReimport();
  422. // //}
  423. // //AssetDatabase.Refresh();
  424. // //如果目录不存在,就创建一个目录
  425. // string targetPath = Application.dataPath + "/StreamingAssets/NewRes";
  426. // if (!Directory.Exists(targetPath))
  427. // {
  428. // Directory.CreateDirectory(targetPath);
  429. // }
  430. // BuildPipeline.BuildAssetBundles(targetPath, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.Android);
  431. // // 刷新编辑器
  432. // AssetDatabase.Refresh();
  433. //}
  434. /////// <summary>
  435. /////// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
  436. /////// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下
  437. /////// </summary>
  438. ////private static void ClearAssetBundlesName()
  439. ////{
  440. //// int length = AssetDatabase.GetAllAssetBundleNames().Length;
  441. //// Debug.Log(length);
  442. //// string[] oldAssetBundleNames = new string[length];
  443. //// for (int i = 0; i < length; i++)
  444. //// {
  445. //// oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
  446. //// }
  447. //// for (int j = 0; j < oldAssetBundleNames.Length; j++)
  448. //// {
  449. //// AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
  450. //// }
  451. //// length = AssetDatabase.GetAllAssetBundleNames().Length;
  452. ////}
  453. /// <summary>
  454. /// 得到鼠标选中的场景文件,只返回工程内的文件名及后缀
  455. /// </summary>
  456. /// <returns>所有选中的场景文件名</returns>
  457. private static string[] GetScenePathFormSelectionFile()
  458. {
  459. UnityEngine.Object[] ScenePaths = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
  460. List<string> Scenes = new List<string>();
  461. for (int i = 0; i < ScenePaths.Length; ++i)
  462. {
  463. // 排除文件夹
  464. if (ScenePaths[i].GetType().Name == "SceneAsset")
  465. {
  466. string s = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(ScenePaths[i]);
  467. //Debug.Log("完整路径:" + s);
  468. string ss = s.Substring(s.LastIndexOf("Assets/"), s.Length - s.LastIndexOf("Assets/"));
  469. Scenes.Add(ss);
  470. //Debug.Log("使用路径:" + ss);
  471. }
  472. }
  473. return Scenes.ToArray();
  474. }
  475. }