UI_TextComplex.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ------------------- Code Monkey -------------------
  3. Thank you for downloading the Code Monkey Utilities
  4. I hope you find them useful in your projects
  5. If you have any questions use the contact form
  6. Cheers!
  7. unitycodemonkey.com
  8. --------------------------------------------------
  9. */
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. namespace CodeMonkey.Utils {
  13. /*
  14. * Displays text with icons in between the text
  15. * */
  16. public class UI_TextComplex {
  17. private static Transform GetCanvasTransform() {
  18. return UtilsClass.GetCanvasTransform();
  19. }
  20. public struct Icon {
  21. public Sprite sprite;
  22. public Vector2 size;
  23. public Color color;
  24. public Icon(Sprite sprite, Vector2 size, Color? color = null) {
  25. this.sprite = sprite;
  26. this.size = size;
  27. if (color == null) {
  28. this.color = Color.white;
  29. } else {
  30. this.color = (Color) color;
  31. }
  32. }
  33. }
  34. public GameObject gameObject;
  35. private Transform transform;
  36. private RectTransform rectTransform;
  37. // iconChar prepends the iconArr index;
  38. // Example using iconChar '#':
  39. // test #0 asdf
  40. // Displays "test [iconArr[0]] asdf"
  41. public UI_TextComplex(Transform parent, Vector2 anchoredPosition, int fontSize, char iconChar, string text, Icon[] iconArr, Font font) {
  42. SetupParent(parent, anchoredPosition);
  43. string tmp = text;
  44. float textPosition = 0f;
  45. while (tmp.IndexOf(iconChar) != -1) {
  46. string untilTmp = tmp.Substring(0, tmp.IndexOf(iconChar));
  47. string iconNumber = tmp.Substring(tmp.IndexOf(iconChar)+1);
  48. int indexOfSpaceAfterIconNumber = iconNumber.IndexOf(" ");
  49. if (indexOfSpaceAfterIconNumber != -1) {
  50. // Still has more space after iconNumber
  51. iconNumber = iconNumber.Substring(0, indexOfSpaceAfterIconNumber);
  52. } else {
  53. // No more space after iconNumber
  54. }
  55. tmp = tmp.Substring(tmp.IndexOf(iconChar+iconNumber) + (iconChar+iconNumber).Length);
  56. if (untilTmp.Trim() != "") {
  57. Text uiText = UtilsClass.DrawTextUI(untilTmp, transform, new Vector2(textPosition,0), fontSize, font);
  58. textPosition += uiText.preferredWidth;
  59. }
  60. // Draw Icon
  61. int iconIndex = UtilsClass.Parse_Int(iconNumber, 0);
  62. Icon icon = iconArr[iconIndex];
  63. UtilsClass.DrawSprite(icon.sprite, transform, new Vector2(textPosition + icon.size.x / 2f, 0), icon.size);
  64. textPosition += icon.size.x;
  65. }
  66. if (tmp.Trim() != "") {
  67. UtilsClass.DrawTextUI(tmp, transform, new Vector2(textPosition,0), fontSize, font);
  68. }
  69. }
  70. private void SetupParent(Transform parent, Vector2 anchoredPosition) {
  71. gameObject = new GameObject("UI_TextComplex", typeof(RectTransform));
  72. transform = gameObject.transform;
  73. rectTransform = gameObject.GetComponent<RectTransform>();
  74. rectTransform.SetParent(parent, false);
  75. rectTransform.sizeDelta = new Vector2(0, 0);
  76. rectTransform.anchorMin = new Vector2(0, .5f);
  77. rectTransform.anchorMax = new Vector2(0, .5f);
  78. rectTransform.pivot = new Vector2(0, .5f);
  79. rectTransform.anchoredPosition = anchoredPosition;
  80. }
  81. public void SetTextColor(Color color) {
  82. foreach (Transform trans in transform) {
  83. Text text = trans.GetComponent<Text>();
  84. if (text != null) {
  85. text.color = color;
  86. }
  87. }
  88. }
  89. public float GetTotalWidth() {
  90. float textPosition = 0f;
  91. foreach (Transform trans in transform) {
  92. Text text = trans.GetComponent<Text>();
  93. if (text != null) {
  94. textPosition += text.preferredWidth;
  95. }
  96. Image image = trans.GetComponent<Image>();
  97. if (image != null) {
  98. textPosition += image.GetComponent<RectTransform>().sizeDelta.x;
  99. }
  100. }
  101. return textPosition;
  102. }
  103. public float GetTotalHeight() {
  104. foreach (Transform trans in transform) {
  105. Text text = trans.GetComponent<Text>();
  106. if (text != null) {
  107. return text.preferredHeight;
  108. }
  109. }
  110. return 0f;
  111. }
  112. public void AddTextOutline(Color color, float size) {
  113. foreach (Transform textComplexTrans in transform) {
  114. if (textComplexTrans.GetComponent<Text>() != null) {
  115. Outline outline = textComplexTrans.gameObject.AddComponent<Outline>();
  116. outline.effectColor = color;
  117. outline.effectDistance = new Vector2(size, size);
  118. }
  119. }
  120. }
  121. public void SetAnchorMiddle() {
  122. rectTransform.anchorMin = new Vector2(.5f, .5f);
  123. rectTransform.anchorMax = new Vector2(.5f, .5f);
  124. }
  125. public void CenterOnPosition(Vector2 position) {
  126. rectTransform.anchoredPosition = position + new Vector2(-GetTotalWidth() / 2f, 0);
  127. }
  128. public void DestroySelf() {
  129. Object.Destroy(gameObject);
  130. }
  131. }
  132. }