TagSupportText.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using HyperlinkText;
  6. namespace UnityEngine.UI
  7. {
  8. [ExecuteInEditMode]
  9. [AddComponentMenu("UI/Effects/Tag Support Text", 64)]
  10. public class TagSupportText : BaseMeshEffect
  11. {
  12. protected TagSupportText()
  13. {
  14. }
  15. private Text text
  16. {
  17. get
  18. {
  19. Text result;
  20. if ((result = this.m_CachedText) == null)
  21. {
  22. result = (this.m_CachedText = base.GetComponent<Text>());
  23. }
  24. return result;
  25. }
  26. }
  27. public override void ModifyMesh(VertexHelper vertexHelper)
  28. {
  29. if (!this.IsActive())
  30. {
  31. return;
  32. }
  33. List<UIVertex> list = TagSupportText._verticesPool.Get();
  34. vertexHelper.GetUIVertexStream(list);
  35. this.Modify(ref list);
  36. vertexHelper.Clear();
  37. vertexHelper.AddUIVertexTriangleStream(list);
  38. TagSupportText._verticesPool.Release(list);
  39. }
  40. public void Modify(ref List<UIVertex> verts)
  41. {
  42. if (!this.IsActive())
  43. {
  44. return;
  45. }
  46. if (this.text == null)
  47. {
  48. Debug.LogWarning("TagSupportText:Text コンポーネントが見つかりません");
  49. return;
  50. }
  51. string[] array = this.text.text.Split(new char[]
  52. {
  53. '\n'
  54. });
  55. switch (this.text.alignment)
  56. {
  57. }
  58. float num = (float)this.text.fontSize / 100f;
  59. int num2 = 0;
  60. for (int i = 0; i < array.Length; i++)
  61. {
  62. float num3 = 0f;
  63. string text = array[i];
  64. int length = text.Length;
  65. IEnumerator regexMatchedTagCollection = this.GetRegexMatchedTagCollection(text, out length);
  66. Match match = null;
  67. if (regexMatchedTagCollection.MoveNext())
  68. {
  69. match = (Match)regexMatchedTagCollection.Current;
  70. }
  71. int j = 0;
  72. int num4 = 0;
  73. while (j < text.Length)
  74. {
  75. int num5 = num2 * 6;
  76. int num6 = num2 * 6 + 1;
  77. int num7 = num2 * 6 + 2;
  78. int num8 = num2 * 6 + 3;
  79. int num9 = num2 * 6 + 4;
  80. int num10 = num2 * 6 + 5;
  81. if (num10 > verts.Count - 1)
  82. {
  83. return;
  84. }
  85. UIVertex value = verts[num5];
  86. UIVertex value2 = verts[num6];
  87. UIVertex value3 = verts[num7];
  88. UIVertex value4 = verts[num8];
  89. UIVertex value5 = verts[num9];
  90. UIVertex value6 = verts[num10];
  91. if (match != null && match.Index == j)
  92. {
  93. string value7 = match.Value;
  94. TextGenerationSettings generationSettings = this.text.GetGenerationSettings(this.text.rectTransform.rect.size);
  95. float preferredWidth = this.text.cachedTextGenerator.GetPreferredWidth(value7, generationSettings);
  96. num3 += -preferredWidth;
  97. for (int k = 0; k < value7.Length; k++)
  98. {
  99. value = verts[num5 + k * 6];
  100. value2 = verts[num6 + k * 6];
  101. value3 = verts[num7 + k * 6];
  102. value4 = verts[num8 + k * 6];
  103. value5 = verts[num9 + k * 6];
  104. value6 = verts[num10 + k * 6];
  105. value.position = Vector3.zero;
  106. value2.position = Vector3.zero;
  107. value3.position = Vector3.zero;
  108. value4.position = Vector3.zero;
  109. value5.position = Vector3.zero;
  110. value6.position = Vector3.zero;
  111. verts[num5 + k * 6] = value;
  112. verts[num6 + k * 6] = value2;
  113. verts[num7 + k * 6] = value3;
  114. verts[num8 + k * 6] = value4;
  115. verts[num9 + k * 6] = value5;
  116. verts[num10 + k * 6] = value6;
  117. }
  118. num4--;
  119. num2 += value7.Length;
  120. j += value7.Length - 1;
  121. match = null;
  122. if (regexMatchedTagCollection.MoveNext())
  123. {
  124. match = (Match)regexMatchedTagCollection.Current;
  125. }
  126. }
  127. else
  128. {
  129. Vector3 b = Vector3.right * num3;
  130. value.position += b;
  131. value2.position += b;
  132. value3.position += b;
  133. value4.position += b;
  134. value5.position += b;
  135. value6.position += b;
  136. verts[num5] = value;
  137. verts[num6] = value2;
  138. verts[num7] = value3;
  139. verts[num8] = value4;
  140. verts[num9] = value5;
  141. verts[num10] = value6;
  142. num2++;
  143. }
  144. j++;
  145. num4++;
  146. }
  147. num2++;
  148. }
  149. }
  150. private IEnumerator GetRegexMatchedTagCollection(string line, out int lineLengthWithoutTags)
  151. {
  152. MatchCollection matchCollection = TagSupportText.REG_SUPPORTED_TAG.Matches(line);
  153. lineLengthWithoutTags = 0;
  154. int num = 0;
  155. if (matchCollection.Count > 0)
  156. {
  157. IEnumerator enumerator = matchCollection.GetEnumerator();
  158. try
  159. {
  160. while (enumerator.MoveNext())
  161. {
  162. object obj = enumerator.Current;
  163. Match match = (Match)obj;
  164. num += match.Length;
  165. }
  166. }
  167. finally
  168. {
  169. IDisposable disposable;
  170. if ((disposable = (enumerator as IDisposable)) != null)
  171. {
  172. disposable.Dispose();
  173. }
  174. }
  175. }
  176. lineLengthWithoutTags = line.Length - num;
  177. return matchCollection.GetEnumerator();
  178. }
  179. private static string SupportedTagRegexPatterns = "(<a href\\s*=\\s*.*?>)|(</a>)|(<u>)|(</u>)";
  180. private static Regex REG_SUPPORTED_TAG = new Regex(TagSupportText.SupportedTagRegexPatterns, RegexOptions.CultureInvariant);
  181. private static readonly ObjectPool<List<UIVertex>> _verticesPool = new ObjectPool<List<UIVertex>>(null, delegate(List<UIVertex> l)
  182. {
  183. l.Clear();
  184. });
  185. private Text m_CachedText;
  186. }
  187. }