UIConfigEditor.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using UnityEngine;
  2. using UnityEditor;
  3. using FairyGUI;
  4. namespace FairyGUIEditor
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. [CustomEditor(typeof(UIConfig))]
  10. public class UIConfigEditor : Editor
  11. {
  12. string[] propertyToExclude;
  13. bool itemsFoldout;
  14. bool packagesFoldOut;
  15. int errorState;
  16. private const float kButtonWidth = 18f;
  17. void OnEnable()
  18. {
  19. propertyToExclude = new string[] { "m_Script", "Items", "PreloadPackages" };
  20. itemsFoldout = EditorPrefs.GetBool("itemsFoldOut");
  21. packagesFoldOut = EditorPrefs.GetBool("packagesFoldOut");
  22. errorState = 0;
  23. }
  24. public override void OnInspectorGUI()
  25. {
  26. serializedObject.Update();
  27. DrawPropertiesExcluding(serializedObject, propertyToExclude);
  28. UIConfig config = (UIConfig)target;
  29. EditorGUILayout.BeginHorizontal();
  30. EditorGUI.BeginChangeCheck();
  31. itemsFoldout = EditorGUILayout.Foldout(itemsFoldout, "Config Items");
  32. if (EditorGUI.EndChangeCheck())
  33. EditorPrefs.SetBool("itemsFoldOut", itemsFoldout);
  34. EditorGUILayout.EndHorizontal();
  35. bool modified = false;
  36. if (itemsFoldout)
  37. {
  38. Undo.RecordObject(config, "Items");
  39. int len = config.Items.Count;
  40. EditorGUILayout.BeginHorizontal();
  41. EditorGUILayout.PrefixLabel("Add");
  42. UIConfig.ConfigKey selectedKey = (UIConfig.ConfigKey)EditorGUILayout.EnumPopup((System.Enum)UIConfig.ConfigKey.PleaseSelect);
  43. if (selectedKey != UIConfig.ConfigKey.PleaseSelect)
  44. {
  45. int index = (int)selectedKey;
  46. if (index > len - 1)
  47. {
  48. for (int i = len; i < index; i++)
  49. config.Items.Add(new UIConfig.ConfigValue());
  50. UIConfig.ConfigValue value = new UIConfig.ConfigValue();
  51. value.valid = true;
  52. UIConfig.SetDefaultValue(selectedKey, value);
  53. config.Items.Add(value);
  54. }
  55. else
  56. {
  57. UIConfig.ConfigValue value = config.Items[index];
  58. if (value == null)
  59. {
  60. value = new UIConfig.ConfigValue();
  61. value.valid = true;
  62. UIConfig.SetDefaultValue(selectedKey, value);
  63. config.Items[index] = value;
  64. }
  65. else if (!value.valid)
  66. {
  67. value.valid = true;
  68. UIConfig.SetDefaultValue(selectedKey, value);
  69. }
  70. }
  71. }
  72. EditorGUILayout.EndHorizontal();
  73. for (int i = 0; i < len; i++)
  74. {
  75. UIConfig.ConfigValue value = config.Items[i];
  76. if (value == null || !value.valid)
  77. continue;
  78. EditorGUILayout.BeginHorizontal();
  79. EditorGUILayout.PrefixLabel(((UIConfig.ConfigKey)i).ToString());
  80. switch ((UIConfig.ConfigKey)i)
  81. {
  82. case UIConfig.ConfigKey.ClickDragSensitivity:
  83. case UIConfig.ConfigKey.DefaultComboBoxVisibleItemCount:
  84. case UIConfig.ConfigKey.DefaultScrollStep:
  85. case UIConfig.ConfigKey.TouchDragSensitivity:
  86. case UIConfig.ConfigKey.TouchScrollSensitivity:
  87. case UIConfig.ConfigKey.InputCaretSize:
  88. value.i = EditorGUILayout.IntField(value.i);
  89. break;
  90. case UIConfig.ConfigKey.ButtonSound:
  91. case UIConfig.ConfigKey.GlobalModalWaiting:
  92. case UIConfig.ConfigKey.HorizontalScrollBar:
  93. case UIConfig.ConfigKey.LoaderErrorSign:
  94. case UIConfig.ConfigKey.PopupMenu:
  95. case UIConfig.ConfigKey.PopupMenu_seperator:
  96. case UIConfig.ConfigKey.TooltipsWin:
  97. case UIConfig.ConfigKey.VerticalScrollBar:
  98. case UIConfig.ConfigKey.WindowModalWaiting:
  99. case UIConfig.ConfigKey.DefaultFont:
  100. value.s = EditorGUILayout.TextField(value.s);
  101. break;
  102. case UIConfig.ConfigKey.DefaultScrollBounceEffect:
  103. case UIConfig.ConfigKey.DefaultScrollTouchEffect:
  104. case UIConfig.ConfigKey.RenderingTextBrighterOnDesktop:
  105. case UIConfig.ConfigKey.AllowSoftnessOnTopOrLeftSide:
  106. case UIConfig.ConfigKey.DepthSupportForPaintingMode:
  107. value.b = EditorGUILayout.Toggle(value.b);
  108. break;
  109. case UIConfig.ConfigKey.ButtonSoundVolumeScale:
  110. value.f = EditorGUILayout.Slider(value.f, 0, 1);
  111. break;
  112. case UIConfig.ConfigKey.ModalLayerColor:
  113. case UIConfig.ConfigKey.InputHighlightColor:
  114. value.c = EditorGUILayout.ColorField(value.c);
  115. break;
  116. case UIConfig.ConfigKey.Branch:
  117. EditorGUI.BeginChangeCheck();
  118. value.s = EditorGUILayout.TextField(value.s);
  119. if (EditorGUI.EndChangeCheck())
  120. modified = true;
  121. break;
  122. }
  123. if (GUILayout.Button(new GUIContent("X", "Delete Item"), EditorStyles.miniButtonRight, GUILayout.Width(30)))
  124. {
  125. config.Items[i].Reset();
  126. UIConfig.SetDefaultValue((UIConfig.ConfigKey)i, config.Items[i]);
  127. modified = true;
  128. }
  129. EditorGUILayout.EndHorizontal();
  130. }
  131. }
  132. EditorGUILayout.BeginHorizontal();
  133. EditorGUI.BeginChangeCheck();
  134. packagesFoldOut = EditorGUILayout.Foldout(packagesFoldOut, "Preload Packages");
  135. if (EditorGUI.EndChangeCheck())
  136. EditorPrefs.SetBool("packagesFoldOut", packagesFoldOut);
  137. EditorGUILayout.EndHorizontal();
  138. if (packagesFoldOut)
  139. {
  140. Undo.RecordObject(config, "PreloadPackages");
  141. EditorToolSet.LoadPackages();
  142. if (EditorToolSet.packagesPopupContents != null)
  143. {
  144. EditorGUILayout.BeginHorizontal();
  145. EditorGUILayout.PrefixLabel("Add");
  146. int selected = EditorGUILayout.Popup(EditorToolSet.packagesPopupContents.Length - 1, EditorToolSet.packagesPopupContents);
  147. EditorGUILayout.EndHorizontal();
  148. if (selected != EditorToolSet.packagesPopupContents.Length - 1)
  149. {
  150. UIPackage pkg = UIPackage.GetPackages()[selected];
  151. string tmp = pkg.assetPath.ToLower();
  152. int pos = tmp.LastIndexOf("resources/");
  153. if (pos != -1)
  154. {
  155. string packagePath = pkg.assetPath.Substring(pos + 10);
  156. if (config.PreloadPackages.IndexOf(packagePath) == -1)
  157. config.PreloadPackages.Add(packagePath);
  158. errorState = 0;
  159. }
  160. else
  161. {
  162. errorState = 10;
  163. }
  164. }
  165. }
  166. if (errorState > 0)
  167. {
  168. errorState--;
  169. EditorGUILayout.HelpBox("Package is not in resources folder.", MessageType.Warning);
  170. }
  171. int cnt = config.PreloadPackages.Count;
  172. int pi = 0;
  173. while (pi < cnt)
  174. {
  175. EditorGUILayout.BeginHorizontal();
  176. EditorGUILayout.PrefixLabel("" + pi + ".");
  177. config.PreloadPackages[pi] = EditorGUILayout.TextField(config.PreloadPackages[pi]);
  178. if (GUILayout.Button(new GUIContent("X", "Delete Item"), EditorStyles.miniButtonRight, GUILayout.Width(30)))
  179. {
  180. config.PreloadPackages.RemoveAt(pi);
  181. cnt--;
  182. }
  183. else
  184. pi++;
  185. EditorGUILayout.EndHorizontal();
  186. }
  187. }
  188. else
  189. errorState = 0;
  190. if (serializedObject.ApplyModifiedProperties() || modified)
  191. (target as UIConfig).ApplyModifiedProperties();
  192. }
  193. }
  194. }