Advertisement
Guest User

Untitled

a guest
May 5th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. // Usage: icon name in text is replaced by image
  8. public class TextPic : Text
  9. {
  10. [System.Serializable]
  11. public struct IconName
  12. {
  13. public string name;
  14. public Sprite sprite;
  15. }
  16.  
  17. /// <summary>
  18. /// Regular expression to replace
  19. /// </summary>
  20. static readonly Regex s_Regex =
  21. new Regex(@"<quad name=(.+?) size=(\d*\.?\d+%?) width=(\d*\.?\d+%?) />", RegexOptions.Singleline);
  22.  
  23. public IconName[] inspectorIconList;
  24.  
  25. // Scale image by x, y
  26. [SerializeField]
  27. public Vector2 imageScalingFactor = Vector2.one;
  28.  
  29. // Offset image by x, y
  30. [SerializeField]
  31. public Vector2 imageOffset = Vector2.zero;
  32.  
  33. // Pad image by x
  34. public float imagePad;
  35.  
  36. /// <summary>
  37. /// Image Pool
  38. /// </summary>
  39. readonly List<Image> m_ImagesPool = new List<Image>();
  40.  
  41. /// <summary>
  42. /// Vertex Index
  43. /// </summary>
  44. readonly List<int> m_ImagesVertexIndex = new List<int>();
  45.  
  46. /// <summary>
  47. /// After parsing the final text
  48. /// </summary>
  49. string m_OutputText;
  50.  
  51. Dictionary<string, Sprite> iconList = new Dictionary<string, Sprite>();
  52. List<Vector2> positions = new List<Vector2>();
  53.  
  54. public override void SetVerticesDirty()
  55. {
  56. base.SetVerticesDirty();
  57. UpdateQuadImage();
  58. }
  59.  
  60. protected override void OnPopulateMesh(VertexHelper toFill)
  61. {
  62. var orignText = m_Text;
  63. m_Text = m_OutputText;
  64. base.OnPopulateMesh(toFill);
  65. m_Text = orignText;
  66. positions.Clear();
  67.  
  68. var vert = new UIVertex();
  69. for (var i = 0; i < m_ImagesVertexIndex.Count; i++)
  70. {
  71. var endIndex = m_ImagesVertexIndex[i];
  72. var rt = m_ImagesPool[i].rectTransform;
  73. var size = rt.sizeDelta;
  74. if (endIndex < toFill.currentVertCount)
  75. {
  76. toFill.PopulateUIVertex(ref vert, endIndex);
  77. var position = new Vector2((vert.position.x + size.x / 2), (vert.position.y + size.y / 2));
  78. position += imageOffset;
  79. if (i > 0)
  80. {
  81. var padVector = new Vector2(imagePad * i, 0);
  82. position += padVector;
  83. }
  84. positions.Add(position);
  85.  
  86. // Erase the lower left corner of the black specks
  87. var end = endIndex - 3;
  88. toFill.PopulateUIVertex(ref vert, end);
  89. var pos = vert.position;
  90. for (int j = endIndex; j > end; j--)
  91. {
  92. toFill.PopulateUIVertex(ref vert, endIndex);
  93. vert.position = pos;
  94. toFill.SetUIVertex(vert, j);
  95. }
  96. }
  97. }
  98.  
  99. if (m_ImagesVertexIndex.Count != 0)
  100. {
  101. m_ImagesVertexIndex.Clear();
  102. }
  103. }
  104.  
  105. /**
  106. * Unity Inspector cant display Dictionary vars,
  107. * so we use this little hack to setup the iconList
  108. */
  109. new void Start()
  110. {
  111. if (inspectorIconList != null)
  112. {
  113. foreach (IconName icon in inspectorIconList)
  114. {
  115. iconList.Add(icon.name, icon.sprite);
  116. }
  117. }
  118. }
  119.  
  120. void UpdateQuadImage()
  121. {
  122. #if UNITY_EDITOR
  123. if (UnityEditor.PrefabUtility.GetPrefabType(this) == UnityEditor.PrefabType.Prefab)
  124. {
  125. return;
  126. }
  127. #endif
  128.  
  129. m_OutputText = GetOutputText();
  130. m_ImagesVertexIndex.Clear();
  131. foreach (Match match in s_Regex.Matches(m_OutputText))
  132. {
  133. var picIndex = match.Index;
  134. var endIndex = picIndex * 4 + 3;
  135. m_ImagesVertexIndex.Add(endIndex);
  136.  
  137. m_ImagesPool.RemoveAll(image => image == null);
  138. if (m_ImagesPool.Count == 0)
  139. {
  140. GetComponentsInChildren<Image>(m_ImagesPool);
  141. }
  142. if (m_ImagesVertexIndex.Count > m_ImagesPool.Count)
  143. {
  144. var resources = new DefaultControls.Resources();
  145. var go = DefaultControls.CreateImage(resources);
  146. go.layer = gameObject.layer;
  147. var rt = go.transform as RectTransform;
  148. if (rt)
  149. {
  150. rt.SetParent(rectTransform);
  151. rt.localPosition = Vector3.zero;
  152. rt.localRotation = Quaternion.identity;
  153. rt.localScale = Vector3.one;
  154. }
  155. m_ImagesPool.Add(go.GetComponent<Image>());
  156. }
  157.  
  158. var spriteName = match.Groups[1].Value;
  159. var img = m_ImagesPool[m_ImagesVertexIndex.Count - 1];
  160. if (img.sprite == null || img.sprite.name != spriteName)
  161. {
  162. if (inspectorIconList != null && inspectorIconList.Length > 0)
  163. {
  164. foreach (IconName icon in inspectorIconList)
  165. {
  166. if (icon.name == spriteName)
  167. {
  168. img.sprite = icon.sprite;
  169. break;
  170. }
  171. }
  172. }
  173. }
  174. img.rectTransform.sizeDelta = new Vector2(fontSize * imageScalingFactor.x, fontSize * imageScalingFactor.y);
  175. img.enabled = true;
  176. if (positions.Count == m_ImagesPool.Count)
  177. {
  178. img.rectTransform.anchoredPosition = positions[m_ImagesVertexIndex.Count - 1];
  179. }
  180. }
  181.  
  182. for (var i = m_ImagesVertexIndex.Count; i < m_ImagesPool.Count; i++)
  183. {
  184. if (m_ImagesPool[i])
  185. {
  186. m_ImagesPool[i].enabled = false;
  187. }
  188. }
  189. }
  190.  
  191. string GetOutputText()
  192. {
  193. var fixedString = this.text;
  194. if (inspectorIconList != null && inspectorIconList.Length > 0)
  195. {
  196. foreach (IconName icon in inspectorIconList)
  197. {
  198. if (!string.IsNullOrEmpty(icon.name))
  199. {
  200. fixedString = fixedString.Replace(icon.name, "<quad name=" + icon.name + " size=" + fontSize + " width=1 />");
  201. }
  202. }
  203. }
  204. return fixedString;
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement