PackWindow.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace Pack
  8. {
  9. public partial class PackWindow : EditorWindow
  10. {
  11. [MenuItem("AssetBundle/打包工具")]
  12. public static PackWindow OpenPackWindow()
  13. {
  14. var window = EditorWindow.GetWindow<PackWindow>(true, "App工具", true);
  15. window.minSize = new Vector2(800, 500);
  16. Rect position = window.position;
  17. float x = Mathf.Clamp(position.x, 32, Screen.currentResolution.width - 32);
  18. if (!Mathf.Approximately(position.x, x))
  19. position.x = Mathf.Clamp((Screen.currentResolution.width - position.width) * 0.5f, 32, Screen.currentResolution.width - 32);
  20. float y = Mathf.Clamp(position.y, 32, Screen.currentResolution.height - 32);
  21. if (!Mathf.Approximately(position.y, y))
  22. position.y = Mathf.Clamp((Screen.currentResolution.height - position.height) * 0.5f, 32, Screen.currentResolution.height - 32);
  23. window.position = position;
  24. return window;
  25. }
  26. private const string SELECT_PLAT_SAVE_KEY = "SELECT_PLAT_SAVE_KEY";
  27. public enum PackToolFlags
  28. {
  29. Build = 1 << 0,
  30. ChangeVersion = 1 << 1,
  31. }
  32. private Dictionary<string, PackPlatformBase> m_PackPlatformMap;
  33. private string[] m_PackPlatformNames;
  34. private string[] m_PackPlatformUniqueIds;
  35. private PackToolFlags m_PackToolFlags = PackToolFlags.Build;
  36. private void OnEnable()
  37. {
  38. ResetDefaultPackPlatforms();
  39. }
  40. private void OnLostFocus()
  41. {
  42. var methodInfo = typeof(EditorGUI).GetMethod("EndEditingActiveTextField", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
  43. methodInfo.Invoke(null, null);
  44. }
  45. private void OnDisable()
  46. {
  47. OnLostFocus();
  48. }
  49. private void OnGUI()
  50. {
  51. EditorGUILayout.BeginVertical();
  52. EditorGUILayout.BeginHorizontal(PackGUI.styles.toolbar);
  53. if (GUILayout.Toggle((m_PackToolFlags == PackToolFlags.Build), "打包", PackGUI.styles.toolbarButton))
  54. {
  55. if (m_PackToolFlags != PackToolFlags.Build)
  56. {
  57. if (!SavePlatformSingleChange() && !SavePlatformVersionCodeChange())
  58. {
  59. ClearPlatformSingleChange();
  60. ClearSavePlatformVersionCodeChange();
  61. m_PackToolFlags = PackToolFlags.Build;
  62. m_ScrollPosition = Vector2.zero;
  63. }
  64. }
  65. }
  66. if (GUILayout.Toggle((m_PackToolFlags == PackToolFlags.ChangeVersion), "快速修改版本号", PackGUI.styles.toolbarButton))
  67. {
  68. if (m_PackToolFlags != PackToolFlags.ChangeVersion)
  69. {
  70. if (!SavePlatformSingleChange() && !SavePlatformVersionCodeChange())
  71. {
  72. ClearPlatformSingleChange();
  73. ClearSavePlatformVersionCodeChange();
  74. m_PackToolFlags = PackToolFlags.ChangeVersion;
  75. m_ScrollPosition = Vector2.zero;
  76. }
  77. }
  78. }
  79. GUILayout.FlexibleSpace();
  80. if (GUILayout.Button("刷新当前配置", PackGUI.styles.toolbarButton))
  81. {
  82. if (!SavePlatformSingleChange() && !SavePlatformVersionCodeChange())
  83. {
  84. ClearPlatformSingleChange();
  85. ClearSavePlatformVersionCodeChange();
  86. ResetDefaultPackPlatforms();
  87. }
  88. }
  89. if (GUILayout.Button("打开配置文件", PackGUI.styles.toolbarButton))
  90. {
  91. EditorUtility.OpenWithDefaultApp(Path.GetFullPath(Application.dataPath + PackConstant.PackPlatformConfigPath));
  92. }
  93. EditorGUILayout.EndHorizontal();
  94. EditorGUILayout.Space();
  95. if (m_PackToolFlags == PackToolFlags.Build)
  96. {
  97. DrawPackGUI();
  98. }
  99. else if (m_PackToolFlags == PackToolFlags.ChangeVersion)
  100. {
  101. DrawChangeVersionGUI();
  102. }
  103. EditorGUILayout.EndVertical();
  104. }
  105. private void ResetDefaultPackPlatforms()
  106. {
  107. m_PackPlatformNames = null;
  108. m_PackPlatformMap = null;
  109. m_PackPlatformUniqueIds = null;
  110. m_SelectPackPlatformNameIdx = -1;
  111. try
  112. {
  113. PackPlatforms packPlatforms = PackRun.GetConfigs(true);
  114. if (packPlatforms == null) return;
  115. List<string> packPlatformNameLs = new List<string>();
  116. List<string> packPlatformUniqueIdLs = new List<string>();
  117. m_PackPlatformMap = new Dictionary<string, PackPlatformBase>();
  118. if (packPlatforms != null && packPlatforms.packPlatforms != null)
  119. {
  120. PackPlatformBase[] packPlatformBases = packPlatforms.packPlatforms;
  121. Array.Sort(packPlatformBases,(x, y) =>
  122. {
  123. return StringComparer.InvariantCulture.Compare(x.channelUniqueId, y.channelUniqueId);
  124. });
  125. for (int i = 0, iMax = packPlatformBases.Length; i < iMax; i++)
  126. {
  127. PackPlatformBase packPlatform = (PackPlatformBase)packPlatformBases[i].Clone();
  128. string channelUniqueName = packPlatform.GetChannelUniqueName();
  129. string channelUniqueId = packPlatform.channelUniqueId;
  130. packPlatformNameLs.Add(channelUniqueName);
  131. packPlatformUniqueIdLs.Add(channelUniqueId);
  132. if (m_PackPlatformMap.ContainsKey(channelUniqueId))
  133. {
  134. throw new Exception("存在相同的打包数据, 请检测文件内容" + PackConstant.PackPlatformConfigPath);
  135. }
  136. else
  137. {
  138. m_PackPlatformMap.Add(channelUniqueId, packPlatform);
  139. }
  140. }
  141. }
  142. m_PackPlatformNames = packPlatformNameLs.ToArray();
  143. m_PackPlatformUniqueIds = packPlatformUniqueIdLs.ToArray();
  144. string selectChannelUniqueId = EditorPrefs.GetString(SELECT_PLAT_SAVE_KEY);
  145. m_SelectPackPlatformNameIdx = ArrayUtility.FindIndex(m_PackPlatformUniqueIds, (x) => x == selectChannelUniqueId);
  146. }
  147. catch (Exception e)
  148. {
  149. Debug.LogException(e);
  150. m_PackPlatformNames = null;
  151. m_PackPlatformMap = null;
  152. m_PackPlatformUniqueIds = null;
  153. }
  154. }
  155. private void SavePackPlatforms()
  156. {
  157. if (m_PackPlatformUniqueIds == null || m_PackPlatformMap == null) return;
  158. List<PackPlatformBase> packPlatformLs = new List<PackPlatformBase>();
  159. for (int i = 0, iMax = m_PackPlatformUniqueIds.Length; i < iMax; i++)
  160. {
  161. if (m_PackPlatformMap.ContainsKey(m_PackPlatformUniqueIds[i]))
  162. {
  163. packPlatformLs.Add(m_PackPlatformMap[m_PackPlatformUniqueIds[i]]);
  164. }
  165. }
  166. packPlatformLs.Sort((x, y) =>
  167. {
  168. return StringComparer.InvariantCulture.Compare(x.channelUniqueId, y.channelUniqueId);
  169. });
  170. PackPlatforms packPlatforms = new PackPlatforms();
  171. packPlatforms.packPlatforms = packPlatformLs.ToArray();
  172. PackRun.SaveConfigs(packPlatforms);
  173. }
  174. }
  175. }