TextPic.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /// Credit drobina, w34edrtfg, playemgames
  2. /// Sourced from - http://forum.unity3d.com/threads/sprite-icons-with-text-e-g-emoticons.265927/
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using UnityEngine;
  8. using UnityEngine.Events;
  9. using UnityEngine.EventSystems;
  10. using UnityEngine.UI;
  11. [AddComponentMenu("UI/Extensions/TextPic")]
  12. public class TextPic : Text, IPointerClickHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler {
  13. // 解决换行空格问题
  14. private string _rawText = "";
  15. public string rawText {
  16. get { return _rawText; }
  17. }
  18. public override string text {
  19. get {
  20. return m_Text;
  21. }
  22. set {
  23. if (string.IsNullOrEmpty(value)) {
  24. if (!string.IsNullOrEmpty(m_Text)) {
  25. m_Text = "";
  26. _rawText = "";
  27. SetVerticesDirty();
  28. }
  29. } else if (m_Text != value.Replace(" ", "¨")) {
  30. m_Text = value.Replace(" ", "¨");
  31. _rawText = value;
  32. SetVerticesDirty();
  33. SetLayoutDirty();
  34. }
  35. }
  36. }
  37. // Icon entry to replace text with
  38. [Serializable]
  39. public struct IconName {
  40. public string name;
  41. public Sprite sprite;
  42. public Vector2 offset;
  43. public Vector2 scale;
  44. }
  45. // Icons and text to replace
  46. private static List<IconName> _iconList;
  47. private static List<IconName> iconList {
  48. get {
  49. if (_iconList == null) {
  50. _iconList = new List<IconName>();
  51. foreach (Sprite sprite in Resources.LoadAll<Sprite>("Controls/TextPic")) {
  52. _iconList.Add(new IconName { name = sprite.name, sprite = sprite, offset = Vector2.zero, scale = Vector2.one });
  53. }
  54. Debug.Log("加载了 " + _iconList.Count + " 个 TextPic 图标");
  55. }
  56. return _iconList;
  57. }
  58. }
  59. public static void ResetIconList() {
  60. _iconList = null;
  61. }
  62. [Tooltip("Global scaling factor for all images")]
  63. public float ImageScalingFactor = 1.05f;
  64. // Write the name or hex value of the hyperlink color
  65. public string hyperlinkColor = "blue";
  66. // Offset image by x, y
  67. public Vector2 imageOffset = new(0, -2.5f);
  68. public bool isCreating_m_HrefInfos = true;
  69. [Serializable]
  70. public class HrefClickEvent : UnityEvent<string> { }
  71. [SerializeField]
  72. private HrefClickEvent m_OnHrefClick = new();
  73. /// <summary>
  74. /// Hyperlink Click Event
  75. /// </summary>
  76. public HrefClickEvent onHrefClick {
  77. get { return m_OnHrefClick; }
  78. set { m_OnHrefClick = value; }
  79. }
  80. /// <summary>
  81. /// Image Pool
  82. /// </summary>
  83. private readonly List<Image> m_ImagesPool = new();
  84. private readonly List<GameObject> culled_ImagesPool = new();
  85. // Used for check for culling images
  86. private bool clearImages = false;
  87. // Lock to ensure images get culled properly
  88. private UnityEngine.Object thisLock = new();
  89. /// <summary>
  90. /// Vertex Index
  91. /// </summary>
  92. private readonly List<int> m_ImagesVertexIndex = new();
  93. /// <summary>
  94. /// Regular expression to replace
  95. /// </summary>
  96. private static readonly Regex s_Regex =
  97. new(@"<quad name=(.+?) size=(\d*\.?\d+%?) width=(\d*\.?\d+%?) />", RegexOptions.Singleline);
  98. /// <summary>
  99. /// Hyperlink Regular Expression
  100. /// </summary>
  101. private static readonly Regex s_HrefRegex =
  102. new(@"<a href=([^>\n\s]+)>(.*?)(</a>)", RegexOptions.Singleline);
  103. // String to create quads
  104. private string fixedString;
  105. // Update the quad images when true
  106. private bool updateQuad = false;
  107. /// <summary>
  108. /// After parsing the final text
  109. /// </summary>
  110. private string m_OutputText;
  111. private Button button;
  112. // Used for custom selection as a variable for other scripts
  113. private bool selected = false;
  114. public bool Selected {
  115. get { return selected; }
  116. set { selected = value; }
  117. }
  118. // Positions of images for icon placement
  119. private List<Vector2> positions = new();
  120. // Little hack to support multiple hrefs with same name
  121. private string previousText = "";
  122. /// <summary>
  123. /// Hyperlinks Info
  124. /// </summary>
  125. [Serializable]
  126. public class HrefInfo {
  127. public int startIndex;
  128. public int endIndex;
  129. public string name;
  130. public readonly List<Rect> boxes = new();
  131. }
  132. /// <summary>
  133. /// Hyperlink List
  134. /// </summary>
  135. private readonly List<HrefInfo> m_HrefInfos = new();
  136. /// <summary>
  137. /// Text Builder
  138. /// </summary>
  139. private static readonly StringBuilder s_TextBuilder = new();
  140. // Matches for quad tags
  141. private MatchCollection matches;
  142. // Matches for quad tags
  143. private MatchCollection href_matches;
  144. // Matches for removing characters
  145. private MatchCollection removeCharacters;
  146. // Index of current pic
  147. private int picIndex;
  148. // Index of current pic vertex
  149. private int vertIndex;
  150. /// <summary>
  151. /// Unity 2019.1.5 Fixes to text placement resulting from the removal of verts for spaces and non rendered characters
  152. /// </summary>
  153. // There is no directive for incremented versions so will have to hack together
  154. private bool usesNewRendering = false;
  155. #if UNITY_2019_1_OR_NEWER
  156. /// <summary>
  157. /// Regular expression to remove non rendered characters
  158. /// </summary>
  159. private static readonly Regex remove_Regex =
  160. new(@"<b>|</b>|<i>|</i>|<size=.*?>|</size>|<color=.*?>|</color>|<material=.*?>|</material>|<quad name=(.+?) size=(\d*\.?\d+%?) width=(\d*\.?\d+%?) />|<a href=([^>\n\s]+)>|</a>|\s", RegexOptions.Singleline);
  161. // List of indexes that are compared against matches to remove quad tags
  162. List<int> indexes = new();
  163. // Characters to remove from string for finding the correct index for vertices for images
  164. private int charactersRemoved = 0;
  165. // Characters to remove from string for finding the correct start index for vertices for href bounds
  166. private int startCharactersRemoved = 0;
  167. // Characters to remove from string for finding the correct end index for vertices for href bounds
  168. private int endCharactersRemoved = 0;
  169. #endif
  170. // Count of current href
  171. private int count = 0;
  172. // Index of current href
  173. private int indexText = 0;
  174. // Original text temporary variable holder
  175. private string originalText;
  176. // Vertex we are modifying
  177. private UIVertex vert;
  178. // Local Point for Href
  179. private Vector2 lp;
  180. /// METHODS ///
  181. public void ResetIcons() {
  182. Reset_m_HrefInfos();
  183. base.Start();
  184. }
  185. protected void UpdateQuadImage() {
  186. #if UNITY_EDITOR && !UNITY_2018_3_OR_NEWER
  187. if (UnityEditor.PrefabUtility.GetPrefabType(this) == UnityEditor.PrefabType.Prefab) {
  188. return;
  189. }
  190. #endif
  191. m_OutputText = GetOutputText();
  192. matches = s_Regex.Matches(m_OutputText);
  193. if (matches != null && matches.Count > 0) {
  194. for (int i = 0; i < matches.Count; i++) {
  195. m_ImagesPool.RemoveAll(image => image == null);
  196. if (m_ImagesPool.Count == 0) {
  197. GetComponentsInChildren<Image>(true, m_ImagesPool);
  198. }
  199. if (matches.Count > m_ImagesPool.Count) {
  200. DefaultControls.Resources resources = new();
  201. GameObject go = DefaultControls.CreateImage(resources);
  202. go.layer = gameObject.layer;
  203. RectTransform rt = go.transform as RectTransform;
  204. if (rt) {
  205. rt.SetParent(rectTransform);
  206. rt.anchoredPosition3D = Vector3.zero;
  207. rt.localRotation = Quaternion.identity;
  208. rt.localScale = Vector3.one;
  209. }
  210. m_ImagesPool.Add(go.GetComponent<Image>());
  211. }
  212. string spriteName = matches[i].Groups[1].Value;
  213. Image img = m_ImagesPool[i];
  214. Vector2 imgoffset = Vector2.zero;
  215. if (img.sprite == null || img.sprite.name != spriteName) {
  216. if (iconList != null && iconList.Count > 0) {
  217. for (int s = 0; s < iconList.Count; s++) {
  218. if (iconList[s].name == spriteName) {
  219. img.sprite = iconList[s].sprite;
  220. img.preserveAspect = true;
  221. img.rectTransform.sizeDelta = new Vector2(fontSize * ImageScalingFactor * iconList[s].scale.x,
  222. fontSize * ImageScalingFactor * iconList[s].scale.y);
  223. imgoffset = iconList[s].offset;
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. img.enabled = true;
  230. if (positions.Count > 0 && i < positions.Count) {
  231. img.rectTransform.anchoredPosition = positions[i] += imgoffset;
  232. }
  233. }
  234. } else {
  235. // If there are no matches, remove the images from the pool
  236. for (int i = m_ImagesPool.Count - 1; i > 0; i--) {
  237. if (m_ImagesPool[i]) {
  238. if (!culled_ImagesPool.Contains(m_ImagesPool[i].gameObject)) {
  239. culled_ImagesPool.Add(m_ImagesPool[i].gameObject);
  240. m_ImagesPool.Remove(m_ImagesPool[i]);
  241. }
  242. }
  243. }
  244. }
  245. // Remove any images that are not being used
  246. for (int i = m_ImagesPool.Count - 1; i >= matches.Count; i--) {
  247. if (i >= 0 && m_ImagesPool.Count > 0) {
  248. if (m_ImagesPool[i]) {
  249. if (!culled_ImagesPool.Contains(m_ImagesPool[i].gameObject)) {
  250. culled_ImagesPool.Add(m_ImagesPool[i].gameObject);
  251. m_ImagesPool.Remove(m_ImagesPool[i]);
  252. }
  253. }
  254. }
  255. }
  256. // Clear the images when it is safe to do so
  257. if (culled_ImagesPool.Count > 0) {
  258. clearImages = true;
  259. }
  260. }
  261. // Reseting m_HrefInfos array if there is any change in text
  262. void Reset_m_HrefInfos() {
  263. previousText = text;
  264. m_HrefInfos.Clear();
  265. isCreating_m_HrefInfos = true;
  266. }
  267. /// <summary>
  268. /// Finally, the output text hyperlinks get parsed
  269. /// </summary>
  270. /// <returns></returns>
  271. protected string GetOutputText() {
  272. s_TextBuilder.Length = 0;
  273. indexText = 0;
  274. fixedString = text;
  275. if (iconList != null && iconList.Count > 0) {
  276. for (int i = 0; i < iconList.Count; i++) {
  277. if (!string.IsNullOrEmpty(iconList[i].name)) {
  278. fixedString = fixedString.Replace(iconList[i].name,
  279. "<quad name=" + iconList[i].name + " size=" + fontSize + " width=1 />");
  280. }
  281. }
  282. }
  283. count = 0;
  284. href_matches = s_HrefRegex.Matches(fixedString);
  285. if (href_matches != null && href_matches.Count > 0) {
  286. for (int i = 0; i < href_matches.Count; i++) {
  287. s_TextBuilder.Append(fixedString.Substring(indexText, href_matches[i].Index - indexText));
  288. s_TextBuilder.Append("<color=" + hyperlinkColor + ">"); // Hyperlink color
  289. var group = href_matches[i].Groups[1];
  290. if (isCreating_m_HrefInfos) {
  291. HrefInfo hrefInfo = new() {
  292. // Hyperlinks in text starting index
  293. startIndex = (usesNewRendering ? s_TextBuilder.Length : s_TextBuilder.Length * 4),
  294. endIndex = (usesNewRendering ? (s_TextBuilder.Length + href_matches[i].Groups[2].Length - 1) : (s_TextBuilder.Length + href_matches[i].Groups[2].Length - 1) * 4 + 3),
  295. name = group.Value
  296. };
  297. m_HrefInfos.Add(hrefInfo);
  298. } else {
  299. if (count <= m_HrefInfos.Count - 1) {
  300. // Hyperlinks in text starting index
  301. m_HrefInfos[count].startIndex = (usesNewRendering ? s_TextBuilder.Length : s_TextBuilder.Length * 4);
  302. m_HrefInfos[count].endIndex = (usesNewRendering ? (s_TextBuilder.Length + href_matches[i].Groups[2].Length - 1) : (s_TextBuilder.Length + href_matches[i].Groups[2].Length - 1) * 4 + 3);
  303. count++;
  304. }
  305. }
  306. s_TextBuilder.Append(href_matches[i].Groups[2].Value);
  307. s_TextBuilder.Append("</color>");
  308. indexText = href_matches[i].Index + href_matches[i].Length;
  309. }
  310. }
  311. // we should create array only once or if there is any change in the text
  312. if (isCreating_m_HrefInfos)
  313. isCreating_m_HrefInfos = false;
  314. s_TextBuilder.Append(fixedString.Substring(indexText, fixedString.Length - indexText));
  315. m_OutputText = s_TextBuilder.ToString();
  316. m_ImagesVertexIndex.Clear();
  317. matches = s_Regex.Matches(m_OutputText);
  318. #if UNITY_2019_1_OR_NEWER
  319. href_matches = s_HrefRegex.Matches(m_OutputText);
  320. indexes.Clear();
  321. for (int r = 0; r < matches.Count; r++) {
  322. indexes.Add(matches[r].Index);
  323. }
  324. #endif
  325. if (matches != null && matches.Count > 0) {
  326. for (int i = 0; i < matches.Count; i++) {
  327. picIndex = matches[i].Index;
  328. #if UNITY_2019_1_OR_NEWER
  329. if (usesNewRendering) {
  330. charactersRemoved = 0;
  331. removeCharacters = remove_Regex.Matches(m_OutputText);
  332. for (int r = 0; r < removeCharacters.Count; r++) {
  333. if (removeCharacters[r].Index < picIndex && !indexes.Contains(removeCharacters[r].Index)) {
  334. charactersRemoved += removeCharacters[r].Length;
  335. }
  336. }
  337. for (int r = 0; r < i; r++) {
  338. charactersRemoved += (matches[r].Length - 1);
  339. }
  340. picIndex -= charactersRemoved;
  341. }
  342. #endif
  343. vertIndex = picIndex * 4 + 3;
  344. m_ImagesVertexIndex.Add(vertIndex);
  345. }
  346. }
  347. #if UNITY_2019_1_OR_NEWER
  348. if (usesNewRendering) {
  349. if (m_HrefInfos != null && m_HrefInfos.Count > 0) {
  350. for (int i = 0; i < m_HrefInfos.Count; i++) {
  351. startCharactersRemoved = 0;
  352. endCharactersRemoved = 0;
  353. removeCharacters = remove_Regex.Matches(m_OutputText);
  354. for (int r = 0; r < removeCharacters.Count; r++) {
  355. if (removeCharacters[r].Index < m_HrefInfos[i].startIndex && !indexes.Contains(removeCharacters[r].Index)) {
  356. startCharactersRemoved += removeCharacters[r].Length;
  357. } else if (removeCharacters[r].Index < m_HrefInfos[i].startIndex && indexes.Contains(removeCharacters[r].Index)) {
  358. startCharactersRemoved += removeCharacters[r].Length - 1;
  359. }
  360. if (removeCharacters[r].Index < m_HrefInfos[i].endIndex && !indexes.Contains(removeCharacters[r].Index)) {
  361. endCharactersRemoved += removeCharacters[r].Length;
  362. } else if (removeCharacters[r].Index < m_HrefInfos[i].endIndex && indexes.Contains(removeCharacters[r].Index)) {
  363. endCharactersRemoved += removeCharacters[r].Length - 1;
  364. }
  365. }
  366. m_HrefInfos[i].startIndex -= startCharactersRemoved;
  367. m_HrefInfos[i].startIndex = m_HrefInfos[i].startIndex * 4;
  368. m_HrefInfos[i].endIndex -= endCharactersRemoved;
  369. m_HrefInfos[i].endIndex = m_HrefInfos[i].endIndex * 4 + 3;
  370. }
  371. }
  372. }
  373. #endif
  374. return m_OutputText;
  375. }
  376. // Process href links to open them as a url, can override this function for custom functionality
  377. public virtual void OnHrefClick(string hrefName) {
  378. Application.OpenURL(hrefName);
  379. // Debug.Log(hrefName);
  380. }
  381. /// UNITY METHODS ///
  382. protected override void OnPopulateMesh(VertexHelper toFill) {
  383. originalText = m_Text;
  384. m_Text = GetOutputText();
  385. base.OnPopulateMesh(toFill);
  386. m_DisableFontTextureRebuiltCallback = true;
  387. m_Text = originalText;
  388. positions.Clear();
  389. vert = new UIVertex();
  390. for (int i = 0; i < m_ImagesVertexIndex.Count; i++) {
  391. int endIndex = m_ImagesVertexIndex[i];
  392. if (endIndex < toFill.currentVertCount) {
  393. toFill.PopulateUIVertex(ref vert, endIndex);
  394. positions.Add(new Vector2((vert.position.x + fontSize / 2), (vert.position.y + fontSize / 2)) + imageOffset);
  395. // Erase the lower left corner of the black specks
  396. toFill.PopulateUIVertex(ref vert, endIndex - 3);
  397. Vector3 pos = vert.position;
  398. for (int j = endIndex, m = endIndex - 3; j > m; j--) {
  399. toFill.PopulateUIVertex(ref vert, endIndex);
  400. vert.position = pos;
  401. toFill.SetUIVertex(vert, j);
  402. }
  403. }
  404. //StartCoroutine(ClearUpPunctuationMode(this)); // 避首尾标点添加的
  405. }
  406. // Hyperlinks surround processing box
  407. for (int h = 0; h < m_HrefInfos.Count; h++) {
  408. m_HrefInfos[h].boxes.Clear();
  409. if (m_HrefInfos[h].startIndex >= toFill.currentVertCount) {
  410. continue;
  411. }
  412. // Hyperlink inside the text is added to surround the vertex index coordinate frame
  413. toFill.PopulateUIVertex(ref vert, m_HrefInfos[h].startIndex);
  414. Vector3 pos = vert.position;
  415. Bounds bounds = new(pos, Vector3.zero);
  416. for (int i = m_HrefInfos[h].startIndex, m = m_HrefInfos[h].endIndex; i < m; i++) {
  417. if (i >= toFill.currentVertCount) {
  418. break;
  419. }
  420. toFill.PopulateUIVertex(ref vert, i);
  421. pos = vert.position;
  422. // Wrap re-add surround frame
  423. if (pos.x < bounds.min.x) {
  424. m_HrefInfos[h].boxes.Add(new Rect(bounds.min, bounds.size));
  425. bounds = new Bounds(pos, Vector3.zero);
  426. } else {
  427. bounds.Encapsulate(pos); // Extended enclosed box
  428. }
  429. }
  430. m_HrefInfos[h].boxes.Add(new Rect(bounds.min, bounds.size));
  431. }
  432. // Update the quad images
  433. updateQuad = true;
  434. m_DisableFontTextureRebuiltCallback = false;
  435. }
  436. /// <summary>
  437. /// Click event is detected whether to click a hyperlink text
  438. /// </summary>
  439. /// <param name="eventData"></param>
  440. public void OnPointerClick(PointerEventData eventData) {
  441. RectTransformUtility.ScreenPointToLocalPointInRectangle(
  442. rectTransform, eventData.position, eventData.pressEventCamera, out lp);
  443. for (int h = 0; h < m_HrefInfos.Count; h++) {
  444. for (int i = 0; i < m_HrefInfos[h].boxes.Count; ++i) {
  445. if (m_HrefInfos[h].boxes[i].Contains(lp)) {
  446. m_OnHrefClick.Invoke(m_HrefInfos[h].name);
  447. return;
  448. }
  449. }
  450. }
  451. }
  452. public void OnPointerEnter(PointerEventData eventData) {
  453. //do your stuff when highlighted
  454. selected = true;
  455. //if (m_ImagesPool.Count >= 1) {
  456. // for (int i = 0; i < m_ImagesPool.Count; i++) {
  457. // if (button != null && button.isActiveAndEnabled) {
  458. // m_ImagesPool[i].color = button.colors.highlightedColor;
  459. // }
  460. // }
  461. //}
  462. }
  463. public void OnPointerExit(PointerEventData eventData) {
  464. //do your stuff when highlighted
  465. selected = false;
  466. //if (m_ImagesPool.Count >= 1) {
  467. // for (int i = 0; i < m_ImagesPool.Count; i++) {
  468. // if (button != null && button.isActiveAndEnabled) {
  469. // m_ImagesPool[i].color = button.colors.normalColor;
  470. // } else {
  471. // m_ImagesPool[i].color = color;
  472. // }
  473. // }
  474. //}
  475. }
  476. public void OnSelect(BaseEventData eventData) {
  477. //do your stuff when selected
  478. selected = true;
  479. if (m_ImagesPool.Count >= 1) {
  480. for (int i = 0; i < m_ImagesPool.Count; i++) {
  481. if (button != null && button.isActiveAndEnabled) {
  482. m_ImagesPool[i].color = button.colors.highlightedColor;
  483. }
  484. }
  485. }
  486. }
  487. public void OnDeselect(BaseEventData eventData) {
  488. //do your stuff when selected
  489. selected = false;
  490. if (m_ImagesPool.Count >= 1) {
  491. for (int i = 0; i < m_ImagesPool.Count; i++) {
  492. if (button != null && button.isActiveAndEnabled) {
  493. m_ImagesPool[i].color = button.colors.normalColor;
  494. }
  495. }
  496. }
  497. }
  498. public override void SetVerticesDirty() {
  499. base.SetVerticesDirty();
  500. // Update the quad images
  501. updateQuad = true;
  502. }
  503. #if UNITY_EDITOR
  504. protected override void OnValidate() {
  505. base.OnValidate();
  506. // Update the quad images
  507. updateQuad = true;
  508. }
  509. #endif
  510. protected override void OnEnable() {
  511. #if UNITY_2019_1_OR_NEWER
  512. // Here is the hack to see if Unity is using the new rendering system for text
  513. usesNewRendering = false;
  514. if (Application.unityVersion.StartsWith("2019.1.")) {
  515. if (!Char.IsDigit(Application.unityVersion[8])) {
  516. int number = Convert.ToInt32(Application.unityVersion[7].ToString());
  517. if (number > 4) {
  518. usesNewRendering = true;
  519. }
  520. } else {
  521. usesNewRendering = true;
  522. }
  523. } else {
  524. usesNewRendering = true;
  525. }
  526. #endif
  527. base.OnEnable();
  528. supportRichText = true;
  529. alignByGeometry = true;
  530. // Enable images on TextPic disable
  531. if (m_ImagesPool.Count >= 1) {
  532. for (int i = 0; i < m_ImagesPool.Count; i++) {
  533. if (m_ImagesPool[i] != null) {
  534. m_ImagesPool[i].enabled = true;
  535. }
  536. }
  537. }
  538. // Update the quads on re-enable
  539. updateQuad = true;
  540. this.onHrefClick.AddListener(OnHrefClick);
  541. }
  542. protected override void OnDisable() {
  543. base.OnDisable();
  544. // Disable images on TextPic disable
  545. if (m_ImagesPool.Count >= 1) {
  546. for (int i = 0; i < m_ImagesPool.Count; i++) {
  547. if (m_ImagesPool[i] != null) {
  548. m_ImagesPool[i].enabled = false;
  549. }
  550. }
  551. }
  552. this.onHrefClick.RemoveListener(OnHrefClick);
  553. }
  554. new void Start() {
  555. button = GetComponent<Button>();
  556. ResetIcons();
  557. }
  558. void LateUpdate() {
  559. // Reset the hrefs if text is changed
  560. if (previousText != text) {
  561. Reset_m_HrefInfos();
  562. // Update the quad on text change
  563. updateQuad = true;
  564. }
  565. // Need to lock to remove images properly
  566. lock (thisLock) {
  567. // Can only update the images when it is not in a rebuild, this prevents the error
  568. if (updateQuad) {
  569. UpdateQuadImage();
  570. updateQuad = false;
  571. }
  572. // Destroy any images that are not in use
  573. if (clearImages) {
  574. for (int i = 0; i < culled_ImagesPool.Count; i++) {
  575. DestroyImmediate(culled_ImagesPool[i]);
  576. }
  577. culled_ImagesPool.Clear();
  578. clearImages = false;
  579. }
  580. }
  581. }
  582. }