UIStyleInfo.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine.UI;
  6. using UnityEditor;
  7. public class UIStyleInfo
  8. {
  9. public string m_StyleInfoName;
  10. public UITextStyleInfo m_TextInfo = new UITextStyleInfo();
  11. public UIRawImageInfo m_RawImageInfo = new UIRawImageInfo();
  12. public UIImageInfo m_ImageInfo = new UIImageInfo();
  13. public UIRectTransformInfo m_RectTransformInfo = new UIRectTransformInfo();
  14. public UIButtonInfo m_ButtonInfo = new UIButtonInfo();
  15. public bool isFold = false;
  16. public UIStyleInfo GetStyle(GameObject go)
  17. {
  18. Text compText = go.GetComponent<Text>();
  19. if (compText != null)
  20. {
  21. m_TextInfo.isActive = true;
  22. m_TextInfo.GetStyle(compText);
  23. }
  24. Image compImage = go.GetComponent<Image>();
  25. if (compImage != null)
  26. {
  27. m_ImageInfo.isActive = true;
  28. m_ImageInfo.GetStyle(compImage);
  29. }
  30. RectTransform compRectTransform = go.GetComponent<RectTransform>();
  31. if (compRectTransform != null)
  32. {
  33. m_RectTransformInfo.isActive = true;
  34. m_RectTransformInfo.GetStyle(compRectTransform);
  35. }
  36. return this;
  37. }
  38. public void ApplyStyle(GameObject go)
  39. {
  40. if (m_TextInfo.isActive)
  41. {
  42. Text compText = go.GetComponent<Text>();
  43. if (compText!= null)
  44. {
  45. m_TextInfo.ApplyStyle(compText);
  46. }
  47. }
  48. if (m_ImageInfo.isActive)
  49. {
  50. Image compText = go.GetComponent<Image>();
  51. if (compText != null)
  52. {
  53. m_ImageInfo.ApplyStyle(compText);
  54. }
  55. }
  56. if (m_RectTransformInfo.isActive)
  57. {
  58. RectTransform compText = go.GetComponent<RectTransform>();
  59. if (compText != null)
  60. {
  61. m_RectTransformInfo.ApplyStyle(compText);
  62. }
  63. }
  64. }
  65. }
  66. public class UIStyleInfoInterface
  67. {
  68. /// <summary>
  69. /// 正则表达式匹配规则
  70. /// </summary>
  71. public string REfilter;
  72. public bool isFold = false;
  73. public bool isActive = false;
  74. public bool IsFits(string l_UIname)
  75. {
  76. //return Regex.IsMatch(l_UIname, REfilter);
  77. return (l_UIname == REfilter);
  78. }
  79. public virtual void ApplyStyle(Component component)
  80. {
  81. }
  82. public virtual UIStyleInfoInterface GetStyle(Component component)
  83. {
  84. //UIInfoInterface tmp = new UIInfoInterface();
  85. REfilter = component.name;
  86. return this;
  87. }
  88. }
  89. public class UIGraphicInfo : UIStyleInfoInterface
  90. {
  91. public bool raycastTarget;
  92. public Color color;
  93. public Material material;
  94. public override void ApplyStyle(Component component)
  95. {
  96. base.ApplyStyle(component);
  97. Graphic comp = (Graphic)component;
  98. comp.color = color;
  99. comp.material = material;
  100. comp.raycastTarget = raycastTarget;
  101. }
  102. public override UIStyleInfoInterface GetStyle(Component component)
  103. {
  104. Graphic comp = (Graphic)component;
  105. UIGraphicInfo style = (UIGraphicInfo)base.GetStyle(component);
  106. style.color = comp.color;
  107. style.material = comp.material;
  108. style.raycastTarget = comp.raycastTarget;
  109. return style;
  110. }
  111. }
  112. [System.Serializable]
  113. public class UITextStyleInfo : UIGraphicInfo
  114. {
  115. public Font font;
  116. public FontStyle fontStyle;
  117. public int fontSize;
  118. public float lineSpacing;
  119. public TextAnchor alignment;
  120. public HorizontalWrapMode horizontalOverflow;
  121. public VerticalWrapMode verticalOverflow;
  122. public bool alignByGeometry;
  123. public bool richText;
  124. public bool bestFit;
  125. public override void ApplyStyle(Component component)
  126. {
  127. base.ApplyStyle(component);
  128. Text comp = (Text)component;
  129. comp.font = font;
  130. comp.fontStyle = fontStyle;
  131. comp.fontSize = fontSize;
  132. comp.lineSpacing = lineSpacing;
  133. comp.alignment = alignment;
  134. comp.horizontalOverflow = horizontalOverflow;
  135. comp.verticalOverflow = verticalOverflow;
  136. comp.alignByGeometry = alignByGeometry;
  137. comp.supportRichText = richText;
  138. comp.resizeTextForBestFit = bestFit;
  139. }
  140. public override UIStyleInfoInterface GetStyle(Component component)
  141. {
  142. Text comp = (Text)component;
  143. UITextStyleInfo style = (UITextStyleInfo)base.GetStyle(component); ;
  144. style.font = comp.font;
  145. style.fontStyle = comp.fontStyle;
  146. style.fontSize = comp.fontSize;
  147. style.lineSpacing = comp.lineSpacing;
  148. style.alignment = comp.alignment;
  149. style.horizontalOverflow = comp.horizontalOverflow;
  150. style.verticalOverflow = comp.verticalOverflow;
  151. style.alignByGeometry = comp.alignByGeometry;
  152. style.richText = comp.supportRichText;
  153. style.bestFit = comp.resizeTextForBestFit;
  154. return style;
  155. }
  156. }
  157. [System.Serializable]
  158. public class UIImageInfo : UIGraphicInfo
  159. {
  160. }
  161. [System.Serializable]
  162. public class UIRawImageInfo : UIGraphicInfo
  163. {
  164. Rect uvRect;
  165. public override void ApplyStyle(Component component)
  166. {
  167. base.ApplyStyle(component);
  168. RawImage comp = (RawImage)component;
  169. comp.uvRect = uvRect;
  170. }
  171. public override UIStyleInfoInterface GetStyle(Component component)
  172. {
  173. RawImage comp = (RawImage)component;
  174. UIRawImageInfo style = (UIRawImageInfo)base.GetStyle(component);
  175. style.uvRect = comp.uvRect;
  176. return style;
  177. }
  178. }
  179. [System.Serializable]
  180. public class UIButtonInfo : UIStyleInfoInterface
  181. {
  182. bool interactable;
  183. public override void ApplyStyle(Component component)
  184. {
  185. base.ApplyStyle(component);
  186. Button comp = (Button)component;
  187. comp.interactable = interactable;
  188. }
  189. public override UIStyleInfoInterface GetStyle(Component component)
  190. {
  191. Button comp = (Button)component;
  192. UIButtonInfo style = (UIButtonInfo)base.GetStyle(component);
  193. style.interactable = comp.interactable;
  194. return style;
  195. }
  196. }
  197. [System.Serializable]
  198. public class UIRectTransformInfo : UIStyleInfoInterface
  199. {
  200. public Vector2 anchorMin = Vector2.zero;
  201. public Vector2 anchorMax = Vector2.one;
  202. public Vector2 sizeDelta;
  203. public Vector2 pivot;
  204. public Vector3 anchoredPosition3D = Vector3.zero;
  205. public Vector3 localRotation = Vector3.zero;
  206. public Vector3 localScale = Vector3.one;
  207. public override void ApplyStyle(Component component)
  208. {
  209. base.ApplyStyle(component);
  210. RectTransform comp = (RectTransform)component;
  211. comp.anchorMin = anchorMin;
  212. comp.anchorMax = anchorMax;
  213. comp.localEulerAngles = localRotation;
  214. comp.localScale = localScale;
  215. comp.pivot = pivot;
  216. comp.anchoredPosition3D = anchoredPosition3D;
  217. comp.sizeDelta = sizeDelta;
  218. }
  219. public override UIStyleInfoInterface GetStyle(Component component)
  220. {
  221. RectTransform comp = (RectTransform)component;
  222. UIRectTransformInfo style = (UIRectTransformInfo)base.GetStyle(component);
  223. style.anchorMin = comp.anchorMin;
  224. style.anchorMax = comp.anchorMax;
  225. style.localRotation = comp.localEulerAngles;
  226. style.localScale = comp.localScale;
  227. style.pivot = comp.pivot;
  228. style.anchoredPosition3D = comp.anchoredPosition3D;
  229. style.sizeDelta = comp.sizeDelta;
  230. return style;
  231. }
  232. }