TMP_TextInfo.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. private static Vector2 k_InfinityVectorPositive = new Vector2(32767, 32767);
  14. private 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. public TMP_TextInfo(TMP_Text textComponent)
  42. {
  43. this.textComponent = textComponent;
  44. characterInfo = new TMP_CharacterInfo[8];
  45. wordInfo = new TMP_WordInfo[4];
  46. linkInfo = new TMP_LinkInfo[0];
  47. lineInfo = new TMP_LineInfo[2];
  48. pageInfo = new TMP_PageInfo[4];
  49. meshInfo = new TMP_MeshInfo[1];
  50. meshInfo[0].mesh = textComponent.mesh;
  51. materialCount = 1;
  52. }
  53. /// <summary>
  54. /// Function to clear the counters of the text object.
  55. /// </summary>
  56. public void Clear()
  57. {
  58. characterCount = 0;
  59. spaceCount = 0;
  60. wordCount = 0;
  61. linkCount = 0;
  62. lineCount = 0;
  63. pageCount = 0;
  64. spriteCount = 0;
  65. for (int i = 0; i < this.meshInfo.Length; i++)
  66. {
  67. this.meshInfo[i].vertexCount = 0;
  68. }
  69. }
  70. /// <summary>
  71. /// Function to clear the content of the MeshInfo array while preserving the Triangles, Normals and Tangents.
  72. /// </summary>
  73. public void ClearMeshInfo(bool updateMesh)
  74. {
  75. for (int i = 0; i < this.meshInfo.Length; i++)
  76. this.meshInfo[i].Clear(updateMesh);
  77. }
  78. /// <summary>
  79. /// Function to clear the content of all the MeshInfo arrays while preserving their Triangles, Normals and Tangents.
  80. /// </summary>
  81. public void ClearAllMeshInfo()
  82. {
  83. for (int i = 0; i < this.meshInfo.Length; i++)
  84. this.meshInfo[i].Clear(true);
  85. }
  86. /// <summary>
  87. ///
  88. /// </summary>
  89. public void ResetVertexLayout(bool isVolumetric)
  90. {
  91. for (int i = 0; i < this.meshInfo.Length; i++)
  92. this.meshInfo[i].ResizeMeshInfo(0, isVolumetric);
  93. }
  94. /// <summary>
  95. /// Function used to mark unused vertices as degenerate.
  96. /// </summary>
  97. /// <param name="materials"></param>
  98. public void ClearUnusedVertices(MaterialReference[] materials)
  99. {
  100. for (int i = 0; i < meshInfo.Length; i++)
  101. {
  102. int start = 0; // materials[i].referenceCount * 4;
  103. meshInfo[i].ClearUnusedVertices(start);
  104. }
  105. }
  106. /// <summary>
  107. /// Function to clear and initialize the lineInfo array.
  108. /// </summary>
  109. public void ClearLineInfo()
  110. {
  111. if (this.lineInfo == null)
  112. this.lineInfo = new TMP_LineInfo[2];
  113. for (int i = 0; i < this.lineInfo.Length; i++)
  114. {
  115. this.lineInfo[i].characterCount = 0;
  116. this.lineInfo[i].spaceCount = 0;
  117. this.lineInfo[i].wordCount = 0;
  118. this.lineInfo[i].controlCharacterCount = 0;
  119. this.lineInfo[i].width = 0;
  120. this.lineInfo[i].ascender = k_InfinityVectorNegative.x;
  121. this.lineInfo[i].descender = k_InfinityVectorPositive.x;
  122. this.lineInfo[i].lineExtents.min = k_InfinityVectorPositive;
  123. this.lineInfo[i].lineExtents.max = k_InfinityVectorNegative;
  124. this.lineInfo[i].maxAdvance = 0;
  125. //this.lineInfo[i].maxScale = 0;
  126. }
  127. }
  128. /// <summary>
  129. /// Function to copy the MeshInfo Arrays and their primary vertex data content.
  130. /// </summary>
  131. /// <returns>A copy of the MeshInfo[]</returns>
  132. public TMP_MeshInfo[] CopyMeshInfoVertexData()
  133. {
  134. if (m_CachedMeshInfo == null || m_CachedMeshInfo.Length != meshInfo.Length)
  135. {
  136. m_CachedMeshInfo = new TMP_MeshInfo[meshInfo.Length];
  137. // Initialize all the vertex data arrays
  138. for (int i = 0; i < m_CachedMeshInfo.Length; i++)
  139. {
  140. int length = meshInfo[i].vertices.Length;
  141. m_CachedMeshInfo[i].vertices = new Vector3[length];
  142. m_CachedMeshInfo[i].uvs0 = new Vector2[length];
  143. m_CachedMeshInfo[i].uvs2 = new Vector2[length];
  144. m_CachedMeshInfo[i].colors32 = new Color32[length];
  145. //m_CachedMeshInfo[i].normals = new Vector3[length];
  146. //m_CachedMeshInfo[i].tangents = new Vector4[length];
  147. //m_CachedMeshInfo[i].triangles = new int[meshInfo[i].triangles.Length];
  148. }
  149. }
  150. for (int i = 0; i < m_CachedMeshInfo.Length; i++)
  151. {
  152. int length = meshInfo[i].vertices.Length;
  153. if (m_CachedMeshInfo[i].vertices.Length != length)
  154. {
  155. m_CachedMeshInfo[i].vertices = new Vector3[length];
  156. m_CachedMeshInfo[i].uvs0 = new Vector2[length];
  157. m_CachedMeshInfo[i].uvs2 = new Vector2[length];
  158. m_CachedMeshInfo[i].colors32 = new Color32[length];
  159. //m_CachedMeshInfo[i].normals = new Vector3[length];
  160. //m_CachedMeshInfo[i].tangents = new Vector4[length];
  161. //m_CachedMeshInfo[i].triangles = new int[meshInfo[i].triangles.Length];
  162. }
  163. // Only copy the primary vertex data
  164. Array.Copy(meshInfo[i].vertices, m_CachedMeshInfo[i].vertices, length);
  165. Array.Copy(meshInfo[i].uvs0, m_CachedMeshInfo[i].uvs0, length);
  166. Array.Copy(meshInfo[i].uvs2, m_CachedMeshInfo[i].uvs2, length);
  167. Array.Copy(meshInfo[i].colors32, m_CachedMeshInfo[i].colors32, length);
  168. //Array.Copy(meshInfo[i].normals, m_CachedMeshInfo[i].normals, length);
  169. //Array.Copy(meshInfo[i].tangents, m_CachedMeshInfo[i].tangents, length);
  170. //Array.Copy(meshInfo[i].triangles, m_CachedMeshInfo[i].triangles, meshInfo[i].triangles.Length);
  171. }
  172. return m_CachedMeshInfo;
  173. }
  174. /// <summary>
  175. /// Function to resize any of the structure contained in the TMP_TextInfo class.
  176. /// </summary>
  177. /// <typeparam name="T"></typeparam>
  178. /// <param name="array"></param>
  179. /// <param name="size"></param>
  180. public static void Resize<T> (ref T[] array, int size)
  181. {
  182. // Allocated to the next power of two
  183. int newSize = size > 1024 ? size + 256 : Mathf.NextPowerOfTwo(size);
  184. Array.Resize(ref array, newSize);
  185. }
  186. /// <summary>
  187. /// Function to resize any of the structure contained in the TMP_TextInfo class.
  188. /// </summary>
  189. /// <typeparam name="T"></typeparam>
  190. /// <param name="array"></param>
  191. /// <param name="size"></param>
  192. /// <param name="isFixedSize"></param>
  193. public static void Resize<T>(ref T[] array, int size, bool isBlockAllocated)
  194. {
  195. //if (size <= array.Length) return;
  196. if (isBlockAllocated) size = size > 1024 ? size + 256 : Mathf.NextPowerOfTwo(size);
  197. if (size == array.Length) return;
  198. Array.Resize(ref array, size);
  199. }
  200. }
  201. }