PackGUI.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 class PackGUI
  10. {
  11. public const float kSingleLineHeight = 24f;
  12. public const float kIndentPerLevel = 15;
  13. public static string kIntFieldFormatString = "#######0";
  14. public static float indent => EditorGUI.indentLevel * kIndentPerLevel;
  15. public static readonly int s_MutliSelectField = "MutliSelectField".GetHashCode();
  16. public static readonly int s_DelayedTextFieldHash = "DelayedEditorTextField".GetHashCode();
  17. public static readonly int s_ButtonGridHash = "ButtonGrid".GetHashCode();
  18. public static readonly int s_SliderHash = "EditorSlider".GetHashCode();
  19. private static Stack<Color> s_ChangeGUIBgColorStack = new Stack<Color>();
  20. public static void BeginChangeGUIBgColor(Color color)
  21. {
  22. s_ChangeGUIBgColorStack.Push(GUI.backgroundColor);
  23. GUI.backgroundColor = color;
  24. }
  25. public static void EndChangeGUIBgColor()
  26. {
  27. if (s_ChangeGUIBgColorStack.Count > 0)
  28. GUI.backgroundColor = s_ChangeGUIBgColorStack.Pop();
  29. }
  30. public static void EndAllChangeGUIBgColor()
  31. {
  32. while (s_ChangeGUIBgColorStack.Count > 0)
  33. {
  34. GUI.backgroundColor = s_ChangeGUIBgColorStack.Pop();
  35. }
  36. }
  37. public static void BeginDiffValue()
  38. {
  39. PackGUI.BeginChangeGUIBgColor(Color.red);
  40. }
  41. public static void EndDiffValue()
  42. {
  43. PackGUI.EndChangeGUIBgColor();
  44. }
  45. private static Stack<Color> s_ChangeGUIColorStack = new Stack<Color>();
  46. public static void BeginChangeGUIColor(Color color)
  47. {
  48. s_ChangeGUIColorStack.Push(GUI.color);
  49. GUI.color = color;
  50. }
  51. public static void EndChangeGUIColor()
  52. {
  53. if (s_ChangeGUIColorStack.Count > 0)
  54. GUI.color = s_ChangeGUIColorStack.Pop();
  55. }
  56. public static void EndAllChangeGUIColor()
  57. {
  58. while (s_ChangeGUIColorStack.Count > 0)
  59. {
  60. GUI.color = s_ChangeGUIColorStack.Pop();
  61. }
  62. }
  63. private static Stack<bool> s_ChangeEnabledStack = new Stack<bool>();
  64. public static void BeginChangeEnabled(bool enabled)
  65. {
  66. s_ChangeEnabledStack.Push(GUI.enabled);
  67. GUI.enabled = enabled;
  68. }
  69. public static void EndChangeEnabled()
  70. {
  71. if (s_ChangeEnabledStack.Count > 0)
  72. GUI.enabled = s_ChangeEnabledStack.Pop();
  73. }
  74. public static void EndAllChangeEnabled()
  75. {
  76. while (s_ChangeEnabledStack.Count > 0)
  77. {
  78. GUI.enabled = s_ChangeEnabledStack.Pop();
  79. }
  80. }
  81. private static Stack<float> s_ChangeLabelWidthStack = new Stack<float>();
  82. public static void BeginChangeLabelWidth(float labelWidth)
  83. {
  84. s_ChangeLabelWidthStack.Push(EditorGUIUtility.labelWidth);
  85. EditorGUIUtility.labelWidth = labelWidth;
  86. }
  87. public static void EndChangeLabelWidth()
  88. {
  89. if (s_ChangeLabelWidthStack.Count > 0)
  90. EditorGUIUtility.labelWidth = s_ChangeLabelWidthStack.Pop();
  91. }
  92. public static void EndAllChangeLabelWidth()
  93. {
  94. while (s_ChangeLabelWidthStack.Count > 0)
  95. {
  96. EditorGUIUtility.labelWidth = s_ChangeLabelWidthStack.Pop();
  97. }
  98. }
  99. public static void DrawLabelField(string label, string content)
  100. {
  101. Rect totalPosition = EditorGUILayout.GetControlRect(true, PackGUI.kSingleLineHeight, PackGUI.styles.label);
  102. int id = GUIUtility.GetControlID(PackGUI.s_DelayedTextFieldHash, FocusType.Keyboard, totalPosition);
  103. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  104. Rect fieldPosition = new Rect(totalPosition.x + EditorGUIUtility.labelWidth, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth, totalPosition.height);
  105. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTempContent(label), id, PackGUI.styles.label);
  106. EditorGUI.HandlePrefixLabel(totalPosition, fieldPosition, EditorGUIUtility.TrTempContent(content), id, PackGUI.styles.label);
  107. }
  108. public static void DrawDelayedIntField(string label, string tooltip, string srcVal, ref string val, bool abs = false)
  109. {
  110. Rect totalPosition = EditorGUILayout.GetControlRect(true, PackGUI.kSingleLineHeight, PackGUI.styles.textField);
  111. int id = GUIUtility.GetControlID(PackGUI.s_DelayedTextFieldHash, FocusType.Keyboard, totalPosition);
  112. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  113. Rect fieldPosition = new Rect(totalPosition.x + EditorGUIUtility.labelWidth, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth - 100, totalPosition.height);
  114. Rect resetPosition = new Rect(totalPosition.x + totalPosition.width - 90, totalPosition.y, 80, totalPosition.height);
  115. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTextContent(label, tooltip), id, PackGUI.styles.label);
  116. bool changed = (srcVal != val);
  117. if (changed) BeginDiffValue();
  118. string temp = EditorGUI.DelayedTextField(fieldPosition, GUIContent.none, id, val, PackGUI.styles.textField);
  119. if (changed) EndDiffValue();
  120. int newVal;
  121. if (ExpressionEvaluator.Evaluate(temp, out newVal))
  122. {
  123. if (abs && newVal < 0) newVal = 0;
  124. val = newVal.ToString();
  125. }
  126. if (changed)
  127. {
  128. if (GUI.Button(resetPosition, EditorGUIUtility.TrTextContent("重置默认", srcVal), PackGUI.styles.miniButton))
  129. {
  130. val = srcVal;
  131. }
  132. }
  133. }
  134. public static void DrawDelayedIntField(string label, string tooltip, uint srcVal, ref uint val, bool abs = false)
  135. {
  136. Rect totalPosition = EditorGUILayout.GetControlRect(true, PackGUI.kSingleLineHeight, PackGUI.styles.textField);
  137. int id = GUIUtility.GetControlID(PackGUI.s_DelayedTextFieldHash, FocusType.Keyboard, totalPosition);
  138. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  139. Rect fieldPosition = new Rect(totalPosition.x + EditorGUIUtility.labelWidth, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth - 100, totalPosition.height);
  140. Rect resetPosition = new Rect(totalPosition.x + totalPosition.width - 90, totalPosition.y, 80, totalPosition.height);
  141. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTextContent(label, tooltip), id, PackGUI.styles.label);
  142. bool changed = (srcVal != val);
  143. if (changed) BeginDiffValue();
  144. string temp = EditorGUI.DelayedTextField(fieldPosition, GUIContent.none, id, val.ToString(), PackGUI.styles.textField);
  145. if (changed) EndDiffValue();
  146. int newVal;
  147. if (ExpressionEvaluator.Evaluate(temp, out newVal))
  148. {
  149. if (abs && newVal < 0) newVal = 0;
  150. val = (uint)newVal;
  151. }
  152. if (changed)
  153. {
  154. if (GUI.Button(resetPosition, EditorGUIUtility.TrTextContent("重置默认", srcVal.ToString()), PackGUI.styles.miniButton))
  155. {
  156. val = srcVal;
  157. }
  158. }
  159. }
  160. public static void DrawDelayedTextField(string label, string srcContent, ref string content, bool notNull = false)
  161. {
  162. DrawDelayedTextField(label, null, srcContent, ref content, notNull);
  163. }
  164. public static void DrawDelayedTextField(string label, string tooltip, string srcContent, ref string content, bool notNull = false)
  165. {
  166. Rect totalPosition = EditorGUILayout.GetControlRect(true, PackGUI.kSingleLineHeight, PackGUI.styles.textField);
  167. int id = GUIUtility.GetControlID(PackGUI.s_DelayedTextFieldHash, FocusType.Keyboard, totalPosition);
  168. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  169. Rect fieldPosition = new Rect(totalPosition.x + EditorGUIUtility.labelWidth, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth - 100, totalPosition.height);
  170. Rect resetPosition = new Rect(totalPosition.x + totalPosition.width - 90, totalPosition.y, 80, totalPosition.height);
  171. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTextContent(label, tooltip), id, PackGUI.styles.label);
  172. bool changed = (srcContent != content);
  173. if (changed) BeginDiffValue();
  174. string temp = EditorGUI.DelayedTextField(fieldPosition, GUIContent.none, id, content, PackGUI.styles.textField);
  175. if (changed) EndDiffValue();
  176. if (notNull)
  177. {
  178. if (!string.IsNullOrEmpty(temp))
  179. {
  180. content = temp;
  181. }
  182. }
  183. else
  184. {
  185. content = temp;
  186. }
  187. if (changed)
  188. {
  189. if (GUI.Button(resetPosition, EditorGUIUtility.TrTextContent("重置默认", srcContent), PackGUI.styles.miniButton))
  190. {
  191. content = srcContent;
  192. }
  193. }
  194. }
  195. public static void DrawIntSlider(string label, uint leftValue, uint rightValue, uint srcContent, ref uint content)
  196. {
  197. DrawIntSlider(label, null, leftValue, rightValue, srcContent, ref content);
  198. }
  199. public static void DrawIntSlider(string label, string tooltip, uint leftValue, uint rightValue, uint srcContent, ref uint content)
  200. {
  201. int contentTemp = (int)content;
  202. DrawIntSlider(label, tooltip, (int)leftValue, (int)rightValue, (int)srcContent, ref contentTemp);
  203. content = (uint)contentTemp;
  204. }
  205. public static void DrawIntSlider(string label, int leftValue, int rightValue, int srcContent, ref int content)
  206. {
  207. DrawIntSlider(label, null, leftValue, rightValue, srcContent, ref content);
  208. }
  209. public static void DrawIntSlider(string label, string tooltip, int leftValue, int rightValue, int srcContent, ref int content)
  210. {
  211. Rect totalPosition = EditorGUILayout.GetControlRect(true, PackGUI.kSingleLineHeight, PackGUI.styles.textField);
  212. int id = GUIUtility.GetControlID(PackGUI.s_SliderHash, FocusType.Keyboard, totalPosition);
  213. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  214. Rect fieldPosition = new Rect(totalPosition.x + EditorGUIUtility.labelWidth + PackGUI.indent, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth - 100 - PackGUI.indent, totalPosition.height);
  215. Rect resetPosition = new Rect(totalPosition.x + totalPosition.width - 90, totalPosition.y, 80, totalPosition.height);
  216. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTextContent(label, tooltip), id, PackGUI.styles.label);
  217. bool changed = (srcContent != content);
  218. if (changed) BeginDiffValue();
  219. Rect dragZoneRect = new Rect(fieldPosition.x, fieldPosition.y, fieldPosition.width - 100, fieldPosition.height);
  220. content = Mathf.RoundToInt((float)doSliderMethodInfo.Invoke(null, new object[]
  221. {
  222. fieldPosition, dragZoneRect, id, content, leftValue, rightValue,
  223. kIntFieldFormatString, leftValue, rightValue, 1f,
  224. PackGUI.styles.horizontalSlider, PackGUI.styles.horizontalSliderThumb, null
  225. }));
  226. if (changed) EndDiffValue();
  227. if (changed)
  228. {
  229. if (GUI.Button(resetPosition, EditorGUIUtility.TrTextContent("重置默认", srcContent.ToString()), PackGUI.styles.miniButton))
  230. {
  231. content = srcContent;
  232. }
  233. }
  234. }
  235. private static MethodInfo s_DoSliderMethodInfo = null;
  236. private static MethodInfo doSliderMethodInfo
  237. {
  238. get
  239. {
  240. if (s_DoSliderMethodInfo == null)
  241. {
  242. BindingFlags bindingFlags = (BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  243. s_DoSliderMethodInfo = typeof(EditorGUI).GetMethod("DoSlider", bindingFlags, null,
  244. new Type[] {
  245. typeof(Rect), typeof(Rect), typeof(int),
  246. typeof(float), typeof(float), typeof(float),
  247. typeof(string),
  248. typeof(float), typeof(float), typeof(float),
  249. typeof(GUIStyle), typeof(GUIStyle), typeof(Texture2D)
  250. }, null);
  251. }
  252. return s_DoSliderMethodInfo;
  253. }
  254. }
  255. public static void DrawMutliSelectField<T>(string label, T[] canSelectItems, T[] srcSelectedItems, ref T[] selectedItems, T[] noEnableValues, int maxSelectNum = -1, bool isSort = true)
  256. {
  257. DrawMutliSelectField<T>(label, null, canSelectItems, srcSelectedItems, ref selectedItems, noEnableValues, maxSelectNum, isSort);
  258. }
  259. public static void DrawMutliSelectField<T>(string label, string tooltip, T[] canSelectItems, T[] srcSelectedItems, ref T[] selectedItems, T[] noEnableValues, int maxSelectNum = -1, bool isSort = true)
  260. {
  261. Rect totalPosition = EditorGUILayout.GetControlRect(false, PackGUI.kSingleLineHeight, PackGUI.styles.popup);
  262. var id = GUIUtility.GetControlID(PackGUI.s_MutliSelectField, FocusType.Keyboard, totalPosition);
  263. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  264. Rect fieldPosition = new Rect(totalPosition.x + PackGUI.indent + EditorGUIUtility.labelWidth, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth - 100 - PackGUI.indent, totalPosition.height);
  265. Rect resetPosition = new Rect(totalPosition.x + totalPosition.width - 90, totalPosition.y, 80, totalPosition.height);
  266. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTextContent(label, tooltip), id, PackGUI.styles.label);
  267. bool changed = !ArrayUtility.ArrayEquals(srcSelectedItems, selectedItems);
  268. if (changed) BeginDiffValue();
  269. selectedItems = MutliSelectFieldGUI.DoMutliSelectFieldShowValue(fieldPosition, id, canSelectItems, selectedItems, noEnableValues, maxSelectNum, isSort, PackGUI.styles.popup);
  270. if (changed) EndDiffValue();
  271. if (changed)
  272. {
  273. if (GUI.Button(resetPosition, EditorGUIUtility.TrTextContent("重置默认", string.Join(";", srcSelectedItems)), PackGUI.styles.miniButton))
  274. {
  275. ArrayUtility.Clear(ref selectedItems);
  276. ArrayUtility.AddRange(ref selectedItems, srcSelectedItems);
  277. }
  278. }
  279. }
  280. public static void DrawVersionCodeField(string label, bool shortFormat, VersionCode srcVersionCode, ref VersionCode versionCode)
  281. {
  282. Rect totalPosition = EditorGUILayout.GetControlRect(false, PackGUI.kSingleLineHeight, PackGUI.styles.popup);
  283. totalPosition.height = PackGUI.kSingleLineHeight;
  284. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  285. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTempContent(label + "(" + versionCode.ToString() + ")"), 0, PackGUI.styles.label);
  286. Rect fieldPosition = new Rect(totalPosition.x + PackGUI.indent + EditorGUIUtility.labelWidth, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth - 100 - PackGUI.indent, totalPosition.height);
  287. Rect offsetPosition = fieldPosition;
  288. offsetPosition.width = (fieldPosition.width - 30) * 0.25f;
  289. DrawUintField(offsetPosition, 0, "major", srcVersionCode.major, ref versionCode.major);
  290. DrawUintField(offsetPosition, 1, "minor", srcVersionCode.minor, ref versionCode.minor);
  291. DrawUintField(offsetPosition, 2, "release", srcVersionCode.release, ref versionCode.release);
  292. if (!shortFormat) DrawUintField(offsetPosition, 3, "patch", srcVersionCode.patch, ref versionCode.patch);
  293. Rect resetPosition = new Rect(totalPosition.x + totalPosition.width - 90, totalPosition.y, 80, totalPosition.height);
  294. if (srcVersionCode != versionCode)
  295. {
  296. if (GUI.Button(resetPosition, EditorGUIUtility.TrTextContent("重置默认", srcVersionCode.ToString(shortFormat)), PackGUI.styles.miniButton))
  297. {
  298. versionCode = srcVersionCode;
  299. }
  300. }
  301. }
  302. public static void DrawUintField(Rect position, int offsetLine, string label, uint srcValue, ref uint value)
  303. {
  304. position.x = position.x + (position.width + 10) * offsetLine;
  305. Vector2 size = PackGUI.styles.label.CalcSize(EditorGUIUtility.TrTempContent(label));
  306. float labelWidth = size.x;
  307. Rect labelPosition = new Rect(position.x, position.y, labelWidth, PackGUI.kSingleLineHeight);
  308. Rect fieldPosition = new Rect(position.x + labelWidth, position.y, position.width - labelWidth, position.height);
  309. var id = GUIUtility.GetControlID(PackGUI.s_MutliSelectField, FocusType.Keyboard, position);
  310. EditorGUI.HandlePrefixLabel(position, labelPosition, EditorGUIUtility.TrTempContent(label), id, PackGUI.styles.label);
  311. bool changed = srcValue != value;
  312. if (changed) PackGUI.BeginDiffValue();
  313. int intVal = EditorGUI.IntField(fieldPosition, (int)value, PackGUI.styles.numberField);
  314. if (intVal < 0) intVal = 0;
  315. value = (uint)intVal;
  316. if (changed) PackGUI.EndDiffValue();
  317. }
  318. public static void DrawSelectFolderField(string label, string rootPath, string srcPath, ref string curPath)
  319. {
  320. Rect totalPosition = EditorGUILayout.GetControlRect(true, PackGUI.kSingleLineHeight, PackGUI.styles.textField);
  321. int id = GUIUtility.GetControlID(PackGUI.s_DelayedTextFieldHash, FocusType.Keyboard, totalPosition);
  322. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  323. Rect fieldPosition = new Rect(totalPosition.x + PackGUI.indent + EditorGUIUtility.labelWidth, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth - 220 - PackGUI.indent, totalPosition.height);
  324. Rect openPosition = new Rect(totalPosition.x + totalPosition.width - 210, totalPosition.y, 110, totalPosition.height);
  325. Rect resetPosition = new Rect(totalPosition.x + totalPosition.width - 90, totalPosition.y, 80, totalPosition.height);
  326. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTempContent(label), id, PackGUI.styles.label);
  327. bool changed = srcPath != curPath;
  328. if (changed) BeginDiffValue();
  329. if (GUI.Button(fieldPosition, curPath, PackGUI.styles.miniButton))
  330. {
  331. rootPath = Path.GetFullPath(rootPath);
  332. rootPath = rootPath.Replace('\\', '/');
  333. if (!Directory.Exists(rootPath))
  334. {
  335. Directory.CreateDirectory(rootPath);
  336. }
  337. string curAbsPath = Path.Combine(rootPath, curPath);
  338. curAbsPath = Path.GetFullPath(rootPath);
  339. curAbsPath = curAbsPath.Replace('\\', '/');
  340. if (!Directory.Exists(curAbsPath))
  341. {
  342. Directory.CreateDirectory(curAbsPath);
  343. }
  344. string tempPath = EditorUtility.OpenFolderPanel("选择一个文件夹", rootPath, curPath);
  345. if (!string.IsNullOrEmpty(tempPath))
  346. {
  347. if (tempPath.StartsWith(rootPath))
  348. {
  349. curPath = tempPath.Substring(rootPath.Length, tempPath.Length - rootPath.Length);
  350. }
  351. }
  352. }
  353. if (changed) EndDiffValue();
  354. PackGUI.BeginChangeEnabled(true);
  355. if (GUI.Button(openPosition, EditorGUIUtility.TrTextContent("打开文件夹"), PackGUI.styles.miniButton))
  356. {
  357. string curAbsPath = Path.Combine(rootPath, curPath);
  358. EditorUtility.OpenWithDefaultApp(curAbsPath);
  359. }
  360. PackGUI.EndChangeEnabled();
  361. if (changed)
  362. {
  363. if (GUI.Button(resetPosition, EditorGUIUtility.TrTextContent("重置默认", srcPath), PackGUI.styles.miniButton))
  364. {
  365. curPath = srcPath;
  366. }
  367. }
  368. }
  369. public static void DrawSelectFileFieldWithFilters(string label, string directory, string[] filters, string srcFileName, ref string curFileName)
  370. {
  371. Rect totalPosition = EditorGUILayout.GetControlRect(true, PackGUI.kSingleLineHeight, PackGUI.styles.textField);
  372. int id = GUIUtility.GetControlID(PackGUI.s_DelayedTextFieldHash, FocusType.Keyboard, totalPosition);
  373. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  374. Rect fieldPosition = new Rect(totalPosition.x + PackGUI.indent + EditorGUIUtility.labelWidth, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth - 220 - PackGUI.indent, totalPosition.height);
  375. Rect openPosition = new Rect(totalPosition.x + totalPosition.width - 210, totalPosition.y, 110, totalPosition.height);
  376. Rect resetPosition = new Rect(totalPosition.x + totalPosition.width - 90, totalPosition.y, 80, totalPosition.height);
  377. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTempContent(label), id, PackGUI.styles.label);
  378. bool changed = srcFileName != curFileName;
  379. if (changed) PackGUI.BeginDiffValue();
  380. if (GUI.Button(fieldPosition, curFileName, PackGUI.styles.miniButton))
  381. {
  382. directory = Path.GetFullPath(directory);
  383. directory = directory.Replace('\\', '/');
  384. if (!Directory.Exists(directory))
  385. {
  386. Directory.CreateDirectory(directory);
  387. }
  388. string tempPath = EditorUtility.OpenFilePanelWithFilters("选择一个文件", directory, filters);
  389. if (!string.IsNullOrEmpty(tempPath))
  390. {
  391. if (tempPath.StartsWith(directory))
  392. {
  393. curFileName = tempPath.Substring(directory.Length, tempPath.Length - directory.Length);
  394. }
  395. }
  396. }
  397. if (changed) PackGUI.EndDiffValue();
  398. PackGUI.BeginChangeEnabled(true);
  399. if (GUI.Button(openPosition, EditorGUIUtility.TrTextContent("打开文件"), PackGUI.styles.miniButton))
  400. {
  401. string curAbsPath = Path.Combine(directory, curFileName);
  402. EditorUtility.OpenWithDefaultApp(curAbsPath);
  403. }
  404. PackGUI.EndChangeEnabled();
  405. if (changed)
  406. {
  407. if (GUI.Button(resetPosition, EditorGUIUtility.TrTextContent("重置默认", srcFileName), PackGUI.styles.miniButton))
  408. {
  409. curFileName = srcFileName;
  410. }
  411. }
  412. }
  413. public static void DrawToggleField(string label, bool srcContent, ref bool content)
  414. {
  415. DrawToggleField(label, null, srcContent, ref content);
  416. }
  417. public static void DrawToggleField(string label, string tooltip, bool srcContent, ref bool content)
  418. {
  419. Rect totalPosition = EditorGUILayout.GetControlRect(true, PackGUI.kSingleLineHeight, PackGUI.styles.textField);
  420. int id = GUIUtility.GetControlID(PackGUI.s_DelayedTextFieldHash, FocusType.Keyboard, totalPosition);
  421. Rect labelPosition = new Rect(totalPosition.x + PackGUI.indent, totalPosition.y, EditorGUIUtility.labelWidth - PackGUI.indent, PackGUI.kSingleLineHeight);
  422. Rect fieldPosition = new Rect(totalPosition.x + EditorGUIUtility.labelWidth, totalPosition.y, totalPosition.width - EditorGUIUtility.labelWidth - 100, totalPosition.height);
  423. Rect resetPosition = new Rect(totalPosition.x + totalPosition.width - 90, totalPosition.y, 80, totalPosition.height);
  424. EditorGUI.HandlePrefixLabel(totalPosition, labelPosition, EditorGUIUtility.TrTextContent(label, tooltip), id, PackGUI.styles.label);
  425. bool changed = (srcContent != content);
  426. if (changed) BeginDiffValue();
  427. content = EditorGUI.Toggle(fieldPosition, content, PackGUI.styles.toggle);
  428. if (changed) EndDiffValue();
  429. if (changed)
  430. {
  431. if (GUI.Button(resetPosition, EditorGUIUtility.TrTextContent("重置默认", srcContent.ToString()), PackGUI.styles.miniButton))
  432. {
  433. content = srcContent;
  434. }
  435. }
  436. }
  437. private static Styles s_Styles;
  438. public static Styles styles
  439. {
  440. get
  441. {
  442. if (s_Styles == null)
  443. {
  444. s_Styles = new Styles();
  445. }
  446. return s_Styles;
  447. }
  448. }
  449. public class Styles
  450. {
  451. public GUIStyle label;
  452. public GUIStyle miniLabel;
  453. public GUIStyle largeLabel;
  454. public GUIStyle boldLabel;
  455. public GUIStyle miniBoldLabel;
  456. public GUIStyle textField;
  457. public GUIStyle miniButton;
  458. public GUIStyle popup;
  459. public GUIStyle toolbar;
  460. public GUIStyle toolbarButton;
  461. public GUIStyle miniButtonLeft;
  462. public GUIStyle miniButtonMid;
  463. public GUIStyle miniButtonRight;
  464. public GUIStyle helpBox;
  465. public GUIStyle numberField;
  466. public GUIStyle box;
  467. public GUIStyle toggle;
  468. public GUIStyle horizontalSlider;
  469. public GUIStyle horizontalSliderThumb;
  470. public GUIStyle reorderableListHeadBg;
  471. public Styles()
  472. {
  473. Font font = Font.CreateDynamicFontFromOSFont(new string[] { "Fira Code Retina", "华文楷体", }, 16);
  474. label = UnifyGUIStyle(EditorStyles.label, font);
  475. miniLabel = UnifyGUIStyle(EditorStyles.miniLabel, font);
  476. largeLabel = UnifyGUIStyle(EditorStyles.largeLabel, font);
  477. boldLabel = UnifyGUIStyle(EditorStyles.boldLabel, font);
  478. boldLabel.fontStyle = FontStyle.Bold;
  479. miniBoldLabel = UnifyGUIStyle(EditorStyles.miniBoldLabel, font);
  480. miniBoldLabel.fontStyle = FontStyle.Bold;
  481. textField = UnifyGUIStyle(EditorStyles.textField, font);
  482. miniButton = UnifyGUIStyle(EditorStyles.miniButton, font);
  483. popup = UnifyGUIStyle(EditorStyles.popup, font);
  484. toolbar = UnifyGUIStyle(EditorStyles.toolbar, font);
  485. toolbarButton = UnifyGUIStyle(EditorStyles.toolbarButton, font);
  486. miniButtonLeft = UnifyGUIStyle(EditorStyles.miniButtonLeft, font);
  487. miniButtonMid = UnifyGUIStyle(EditorStyles.miniButtonMid, font);
  488. miniButtonRight = UnifyGUIStyle(EditorStyles.miniButtonRight, font);
  489. helpBox = UnifyGUIStyle(EditorStyles.helpBox, font);
  490. helpBox.fixedHeight = 42;
  491. numberField = UnifyGUIStyle(EditorStyles.numberField, font);
  492. box = new GUIStyle(GUI.skin.box);
  493. toggle = new GUIStyle(EditorStyles.toggle);
  494. horizontalSlider = new GUIStyle(GUI.skin.horizontalSlider);
  495. // horizontalSlider.fixedHeight = 14;
  496. horizontalSliderThumb = new GUIStyle(GUI.skin.horizontalSliderThumb);
  497. // horizontalSliderThumb.fixedWidth = 14;
  498. // horizontalSliderThumb.fixedHeight = 14;
  499. reorderableListHeadBg = new GUIStyle("RL Header");
  500. reorderableListHeadBg.fixedHeight = 28;
  501. reorderableListHeadBg.fontSize = 16;
  502. if (font) reorderableListHeadBg.font = font;
  503. }
  504. private GUIStyle UnifyGUIStyle(GUIStyle srcGUIStyle, Font font)
  505. {
  506. GUIStyle gUIStyle = new GUIStyle(srcGUIStyle);
  507. gUIStyle.fixedHeight = 22;
  508. gUIStyle.fontSize = 16;
  509. if (font) gUIStyle.font = font;
  510. return gUIStyle;
  511. }
  512. }
  513. }
  514. }