SymbolText.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace WXB
  6. {
  7. [RequireComponent(typeof(RectTransform))]
  8. [AddComponentMenu("UI/SymbolText")]
  9. public partial class SymbolText : Text, Owner
  10. {
  11. static SymbolText()
  12. {
  13. Font.textureRebuilt += RebuildForFont;
  14. }
  15. public static Mesh WorkerMesh { get { return workerMesh; } }
  16. static void RebuildForFont(Font f)
  17. {
  18. for (int i = 0; i < ActiveList.Count; ++i)
  19. {
  20. if (ActiveList[i].font == f)
  21. continue;
  22. if (ActiveList[i].isUsedFont(f))
  23. ActiveList[i].FontTextureChangedOther();
  24. }
  25. }
  26. static public List<SymbolText> ActiveList = new List<SymbolText>();
  27. static TextParser sTextParser = new TextParser();
  28. protected TextParser Parser { get { return sTextParser; } }
  29. // 解析出来的结点
  30. [NonSerialized]
  31. protected LinkedList<NodeBase> mNodeList = new LinkedList<NodeBase>();
  32. [SerializeField]
  33. string m_ElementSegment = "Default"; // 分割类型
  34. protected bool m_textDirty = false; // 文字内容变化了,需要重新解析下结点
  35. protected bool m_renderNodeDirty = false; // 渲染结点的内容变化了,需要重新计算下
  36. protected bool m_layoutDirty = false; // 大小发生变化
  37. // 是否有使用此字体
  38. public bool isUsedFont(Font f)
  39. {
  40. foreach (NodeBase nb in mNodeList)
  41. {
  42. if (nb is TextNode && ((TextNode)nb).d_font == f)
  43. return true;
  44. }
  45. return false;
  46. }
  47. public void SetRenderDirty()
  48. {
  49. FreeDraws();
  50. SetVerticesDirty();
  51. SetMaterialDirty();
  52. }
  53. public override string text
  54. {
  55. set
  56. {
  57. if (string.IsNullOrEmpty(value))
  58. {
  59. if (string.IsNullOrEmpty(m_Text))
  60. return;
  61. m_Text = "";
  62. SetVerticesDirty();
  63. SetTextDirty();
  64. }
  65. else if (m_Text != value)
  66. {
  67. m_Text = value;
  68. m_Text = m_Text.Replace("[]", "");
  69. SetVerticesDirty();
  70. SetLayoutDirty();
  71. SetTextDirty();
  72. }
  73. }
  74. }
  75. public Anchor anchor
  76. {
  77. get { return (Anchor)alignment; }
  78. }
  79. static void FreeNode(NodeBase node)
  80. {
  81. if (node == null)
  82. {
  83. Debug.LogErrorFormat("FreeNode error! node == null");
  84. return;
  85. }
  86. node.Release();
  87. node = null;
  88. }
  89. public void Clear()
  90. {
  91. foreach (NodeBase node in mNodeList)
  92. FreeNode(node);
  93. mNodeList.Clear();
  94. FreeDraws();
  95. }
  96. [SerializeField]
  97. int m_MinLineHeight = 10;
  98. // 最小行高
  99. public int minLineHeight
  100. {
  101. get { return m_MinLineHeight; }
  102. set
  103. {
  104. if (m_MinLineHeight != value)
  105. {
  106. m_MinLineHeight = value;
  107. SetAllDirty();
  108. }
  109. }
  110. }
  111. [SerializeField]
  112. LineAlignment m_LineAlignment = LineAlignment.Bottom;
  113. // 最小行高
  114. public LineAlignment lineAlignment
  115. {
  116. get { return m_LineAlignment; }
  117. set
  118. {
  119. if (m_LineAlignment != value)
  120. {
  121. m_LineAlignment = value;
  122. SetAllDirty();
  123. }
  124. }
  125. }
  126. protected TextParser.Config CreateConfig()
  127. {
  128. TextParser.Config config = new TextParser.Config();
  129. config.anchor = Anchor.Null;
  130. config.font = font;
  131. config.fontStyle = fontStyle;
  132. config.fontSize = fontSize;
  133. config.fontColor = color;
  134. config.isBlink = false;
  135. config.isStrickout = false;
  136. config.isUnderline = false;
  137. config.dyncSpeed = Tools.s_dyn_default_speed;
  138. config.lineAlignment = lineAlignment;
  139. BaseMeshEffect effect = GetComponent<BaseMeshEffect>();
  140. if (effect != null)
  141. {
  142. if (effect is Outline)
  143. {
  144. Outline outline = effect as Outline;
  145. config.effectType = EffectType.Outline;
  146. config.effectColor = outline.effectColor;
  147. config.effectDistance = outline.effectDistance;
  148. }
  149. else if (effect is Shadow)
  150. {
  151. Shadow shadow = effect as Shadow;
  152. config.effectType = EffectType.Shadow;
  153. config.effectColor = shadow.effectColor;
  154. config.effectDistance = shadow.effectDistance;
  155. }
  156. else
  157. {
  158. config.effectType = EffectType.Null;
  159. }
  160. }
  161. else
  162. {
  163. config.effectType = EffectType.Null;
  164. }
  165. return config;
  166. }
  167. RenderCache mRenderCache; // 渲染缓存
  168. public RenderCache renderCache
  169. {
  170. get
  171. {
  172. if (mRenderCache == null)
  173. mRenderCache = new RenderCache(this);
  174. return mRenderCache;
  175. }
  176. }
  177. List<Line> mLines = new List<Line>(); // 每一行的大小
  178. protected override void Awake()
  179. {
  180. mRenderCache = new RenderCache(this);
  181. base.Awake();
  182. m_textDirty = true;
  183. m_renderNodeDirty = true;
  184. DestroyDrawChild();
  185. }
  186. public void DestroyDrawChild()
  187. {
  188. int childCount = rectTransform.childCount;
  189. var components = ListPool<Component>.Get();
  190. Draw d;
  191. for (int i = 0; i < childCount; ++i)
  192. {
  193. if ((d = (rectTransform.GetChild(i)).GetComponent<Draw>()) != null)
  194. {
  195. components.Add(d as Component);
  196. }
  197. }
  198. for (int i = 0; i < components.Count; ++i)
  199. {
  200. Tools.Destroy(components[i].gameObject);
  201. }
  202. ListPool<Component>.Release(components);
  203. }
  204. [SerializeField]
  205. bool m_isCheckFontY = false;
  206. public bool isCheckFontY
  207. {
  208. get { return m_isCheckFontY; }
  209. set
  210. {
  211. if (m_isCheckFontY == value)
  212. return;
  213. m_isCheckFontY = value;
  214. SetVerticesDirty();
  215. }
  216. }
  217. protected override void UpdateGeometry()
  218. {
  219. base.UpdateGeometry();
  220. VertexHelper vh = Tools.vertexHelper;
  221. if (rectTransform != null && rectTransform.rect.width >= 0 && rectTransform.rect.height >= 0 && !renderCache.isEmpty)
  222. {
  223. Rect inputRect = rectTransform.rect;
  224. Vector2 offset = new Vector2(-rectTransform.pivot.x * inputRect.width, rectTransform.pivot.y * inputRect.height);
  225. //inputRect.position += offset;
  226. float nodeHeight = getNodeHeight();
  227. //float nodeWidth = getNodeWidth();
  228. Vector2 refPoint = Vector2.zero;
  229. switch (alignment)
  230. {
  231. case TextAnchor.UpperCenter:
  232. case TextAnchor.UpperLeft:
  233. case TextAnchor.UpperRight: // 顶对齐
  234. break;
  235. case TextAnchor.MiddleCenter:
  236. case TextAnchor.MiddleLeft:
  237. case TextAnchor.MiddleRight:
  238. refPoint.y = (inputRect.height - nodeHeight) / 2f;
  239. break;
  240. default:
  241. refPoint.y = (inputRect.height - nodeHeight);
  242. break;
  243. }
  244. refPoint.x += offset.x;
  245. refPoint.y += offset.y;
  246. vh.Clear();
  247. float scaleSize = pixelsPerUnit;
  248. m_DisableFontTextureRebuiltCallback = true;
  249. if (alignByGeometry)
  250. {
  251. offset = Vector2.zero;
  252. mRenderCache.OnAlignByGeometry(ref offset, scaleSize, mLines[0].y);
  253. refPoint -= offset;
  254. }
  255. for (int i = 0; i < mLines.Count; ++i)
  256. {
  257. mLines[i].minY = float.MaxValue;
  258. mLines[i].maxY = float.MinValue;
  259. }
  260. mRenderCache.OnCheckLineY(scaleSize);
  261. mRenderCache.Render(vh, inputRect, refPoint, scaleSize, workerMesh, material);
  262. m_DisableFontTextureRebuiltCallback = false;
  263. last_size = rectTransform.rect.size;
  264. }
  265. else
  266. {
  267. vh.Clear(); // clear the vertex helper so invalid graphics dont draw.
  268. canvasRenderer.Clear();
  269. last_size = Vector2.zero;
  270. }
  271. }
  272. public float getNodeHeight()
  273. {
  274. float height = 0;
  275. for (int i = 0; i < mLines.Count; ++i)
  276. height += mLines[i].y;
  277. return height;
  278. }
  279. public float getNodeWidth()
  280. {
  281. float width = 0;
  282. float maxWidth = 0;
  283. NodeBase node;
  284. LinkedListNode<NodeBase> itor = mNodeList.First;
  285. while (itor != null)
  286. {
  287. node = itor.Value;
  288. itor = itor.Next;
  289. width += node.getWidth();
  290. if (node.isNewLine())
  291. {
  292. if (maxWidth < width)
  293. maxWidth = width;
  294. width = 0;
  295. }
  296. node = null;
  297. }
  298. if (maxWidth < width)
  299. maxWidth = width;
  300. return maxWidth;
  301. }
  302. protected override void OnEnable()
  303. {
  304. base.OnEnable();
  305. ActiveList.Add(this);
  306. }
  307. protected override void OnDisable()
  308. {
  309. base.OnDisable();
  310. FreeDraws();
  311. ActiveList.Remove(this);
  312. }
  313. protected override void OnDestroy()
  314. {
  315. if (mRenderCache != null)
  316. mRenderCache.Release();
  317. FreeDraws();
  318. base.OnDestroy();
  319. Clear();
  320. }
  321. protected void LateUpdate()
  322. {
  323. float deltaTime = Time.deltaTime;
  324. for (int i = 0; i < m_UsedDraws.Count; ++i)
  325. m_UsedDraws[i].UpdateSelf(deltaTime);
  326. }
  327. }
  328. }