No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMP_TextInfo.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace TMPro
  6. {
  7. /// <summary>
  8. /// Class which contains information about every element contained within the text object.
  9. /// </summary>
  10. [Serializable]
  11. public class TMP_TextInfo
  12. {
  13. internal static Vector2 k_InfinityVectorPositive = new Vector2(32767, 32767);
  14. internal static Vector2 k_InfinityVectorNegative = new Vector2(-32767, -32767);
  15. public TMP_Text textComponent;
  16. public int characterCount;
  17. public int spriteCount;
  18. public int spaceCount;
  19. public int wordCount;
  20. public int linkCount;
  21. public int lineCount;
  22. public int pageCount;
  23. public int materialCount;
  24. public TMP_CharacterInfo[] characterInfo;
  25. public TMP_WordInfo[] wordInfo;
  26. public TMP_LinkInfo[] linkInfo;
  27. public TMP_LineInfo[] lineInfo;
  28. public TMP_PageInfo[] pageInfo;
  29. public TMP_MeshInfo[] meshInfo;
  30. private TMP_MeshInfo[] m_CachedMeshInfo;
  31. // Default Constructor
  32. public TMP_TextInfo()
  33. {
  34. characterInfo = new TMP_CharacterInfo[8];
  35. wordInfo = new TMP_WordInfo[16];
  36. linkInfo = new TMP_LinkInfo[0];
  37. lineInfo = new TMP_LineInfo[2];
  38. pageInfo = new TMP_PageInfo[4];
  39. meshInfo = new TMP_MeshInfo[1];
  40. }
  41. internal TMP_TextInfo(int characterCount)
  42. {
  43. characterInfo = new TMP_CharacterInfo[characterCount];
  44. wordInfo = new TMP_WordInfo[16];
  45. linkInfo = new TMP_LinkInfo[0];
  46. lineInfo = new TMP_LineInfo[2];
  47. pageInfo = new TMP_PageInfo[4];
  48. meshInfo = new TMP_MeshInfo[1];
  49. }
  50. public TMP_TextInfo(TMP_Text textComponent)
  51. {
  52. this.textComponent = textComponent;
  53. characterInfo = new TMP_CharacterInfo[8];
  54. wordInfo = new TMP_WordInfo[4];
  55. linkInfo = new TMP_LinkInfo[0];
  56. lineInfo = new TMP_LineInfo[2];
  57. pageInfo = new TMP_PageInfo[4];
  58. meshInfo = new TMP_MeshInfo[1];
  59. meshInfo[0].mesh = textComponent.mesh;
  60. materialCount = 1;
  61. }
  62. /// <summary>
  63. /// Function to clear the counters of the text object.
  64. /// </summary>
  65. public void Clear()
  66. {
  67. characterCount = 0;
  68. spaceCount = 0;
  69. wordCount = 0;
  70. linkCount = 0;
  71. lineCount = 0;
  72. pageCount = 0;
  73. spriteCount = 0;
  74. for (int i = 0; i < this.meshInfo.Length; i++)
  75. {
  76. this.meshInfo[i].vertexCount = 0;
  77. }
  78. }
  79. /// <summary>
  80. ///
  81. /// </summary>
  82. internal void ClearAllData()
  83. {
  84. characterCount = 0;
  85. spaceCount = 0;
  86. wordCount = 0;
  87. linkCount = 0;
  88. lineCount = 0;
  89. pageCount = 0;
  90. spriteCount = 0;
  91. this.characterInfo = new TMP_CharacterInfo[4];
  92. this.wordInfo = new TMP_WordInfo[1];
  93. this.lineInfo = new TMP_LineInfo[1];
  94. this.pageInfo = new TMP_PageInfo[1];
  95. this.linkInfo = new TMP_LinkInfo[0];
  96. materialCount = 0;
  97. this.meshInfo = new TMP_MeshInfo[1];
  98. }
  99. /// <summary>
  100. /// Function to clear the content of the MeshInfo array while preserving the Triangles, Normals and Tangents.
  101. /// </summary>
  102. public void ClearMeshInfo(bool updateMesh)
  103. {
  104. for (int i = 0; i < this.meshInfo.Length; i++)
  105. this.meshInfo[i].Clear(updateMesh);
  106. }
  107. /// <summary>
  108. /// Function to clear the content of all the MeshInfo arrays while preserving their Triangles, Normals and Tangents.
  109. /// </summary>
  110. public void ClearAllMeshInfo()
  111. {
  112. for (int i = 0; i < this.meshInfo.Length; i++)
  113. this.meshInfo[i].Clear(true);
  114. }
  115. /// <summary>
  116. ///
  117. /// </summary>
  118. public void ResetVertexLayout(bool isVolumetric)
  119. {
  120. for (int i = 0; i < this.meshInfo.Length; i++)
  121. this.meshInfo[i].ResizeMeshInfo(0, isVolumetric);
  122. }
  123. /// <summary>
  124. /// Function used to mark unused vertices as degenerate.
  125. /// </summary>
  126. /// <param name="materials"></param>
  127. public void ClearUnusedVertices(MaterialReference[] materials)
  128. {
  129. for (int i = 0; i < meshInfo.Length; i++)
  130. {
  131. int start = 0; // materials[i].referenceCount * 4;
  132. meshInfo[i].ClearUnusedVertices(start);
  133. }
  134. }
  135. /// <summary>
  136. /// Function to clear and initialize the lineInfo array.
  137. /// </summary>
  138. public void ClearLineInfo()
  139. {
  140. if (this.lineInfo == null)
  141. this.lineInfo = new TMP_LineInfo[2];
  142. int length = this.lineInfo.Length;
  143. for (int i = 0; i < length; i++)
  144. {
  145. this.lineInfo[i].characterCount = 0;
  146. this.lineInfo[i].spaceCount = 0;
  147. this.lineInfo[i].wordCount = 0;
  148. this.lineInfo[i].controlCharacterCount = 0;
  149. this.lineInfo[i].width = 0;
  150. this.lineInfo[i].ascender = k_InfinityVectorNegative.x;
  151. this.lineInfo[i].descender = k_InfinityVectorPositive.x;
  152. this.lineInfo[i].marginLeft = 0;
  153. this.lineInfo[i].marginRight = 0;
  154. this.lineInfo[i].lineExtents.min = k_InfinityVectorPositive;
  155. this.lineInfo[i].lineExtents.max = k_InfinityVectorNegative;
  156. this.lineInfo[i].maxAdvance = 0;
  157. //this.lineInfo[i].maxScale = 0;
  158. }
  159. }
  160. internal void ClearPageInfo()
  161. {
  162. if (this.pageInfo == null)
  163. this.pageInfo = new TMP_PageInfo[2];
  164. int length = this.pageInfo.Length;
  165. for (int i = 0; i < length; i++)
  166. {
  167. this.pageInfo[i].firstCharacterIndex = 0;
  168. this.pageInfo[i].lastCharacterIndex = 0;
  169. this.pageInfo[i].ascender = -32767;
  170. this.pageInfo[i].baseLine = 0;
  171. this.pageInfo[i].descender = 32767;
  172. }
  173. }
  174. /// <summary>
  175. /// Function to copy the MeshInfo Arrays and their primary vertex data content.
  176. /// </summary>
  177. /// <returns>A copy of the MeshInfo[]</returns>
  178. public TMP_MeshInfo[] CopyMeshInfoVertexData()
  179. {
  180. if (m_CachedMeshInfo == null || m_CachedMeshInfo.Length != meshInfo.Length)
  181. {
  182. m_CachedMeshInfo = new TMP_MeshInfo[meshInfo.Length];
  183. // Initialize all the vertex data arrays
  184. for (int i = 0; i < m_CachedMeshInfo.Length; i++)
  185. {
  186. int length = meshInfo[i].vertices.Length;
  187. m_CachedMeshInfo[i].vertices = new Vector3[length];
  188. m_CachedMeshInfo[i].uvs0 = new Vector2[length];
  189. m_CachedMeshInfo[i].uvs2 = new Vector2[length];
  190. m_CachedMeshInfo[i].colors32 = new Color32[length];
  191. //m_CachedMeshInfo[i].normals = new Vector3[length];
  192. //m_CachedMeshInfo[i].tangents = new Vector4[length];
  193. //m_CachedMeshInfo[i].triangles = new int[meshInfo[i].triangles.Length];
  194. }
  195. }
  196. for (int i = 0; i < m_CachedMeshInfo.Length; i++)
  197. {
  198. int length = meshInfo[i].vertices.Length;
  199. if (m_CachedMeshInfo[i].vertices.Length != length)
  200. {
  201. m_CachedMeshInfo[i].vertices = new Vector3[length];
  202. m_CachedMeshInfo[i].uvs0 = new Vector2[length];
  203. m_CachedMeshInfo[i].uvs2 = new Vector2[length];
  204. m_CachedMeshInfo[i].colors32 = new Color32[length];
  205. //m_CachedMeshInfo[i].normals = new Vector3[length];
  206. //m_CachedMeshInfo[i].tangents = new Vector4[length];
  207. //m_CachedMeshInfo[i].triangles = new int[meshInfo[i].triangles.Length];
  208. }
  209. // Only copy the primary vertex data
  210. Array.Copy(meshInfo[i].vertices, m_CachedMeshInfo[i].vertices, length);
  211. Array.Copy(meshInfo[i].uvs0, m_CachedMeshInfo[i].uvs0, length);
  212. Array.Copy(meshInfo[i].uvs2, m_CachedMeshInfo[i].uvs2, length);
  213. Array.Copy(meshInfo[i].colors32, m_CachedMeshInfo[i].colors32, length);
  214. //Array.Copy(meshInfo[i].normals, m_CachedMeshInfo[i].normals, length);
  215. //Array.Copy(meshInfo[i].tangents, m_CachedMeshInfo[i].tangents, length);
  216. //Array.Copy(meshInfo[i].triangles, m_CachedMeshInfo[i].triangles, meshInfo[i].triangles.Length);
  217. }
  218. return m_CachedMeshInfo;
  219. }
  220. /// <summary>
  221. /// Function to resize any of the structure contained in the TMP_TextInfo class.
  222. /// </summary>
  223. /// <typeparam name="T"></typeparam>
  224. /// <param name="array"></param>
  225. /// <param name="size"></param>
  226. public static void Resize<T> (ref T[] array, int size)
  227. {
  228. // Allocated to the next power of two
  229. int newSize = size > 1024 ? size + 256 : Mathf.NextPowerOfTwo(size);
  230. Array.Resize(ref array, newSize);
  231. }
  232. /// <summary>
  233. /// Function to resize any of the structure contained in the TMP_TextInfo class.
  234. /// </summary>
  235. /// <typeparam name="T"></typeparam>
  236. /// <param name="array"></param>
  237. /// <param name="size"></param>
  238. /// <param name="isFixedSize"></param>
  239. public static void Resize<T>(ref T[] array, int size, bool isBlockAllocated)
  240. {
  241. if (isBlockAllocated) size = size > 1024 ? size + 256 : Mathf.NextPowerOfTwo(size);
  242. if (size == array.Length) return;
  243. //Debug.Log("Resizing TextInfo from [" + array.Length + "] to [" + size + "]");
  244. Array.Resize(ref array, size);
  245. }
  246. }
  247. }