PackWindowSinglePlatform.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace Pack
  6. {
  7. public partial class PackWindow
  8. {
  9. private GUIContent[] contents = new GUIContent[]
  10. {
  11. new GUIContent("Android"),
  12. new GUIContent("iOS"),
  13. new GUIContent("StandaloneWindows64"),
  14. new GUIContent("WeiDuanAndroid"),
  15. new GUIContent("H5"),
  16. };
  17. private Type[] types = new Type[]
  18. {
  19. typeof(PackPlatformAndroid),
  20. typeof(PackPlatformiOS),
  21. typeof(PackPlatformPC),
  22. typeof(PackPlatformWeiDuanAndroid),
  23. typeof(PackPlatformH5),
  24. };
  25. private string[] channelIds = new string[]
  26. {
  27. "10000000",
  28. "20000000",
  29. "1",
  30. "10000000",
  31. "50000000",
  32. };
  33. private int m_SelectPackPlatformNameIdx = -1;
  34. private int m_SelectPackPlatformNameIdxTemp = -1;
  35. private Vector2 m_ScrollPosition = Vector2.zero;
  36. private bool m_Editable = false;
  37. private PackPlatformBase m_PlatformBase = null;
  38. private PackPlatformBase m_PlatformBaseChange = null;
  39. private void DrawPackGUI()
  40. {
  41. EditorGUILayout.BeginVertical();
  42. EditorGUILayout.Space();
  43. m_SelectPackPlatformNameIdxTemp = m_SelectPackPlatformNameIdx;
  44. Rect position = EditorGUILayout.GetControlRect(false, PackGUI.kSingleLineHeight, PackGUI.styles.popup);
  45. m_SelectPackPlatformNameIdxTemp = EditorGUI.Popup(position, m_SelectPackPlatformNameIdxTemp, m_PackPlatformNames, PackGUI.styles.popup);
  46. if (m_SelectPackPlatformNameIdx != m_SelectPackPlatformNameIdxTemp)
  47. {
  48. m_SelectPackPlatformNameIdx = m_SelectPackPlatformNameIdxTemp;
  49. CheckSelectPackPlatform(true);
  50. if (m_PlatformBase != null)
  51. {
  52. EditorPrefs.SetString(SELECT_PLAT_SAVE_KEY, m_PlatformBase.channelUniqueId);
  53. }
  54. }
  55. else
  56. {
  57. CheckSelectPackPlatform(false);
  58. }
  59. bool isChanged = (m_PlatformBase != null && m_PlatformBaseChange != null && !m_PlatformBase.Equals(m_PlatformBaseChange));
  60. EditorGUILayout.Space();
  61. EditorGUILayout.BeginHorizontal();
  62. GUILayout.FlexibleSpace();
  63. if (isChanged)
  64. {
  65. PackGUI.BeginChangeGUIColor(Color.green);
  66. if (GUILayout.Button("保存修改", PackGUI.styles.miniButtonLeft))
  67. {
  68. SavePlatformSingleChange(false);
  69. }
  70. PackGUI.EndChangeGUIColor();
  71. PackGUI.BeginChangeGUIColor(Color.red);
  72. if (GUILayout.Button("取消修改", PackGUI.styles.miniButtonMid))
  73. {
  74. CheckSelectPackPlatform(true);
  75. }
  76. PackGUI.EndChangeGUIColor();
  77. }
  78. bool editableTemp = GUILayout.Toggle(m_Editable, "解锁修改", isChanged ? PackGUI.styles.miniButtonMid : PackGUI.styles.miniButtonLeft);
  79. if (editableTemp != m_Editable)
  80. {
  81. if (!SavePlatformSingleChange())
  82. {
  83. m_Editable = editableTemp;
  84. }
  85. }
  86. position = EditorGUILayout.GetControlRect(true, PackGUI.kSingleLineHeight, PackGUI.styles.miniButtonMid, GUILayout.Width(40));
  87. if (GUI.Button(position, "+", PackGUI.styles.miniButtonMid))
  88. {
  89. if (!SavePlatformSingleChange())
  90. {
  91. EditorUtility.DisplayCustomMenu(position, contents, -1, (object userData, string[] options, int selected) =>
  92. {
  93. PackPlatformBase packPlatformBase = null;
  94. if (selected < 0 || selected >= types.Length)
  95. {
  96. Debug.LogError("未知平台");
  97. }
  98. else
  99. {
  100. packPlatformBase = (PackPlatformBase)(Activator.CreateInstance(types[selected], null));
  101. if (packPlatformBase != null)
  102. {
  103. packPlatformBase.channelUniqueId = channelIds[selected];
  104. }
  105. }
  106. // if (selected == 0)
  107. // packPlatformBase = new PackPlatformAndroid();
  108. // else if (selected == 1)
  109. // packPlatformBase = new PackPlatformiOS();
  110. // else if (selected == 2)
  111. // packPlatformBase = new PackPlatformPC();
  112. // else
  113. // Debug.LogError("未知平台");
  114. if (packPlatformBase != null)
  115. {
  116. string channelUniqueName = packPlatformBase.GetChannelUniqueName();
  117. string channelUniqueId = packPlatformBase.channelUniqueId;
  118. int index = 1;
  119. if (m_PackPlatformMap == null) m_PackPlatformMap = new Dictionary<string, PackPlatformBase>();
  120. if (m_PackPlatformNames == null) m_PackPlatformNames = new string[0];
  121. if (m_PackPlatformUniqueIds == null) m_PackPlatformUniqueIds = new string[0];
  122. while (m_PackPlatformMap.ContainsKey(channelUniqueId) || ArrayUtility.Contains(m_PackPlatformNames, channelUniqueName))
  123. {
  124. packPlatformBase.distributeName = packPlatformBase.distributeName + index;
  125. channelUniqueName = packPlatformBase.GetChannelUniqueName();
  126. channelUniqueId = channelUniqueId + index;
  127. packPlatformBase.channelUniqueId = channelUniqueId;
  128. }
  129. ArrayUtility.Add(ref m_PackPlatformNames, channelUniqueName);
  130. ArrayUtility.Add(ref m_PackPlatformUniqueIds, channelUniqueId);
  131. m_PackPlatformMap.Add(channelUniqueId, packPlatformBase);
  132. SavePackPlatforms();
  133. ResetDefaultPackPlatforms();
  134. for (int i = 0, iMax = m_PackPlatformUniqueIds.Length; i < iMax; i++)
  135. {
  136. if (m_PackPlatformUniqueIds[i] == channelUniqueId)
  137. {
  138. m_SelectPackPlatformNameIdx = i;
  139. EditorPrefs.SetString(SELECT_PLAT_SAVE_KEY, channelUniqueId);
  140. CheckSelectPackPlatform(true);
  141. break;
  142. }
  143. }
  144. }
  145. }, null);
  146. }
  147. }
  148. EditorGUI.BeginDisabledGroup((m_PlatformBase == null || m_PlatformBaseChange == null));
  149. if (GUILayout.Button("-", PackGUI.styles.miniButtonRight, GUILayout.Width(40)))
  150. {
  151. if (!SavePlatformSingleChange())
  152. {
  153. if (EditorUtility.DisplayDialog("提示", "是否删除当前配置", "是", "否"))
  154. {
  155. string channelUniqueName = m_PlatformBase.GetChannelUniqueName();
  156. string channelUniqueId = m_PlatformBase.channelUniqueId;
  157. ArrayUtility.Remove(ref m_PackPlatformNames, channelUniqueName);
  158. ArrayUtility.Remove(ref m_PackPlatformUniqueIds, channelUniqueId);
  159. m_PackPlatformMap.Remove(channelUniqueId);
  160. m_PlatformBase = null;
  161. m_PlatformBaseChange = null;
  162. m_SelectPackPlatformNameIdx = m_SelectPackPlatformNameIdx - 1;
  163. if (m_SelectPackPlatformNameIdx < 0 && m_PackPlatformNames != null && m_PackPlatformNames.Length > 0)
  164. {
  165. m_SelectPackPlatformNameIdx = 0;
  166. }
  167. if (m_PackPlatformUniqueIds != null && m_PackPlatformUniqueIds.Length > m_SelectPackPlatformNameIdx)
  168. {
  169. EditorPrefs.SetString(SELECT_PLAT_SAVE_KEY, m_PackPlatformUniqueIds[m_SelectPackPlatformNameIdx]);
  170. }
  171. else
  172. {
  173. EditorPrefs.DeleteKey(SELECT_PLAT_SAVE_KEY);
  174. }
  175. CheckSelectPackPlatform(true);
  176. SavePackPlatforms();
  177. }
  178. }
  179. }
  180. EditorGUI.EndDisabledGroup();
  181. EditorGUILayout.EndHorizontal();
  182. if (m_PlatformBase != null && m_PlatformBaseChange != null)
  183. {
  184. EditorGUILayout.BeginVertical();
  185. EditorGUILayout.Space();
  186. m_ScrollPosition = EditorGUILayout.BeginScrollView(m_ScrollPosition);
  187. m_PlatformBaseChange.DrawGUI(m_Editable, m_PlatformBase);
  188. EditorGUILayout.EndScrollView();
  189. EditorGUILayout.EndVertical();
  190. }
  191. else
  192. {
  193. GUILayout.FlexibleSpace();
  194. EditorGUILayout.LabelField(GUIContent.none, EditorGUIUtility.TrTextContentWithIcon("暂无目标平台信息", MessageType.Warning), PackGUI.styles.helpBox);
  195. GUILayout.FlexibleSpace();
  196. }
  197. EditorGUI.BeginDisabledGroup((m_PlatformBase == null || m_PlatformBaseChange == null));
  198. EditorGUILayout.Space();
  199. EditorGUILayout.Space();
  200. EditorGUILayout.BeginVertical(PackGUI.styles.box);
  201. EditorGUILayout.Space();
  202. EditorGUILayout.Space();
  203. EditorGUILayout.LabelField("当前已切换的平台信息", PackGUI.styles.boldLabel);
  204. EditorGUI.indentLevel += 1;
  205. if (m_PackPlatformMap == null || !m_PackPlatformMap.ContainsKey(PackRun.GetCurChannelUniqueId()))
  206. EditorGUILayout.LabelField("无", PackGUI.styles.label);
  207. else
  208. EditorGUILayout.LabelField(m_PackPlatformMap[PackRun.GetCurChannelUniqueId()].GetChannelUniqueName(), PackGUI.styles.label);
  209. EditorGUI.indentLevel -= 1;
  210. EditorGUILayout.Space();
  211. EditorGUILayout.Space();
  212. EditorGUILayout.BeginHorizontal();
  213. EditorGUI.BeginDisabledGroup(m_PlatformBase == null || !m_PlatformBase.CanChangePlatform());
  214. if (GUILayout.Button("切换平台", PackGUI.styles.miniButton))
  215. {
  216. if (EditorUtility.DisplayDialog("提示", "确认切换平台?", "是", "否"))
  217. {
  218. if (!SavePlatformSingleChange())
  219. {
  220. try
  221. {
  222. PackRun.ChangePlatform(m_PlatformBase);
  223. EditorUtility.DisplayDialog("提示", "切换成功", "好的");
  224. }
  225. catch (System.Exception e)
  226. {
  227. Debug.LogException(e);
  228. EditorUtility.DisplayDialog("提示", "切换失败", "好的");
  229. }
  230. }
  231. }
  232. GUIUtility.ExitGUI();
  233. }
  234. EditorGUI.EndDisabledGroup();
  235. if (GUILayout.Button("清除当前切换的平台", PackGUI.styles.miniButton))
  236. {
  237. if (EditorUtility.DisplayDialog("提示", "确认清除当前切换的平台?", "是", "否"))
  238. {
  239. if (!SavePlatformSingleChange())
  240. {
  241. try
  242. {
  243. PackRun.ClearBuildAndResetDefault();
  244. EditorUtility.DisplayDialog("提示", "清除成功", "好的");
  245. }
  246. catch (System.Exception e)
  247. {
  248. Debug.LogException(e);
  249. EditorUtility.DisplayDialog("提示", "清除失败", "好的");
  250. }
  251. }
  252. }
  253. GUIUtility.ExitGUI();
  254. }
  255. EditorGUI.BeginDisabledGroup(m_PlatformBase == null || !m_PlatformBase.CanBuildAssetBundles());
  256. if (GUILayout.Button("只打包AB", PackGUI.styles.miniButton))
  257. {
  258. if (EditorUtility.DisplayDialog("提示", "确认打包AB?", "是", "否"))
  259. {
  260. if (!SavePlatformSingleChange())
  261. {
  262. try
  263. {
  264. PackRun.BuildAB(m_PlatformBase);
  265. EditorUtility.DisplayDialog("提示", "打包AB成功", "好的");
  266. }
  267. catch (System.Exception e)
  268. {
  269. Debug.LogException(e);
  270. EditorUtility.DisplayDialog("提示", "打包AB失败", "好的");
  271. }
  272. }
  273. }
  274. GUIUtility.ExitGUI();
  275. }
  276. EditorGUI.EndDisabledGroup();
  277. EditorGUI.BeginDisabledGroup(m_PlatformBase == null || !m_PlatformBase.CanBuildApp());
  278. if (GUILayout.Button("打包App", PackGUI.styles.miniButton))
  279. {
  280. if (EditorUtility.DisplayDialog("提示", "确认打包App?", "是", "否"))
  281. {
  282. if (!SavePlatformSingleChange())
  283. {
  284. try
  285. {
  286. PackRun.BuildApp(m_PlatformBase);
  287. EditorUtility.DisplayDialog("提示", "打包App成功", "好的");
  288. }
  289. catch (System.Exception e)
  290. {
  291. Debug.LogException(e);
  292. EditorUtility.DisplayDialog("提示", "打包App失败", "好的");
  293. }
  294. }
  295. }
  296. GUIUtility.ExitGUI();
  297. }
  298. EditorGUI.EndDisabledGroup();
  299. EditorGUI.BeginDisabledGroup(m_PlatformBase == null || !m_PlatformBase.CanBuildApp());
  300. if (GUILayout.Button("仅打包Lua", PackGUI.styles.miniButton))
  301. {
  302. if (EditorUtility.DisplayDialog("提示", "确认仅打包Lua?", "是", "否"))
  303. {
  304. if (!SavePlatformSingleChange())
  305. {
  306. try
  307. {
  308. //PackRun.BuildApp(m_PlatformBase);
  309. PackRun.BuildLuaAB(m_PlatformBase);
  310. EditorUtility.DisplayDialog("提示", "仅打包Lua成功", "好的");
  311. }
  312. catch (System.Exception e)
  313. {
  314. Debug.LogException(e);
  315. EditorUtility.DisplayDialog("提示", "仅打包Lua失败", "好的");
  316. }
  317. }
  318. }
  319. GUIUtility.ExitGUI();
  320. }
  321. EditorGUI.EndDisabledGroup();
  322. EditorGUILayout.EndHorizontal();
  323. EditorGUILayout.Space();
  324. EditorGUILayout.Space();
  325. EditorGUILayout.EndVertical();
  326. EditorGUI.EndDisabledGroup();
  327. EditorGUILayout.EndVertical();
  328. PackGUI.EndAllChangeGUIColor();
  329. PackGUI.EndAllChangeGUIBgColor();
  330. }
  331. private void CheckSelectPackPlatform(bool forceUpdate)
  332. {
  333. if (!forceUpdate && m_PlatformBase != null && m_PlatformBaseChange != null) return;
  334. if (m_SelectPackPlatformNameIdx >= 0 && m_SelectPackPlatformNameIdx < m_PackPlatformUniqueIds.Length)
  335. {
  336. string channelUniqueId = m_PackPlatformUniqueIds[m_SelectPackPlatformNameIdx];
  337. if (m_PackPlatformMap != null)
  338. {
  339. PackPlatformBase newPlatformBase = m_PackPlatformMap[channelUniqueId];
  340. if (newPlatformBase != null)
  341. {
  342. if (m_PlatformBase != null && !m_PlatformBase.Equals(newPlatformBase))
  343. {
  344. if (SavePlatformSingleChange())
  345. {
  346. return;
  347. }
  348. }
  349. m_PlatformBase = newPlatformBase;
  350. if (m_PlatformBase != null)
  351. m_PlatformBaseChange = (PackPlatformBase)m_PlatformBase.Clone();
  352. return;
  353. }
  354. }
  355. }
  356. ClearPlatformSingleChange();
  357. }
  358. private void ClearPlatformSingleChange()
  359. {
  360. m_PlatformBase = null;
  361. m_PlatformBaseChange = null;
  362. }
  363. private bool SavePlatformSingleChange(bool remind = true)
  364. {
  365. if (m_PlatformBase != null && m_PlatformBaseChange != null)
  366. {
  367. if (!m_PlatformBase.Equals(m_PlatformBaseChange))
  368. {
  369. if (remind)
  370. {
  371. int overlay = (EditorUtility.DisplayDialogComplex("提示", "是否保存当前修改", "是", "再看看", "否"));
  372. if (overlay == 1)
  373. {
  374. return true;
  375. }
  376. if (overlay == 2)
  377. {
  378. m_PlatformBaseChange = (PackPlatformBase)m_PlatformBase.Clone();
  379. return false;
  380. }
  381. }
  382. string channelUniqueId = m_PlatformBase.channelUniqueId;
  383. string newChannelUniqueId = m_PlatformBaseChange.channelUniqueId;
  384. string channelUniqueName = m_PlatformBase.GetChannelUniqueName();
  385. string newChannelUniqueName = m_PlatformBaseChange.GetChannelUniqueName();
  386. if (channelUniqueName != newChannelUniqueName && ArrayUtility.Contains(m_PackPlatformNames, newChannelUniqueName))
  387. {
  388. if (EditorUtility.DisplayDialog("提示", string.Format("名字重复 : {0}, 继续修改?", newChannelUniqueName), "修改", "不保存"))
  389. {
  390. return true;
  391. }
  392. else
  393. {
  394. m_PlatformBaseChange = (PackPlatformBase)m_PlatformBase.Clone();
  395. return false;
  396. }
  397. }
  398. if (channelUniqueId != newChannelUniqueId && m_PackPlatformMap.ContainsKey(newChannelUniqueId))
  399. {
  400. if (EditorUtility.DisplayDialog("提示", string.Format("唯一名字(工具打包时使用名字) : {0}, 继续修改?", newChannelUniqueId), "修改", "不保存"))
  401. {
  402. return true;
  403. }
  404. else
  405. {
  406. m_PlatformBaseChange = (PackPlatformBase)m_PlatformBase.Clone();
  407. return false;
  408. }
  409. }
  410. EditorPrefs.SetString(SELECT_PLAT_SAVE_KEY, newChannelUniqueId);
  411. int index = ArrayUtility.FindIndex(m_PackPlatformUniqueIds, (x) => x == channelUniqueId);
  412. m_PackPlatformNames[index] = newChannelUniqueName;
  413. m_PackPlatformUniqueIds[index] = newChannelUniqueId;
  414. m_PackPlatformMap.Remove(channelUniqueId);
  415. m_PackPlatformMap.Add(newChannelUniqueId, m_PlatformBaseChange);
  416. SavePackPlatforms();
  417. m_PlatformBase = m_PlatformBaseChange;
  418. m_PlatformBaseChange = (PackPlatformBase)m_PlatformBaseChange.Clone();
  419. }
  420. }
  421. return false;
  422. }
  423. }
  424. }