UIStyleManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. public class UIStyleManager
  6. {
  7. Dictionary<string, UIStyleInfo> m_styleData ;
  8. public void OnEnable()
  9. {
  10. m_styleData = UIStyleConfigManager.GetData();
  11. }
  12. public void GUI()
  13. {
  14. UIStyleList();
  15. CreateUIStyleGUI();
  16. ButtonsGUI();
  17. DeleteLogic();
  18. }
  19. bool isFoldCreateUIStyle = false;
  20. string m_CreateStyleName = "";
  21. void CreateUIStyleGUI()
  22. {
  23. EditorGUI.indentLevel = 1;
  24. isFoldCreateUIStyle = EditorGUILayout.Foldout(isFoldCreateUIStyle, "创建Style:");
  25. if (isFoldCreateUIStyle)
  26. {
  27. EditorGUI.indentLevel = 2;
  28. m_CreateStyleName = EditorGUILayout.TextField("Style名称:",m_CreateStyleName);
  29. if (m_CreateStyleName != "")
  30. {
  31. EditorGUILayout.BeginHorizontal();
  32. EditorGUILayout.Space();
  33. if (UIStyleConfigManager.GetData(m_CreateStyleName)== null)
  34. {
  35. if (GUILayout.Button("创建新UIStyle", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  36. {
  37. CreateUIStyle(m_CreateStyleName);
  38. m_CreateStyleName = "";
  39. }
  40. }
  41. else
  42. {
  43. if (GUILayout.Button("覆盖UIStyle", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  44. {
  45. if (EditorUtility.DisplayDialog("警告", "该模板已存在,是否覆盖?", "是", "否"))
  46. {
  47. CreateUIStyle(m_CreateStyleName);
  48. m_CreateStyleName = "";
  49. }
  50. }
  51. }
  52. EditorGUILayout.Space();
  53. EditorGUILayout.EndHorizontal();
  54. }
  55. EditorGUILayout.Space();
  56. }
  57. }
  58. void ButtonsGUI()
  59. {
  60. if (GUILayout.Button("保存"))
  61. {
  62. UIStyleConfigManager.SaveData();
  63. }
  64. if(GUILayout.Button("应用全部Style"))
  65. {
  66. ApplyAllStyle();
  67. }
  68. }
  69. void CreateUIStyle(string l_uiStyleName)
  70. {
  71. UIStyleInfo l_tmp = new UIStyleInfo();
  72. l_tmp.m_StyleInfoName = l_uiStyleName;
  73. UIStyleConfigManager.AddData(l_uiStyleName, l_tmp);
  74. }
  75. bool isFoldUIStyleList = false;
  76. Vector2 m_styleListScroll = new Vector2();
  77. /// <summary>
  78. /// UIStyle列表
  79. /// </summary>
  80. void UIStyleList()
  81. {
  82. EditorGUI.indentLevel = 1;
  83. isFoldUIStyleList = EditorGUILayout.Foldout(isFoldUIStyleList, "Style列表:");
  84. if (isFoldUIStyleList)
  85. {
  86. m_styleListScroll = EditorGUILayout.BeginScrollView(m_styleListScroll,GUILayout.ExpandHeight(false));
  87. foreach (var item in m_styleData.Values)
  88. {
  89. SingleUIStyleInfo(item);
  90. }
  91. EditorGUILayout.EndScrollView();
  92. }
  93. }
  94. string m_deleteKey = "";
  95. UIStyleComponentType m_compType = UIStyleComponentType.Text;
  96. void SingleUIStyleInfo(UIStyleInfo data)
  97. {
  98. EditorGUI.indentLevel = 2;
  99. data.isFold = EditorGUILayout.Foldout(data.isFold, data.m_StyleInfoName+ ":");
  100. if (data.isFold)
  101. {
  102. if (data.m_TextInfo != null && data.m_TextInfo.isActive)
  103. {
  104. TextStyleGUI(data.m_TextInfo, data);
  105. }
  106. if (data.m_ImageInfo != null && data.m_ImageInfo.isActive)
  107. {
  108. ImageStyleGUI(data.m_ImageInfo, data);
  109. }
  110. if (data.m_RectTransformInfo != null && data.m_RectTransformInfo.isActive)
  111. {
  112. RectTransformGUI(data.m_RectTransformInfo, data);
  113. }
  114. EditorGUILayout.Space();
  115. EditorGUI.indentLevel = 3;
  116. EditorGUILayout.BeginHorizontal();
  117. m_compType = (UIStyleComponentType)EditorGUILayout.EnumPopup("组件类型:", m_compType);
  118. if(GUILayout.Button("添加一个组件"))
  119. {
  120. AddComp(data, m_compType);
  121. }
  122. EditorGUILayout.EndHorizontal();
  123. EditorGUILayout.BeginHorizontal();
  124. EditorGUILayout.Space();
  125. if (GUILayout.Button("应用", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  126. {
  127. ApplySingleStyle(data);
  128. }
  129. EditorGUILayout.Space();
  130. EditorGUILayout.EndHorizontal();
  131. EditorGUILayout.BeginHorizontal();
  132. EditorGUILayout.Space();
  133. if (GUILayout.Button("删除", GUILayout.Width(EditorGUIStyleData.s_ButtonWidth_large)))
  134. {
  135. if (EditorUtility.DisplayDialog("警告", "该操作不可逆,是否删除?", "是", "否"))
  136. {
  137. m_deleteKey = data.m_StyleInfoName;
  138. }
  139. }
  140. EditorGUILayout.Space();
  141. EditorGUILayout.EndHorizontal();
  142. EditorGUILayout.Space();
  143. }
  144. }
  145. void AddComp(UIStyleInfo style, UIStyleComponentType type)
  146. {
  147. switch (type)
  148. {
  149. case UIStyleComponentType.Text:
  150. style.m_TextInfo.isActive = true;
  151. break;
  152. case UIStyleComponentType.Image:
  153. style.m_ImageInfo.isActive = true;
  154. break;
  155. case UIStyleComponentType.RectTransform:
  156. style.m_RectTransformInfo.isActive = true;
  157. break;
  158. }
  159. }
  160. #pragma warning disable
  161. void GraphicGUI(UIGraphicInfo data)
  162. {
  163. data.color = EditorGUILayout.ColorField("Color:", data.color);
  164. data.material = (Material)EditorGUILayout.ObjectField("Material:", data.material, typeof(Material));
  165. data.raycastTarget = EditorGUILayout.Toggle("Raycast Target:", data.raycastTarget);
  166. }
  167. void TextStyleGUI(UITextStyleInfo style,UIStyleInfo info)
  168. {
  169. EditorGUI.indentLevel = 3;
  170. EditorGUILayout.BeginHorizontal();
  171. style.isFold = EditorGUILayout.Foldout(style.isFold, "Text:");
  172. if (GUILayout.Button("删除"))
  173. {
  174. info.m_TextInfo.isActive = false;
  175. }
  176. EditorGUILayout.EndHorizontal();
  177. if (style.isFold)
  178. {
  179. EditorGUI.indentLevel = 4;
  180. EditorGUILayout.LabelField("Character:");
  181. EditorGUI.indentLevel = 5;
  182. style.font = (Font)EditorGUILayout.ObjectField("Font:", style.font, typeof(Font));
  183. style.fontStyle = (FontStyle)EditorGUILayout.EnumPopup("FontStyle", style.fontStyle);
  184. style.fontSize = EditorGUILayout.IntField("FontSize",style.fontSize);
  185. style.lineSpacing = EditorGUILayout.FloatField("LineSpacing", style.lineSpacing);
  186. style.richText = EditorGUILayout.Toggle("RichText", style.richText);
  187. EditorGUI.indentLevel = 4;
  188. EditorGUILayout.LabelField("Paragraph:");
  189. EditorGUI.indentLevel = 5;
  190. style.alignment = (TextAnchor)EditorGUILayout.EnumPopup("FontStyle", style.alignment);
  191. style.alignByGeometry = EditorGUILayout.Toggle("AlignByGeometry", style.alignByGeometry);
  192. style.horizontalOverflow = (HorizontalWrapMode)EditorGUILayout.EnumPopup("HorizontalOverflow", style.horizontalOverflow);
  193. style.verticalOverflow = (VerticalWrapMode)EditorGUILayout.EnumPopup("VerticalOverflow", style.verticalOverflow);
  194. style.bestFit = EditorGUILayout.Toggle("BestFit", style.bestFit);
  195. EditorGUI.indentLevel = 4;
  196. GraphicGUI(style);
  197. }
  198. }
  199. void ImageStyleGUI(UIImageInfo style, UIStyleInfo info)
  200. {
  201. EditorGUI.indentLevel = 3;
  202. EditorGUILayout.BeginHorizontal();
  203. style.isFold = EditorGUILayout.Foldout(style.isFold, "Image:");
  204. if (GUILayout.Button("删除"))
  205. {
  206. info.m_ImageInfo.isActive = false;
  207. }
  208. EditorGUILayout.EndHorizontal();
  209. if (style.isFold)
  210. {
  211. EditorGUI.indentLevel = 4;
  212. GraphicGUI(style);
  213. }
  214. }
  215. void RectTransformGUI(UIRectTransformInfo style, UIStyleInfo info)
  216. {
  217. EditorGUI.indentLevel = 3;
  218. EditorGUILayout.BeginHorizontal();
  219. style.isFold = EditorGUILayout.Foldout(style.isFold, "RectTransform:");
  220. if (GUILayout.Button("删除"))
  221. {
  222. info.m_RectTransformInfo.isActive = false;
  223. }
  224. EditorGUILayout.EndHorizontal();
  225. if (style.isFold)
  226. {
  227. EditorGUI.indentLevel = 4;
  228. style.anchoredPosition3D = EditorGUILayout.Vector3Field("AnchoredPosition3D", style.anchoredPosition3D);
  229. style.anchorMin = EditorGUILayout.Vector2Field("AnchorMin", style.anchorMin);
  230. style.anchorMax = EditorGUILayout.Vector2Field("AnchorMax", style.anchorMax);
  231. style.pivot = EditorGUILayout.Vector2Field("Pivot", style.pivot);
  232. style.localRotation = EditorGUILayout.Vector3Field("LocalRotation", style.localRotation);
  233. style.localScale = EditorGUILayout.Vector3Field("LocalScale", style.localScale);
  234. }
  235. }
  236. /// <summary>
  237. /// 应用单个Style
  238. /// </summary>
  239. /// <param name="style">UIStyle</param>
  240. void ApplySingleStyle(UIStyleInfo style)
  241. {
  242. foreach (var item in UIEditorWindow.allUIPrefab.Values)
  243. {
  244. UIStyleComponent[] comps = item.GetComponentsInChildren<UIStyleComponent>();
  245. for (int i = 0; i < comps.Length; i++)
  246. {
  247. if (comps[i].m_styleID.Equals(style.m_StyleInfoName))
  248. {
  249. style.ApplyStyle(comps[i].gameObject );
  250. }
  251. }
  252. }
  253. }
  254. void ApplyAllStyle()
  255. {
  256. foreach (var item in m_styleData.Values)
  257. {
  258. ApplySingleStyle(item);
  259. }
  260. }
  261. void DeleteLogic()
  262. {
  263. if(m_deleteKey != "")
  264. {
  265. UIStyleConfigManager.DeleteData(m_deleteKey);
  266. m_deleteKey = "";
  267. }
  268. }
  269. }
  270. public enum UIStyleComponentType
  271. {
  272. Text,
  273. Image,
  274. RectTransform,
  275. }