TMP_TextInfoDebugTool.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class TMP_TextInfoDebugTool : MonoBehaviour
  6. {
  7. // Since this script is used for debugging, we exclude it from builds.
  8. // TODO: Rework this script to make it into an editor utility.
  9. #if UNITY_EDITOR
  10. public bool ShowCharacters;
  11. public bool ShowWords;
  12. public bool ShowLinks;
  13. public bool ShowLines;
  14. public bool ShowMeshBounds;
  15. public bool ShowTextBounds;
  16. [Space(10)]
  17. [TextArea(2, 2)]
  18. public string ObjectStats;
  19. [SerializeField]
  20. private TMP_Text m_TextComponent;
  21. [SerializeField]
  22. private Transform m_Transform;
  23. void OnDrawGizmos()
  24. {
  25. if (m_TextComponent == null)
  26. {
  27. m_TextComponent = gameObject.GetComponent<TMP_Text>();
  28. if (m_TextComponent == null)
  29. return;
  30. if (m_Transform == null)
  31. m_Transform = gameObject.GetComponent<Transform>();
  32. }
  33. // Update Text Statistics
  34. TMP_TextInfo textInfo = m_TextComponent.textInfo;
  35. ObjectStats = "Characters: " + textInfo.characterCount + " Words: " + textInfo.wordCount + " Spaces: " + textInfo.spaceCount + " Sprites: " + textInfo.spriteCount + " Links: " + textInfo.linkCount
  36. + "\nLines: " + textInfo.lineCount + " Pages: " + textInfo.pageCount;
  37. // Draw Quads around each of the Characters
  38. #region Draw Characters
  39. if (ShowCharacters)
  40. DrawCharactersBounds();
  41. #endregion
  42. // Draw Quads around each of the words
  43. #region Draw Words
  44. if (ShowWords)
  45. DrawWordBounds();
  46. #endregion
  47. // Draw Quads around each of the words
  48. #region Draw Links
  49. if (ShowLinks)
  50. DrawLinkBounds();
  51. #endregion
  52. // Draw Quads around each line
  53. #region Draw Lines
  54. if (ShowLines)
  55. DrawLineBounds();
  56. #endregion
  57. // Draw Quad around the bounds of the text
  58. #region Draw Bounds
  59. if (ShowMeshBounds)
  60. DrawBounds();
  61. #endregion
  62. // Draw Quad around the rendered region of the text.
  63. #region Draw Text Bounds
  64. if (ShowTextBounds)
  65. DrawTextBounds();
  66. #endregion
  67. }
  68. /// <summary>
  69. /// Method to draw a rectangle around each character.
  70. /// </summary>
  71. /// <param name="text"></param>
  72. void DrawCharactersBounds()
  73. {
  74. TMP_TextInfo textInfo = m_TextComponent.textInfo;
  75. for (int i = 0; i < textInfo.characterCount; i++)
  76. {
  77. // Draw visible as well as invisible characters
  78. TMP_CharacterInfo cInfo = textInfo.characterInfo[i];
  79. bool isCharacterVisible = i >= m_TextComponent.maxVisibleCharacters ||
  80. cInfo.lineNumber >= m_TextComponent.maxVisibleLines ||
  81. (m_TextComponent.overflowMode == TextOverflowModes.Page && cInfo.pageNumber + 1 != m_TextComponent.pageToDisplay) ? false : true;
  82. if (!isCharacterVisible) continue;
  83. // Get Bottom Left and Top Right position of the current character
  84. Vector3 bottomLeft = m_Transform.TransformPoint(cInfo.bottomLeft);
  85. Vector3 topLeft = m_Transform.TransformPoint(new Vector3(cInfo.topLeft.x, cInfo.topLeft.y, 0));
  86. Vector3 topRight = m_Transform.TransformPoint(cInfo.topRight);
  87. Vector3 bottomRight = m_Transform.TransformPoint(new Vector3(cInfo.bottomRight.x, cInfo.bottomRight.y, 0));
  88. Color color = cInfo.isVisible ? Color.yellow : Color.grey;
  89. DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, color);
  90. // Baseline
  91. Vector3 baselineStart = new Vector3(topLeft.x, m_Transform.TransformPoint(new Vector3(0, cInfo.baseLine, 0)).y, 0);
  92. Vector3 baselineEnd = new Vector3(topRight.x, m_Transform.TransformPoint(new Vector3(0, cInfo.baseLine, 0)).y, 0);
  93. Gizmos.color = Color.cyan;
  94. Gizmos.DrawLine(baselineStart, baselineEnd);
  95. // Draw Ascender & Descender for each character.
  96. Vector3 ascenderStart = new Vector3(topLeft.x, m_Transform.TransformPoint(new Vector3(0, cInfo.ascender, 0)).y, 0);
  97. Vector3 ascenderEnd = new Vector3(topRight.x, m_Transform.TransformPoint(new Vector3(0, cInfo.ascender, 0)).y, 0);
  98. Vector3 descenderStart = new Vector3(bottomLeft.x, m_Transform.TransformPoint(new Vector3(0, cInfo.descender, 0)).y, 0);
  99. Vector3 descenderEnd = new Vector3(bottomRight.x, m_Transform.TransformPoint(new Vector3(0, cInfo.descender, 0)).y, 0);
  100. Gizmos.color = Color.cyan;
  101. Gizmos.DrawLine(ascenderStart, ascenderEnd);
  102. Gizmos.DrawLine(descenderStart, descenderEnd);
  103. // Draw Cap Height
  104. float capHeight = cInfo.baseLine + cInfo.fontAsset.faceInfo.capLine * cInfo.scale;
  105. Vector3 capHeightStart = new Vector3(topLeft.x, m_Transform.TransformPoint(new Vector3(0, capHeight, 0)).y, 0);
  106. Vector3 capHeightEnd = new Vector3(topRight.x, m_Transform.TransformPoint(new Vector3(0, capHeight, 0)).y, 0);
  107. Gizmos.color = Color.cyan;
  108. Gizmos.DrawLine(capHeightStart, capHeightEnd);
  109. // Draw Centerline
  110. float meanline = cInfo.baseLine + cInfo.fontAsset.faceInfo.meanLine * cInfo.scale;
  111. Vector3 centerlineStart = new Vector3(topLeft.x, m_Transform.TransformPoint(new Vector3(0, meanline, 0)).y, 0);
  112. Vector3 centerlineEnd = new Vector3(topRight.x, m_Transform.TransformPoint(new Vector3(0, meanline, 0)).y, 0);
  113. Gizmos.color = Color.cyan;
  114. Gizmos.DrawLine(centerlineStart, centerlineEnd);
  115. // Draw Origin for each character.
  116. float gizmoSize = (ascenderEnd.y - descenderEnd.y) * 0.02f;
  117. Vector3 origin = m_Transform.TransformPoint(cInfo.origin, cInfo.baseLine, 0);
  118. Vector3 originBL = new Vector3(origin.x - gizmoSize, origin.y - gizmoSize, 0);
  119. Vector3 originTL = new Vector3(originBL.x, origin.y + gizmoSize, 0);
  120. Vector3 originTR = new Vector3(origin.x + gizmoSize, originTL.y, 0);
  121. Vector3 originBR = new Vector3(originTR.x, originBL.y, 0);
  122. Gizmos.color = new Color(1, 0.5f, 0);
  123. Gizmos.DrawLine(originBL, originTL);
  124. Gizmos.DrawLine(originTL, originTR);
  125. Gizmos.DrawLine(originTR, originBR);
  126. Gizmos.DrawLine(originBR, originBL);
  127. // Draw xAdvance for each character.
  128. gizmoSize = (ascenderEnd.y - descenderEnd.y) * 0.04f;
  129. float xAdvance = m_Transform.TransformPoint(cInfo.xAdvance, 0, 0).x;
  130. Vector3 topAdvance = new Vector3(xAdvance, baselineStart.y + gizmoSize, 0);
  131. Vector3 bottomAdvance = new Vector3(xAdvance, baselineStart.y - gizmoSize, 0);
  132. Vector3 leftAdvance = new Vector3(xAdvance - gizmoSize, baselineStart.y, 0);
  133. Vector3 rightAdvance = new Vector3(xAdvance + gizmoSize, baselineStart.y, 0);
  134. Gizmos.color = Color.green;
  135. Gizmos.DrawLine(topAdvance, bottomAdvance);
  136. Gizmos.DrawLine(leftAdvance, rightAdvance);
  137. }
  138. }
  139. /// <summary>
  140. /// Method to draw rectangles around each word of the text.
  141. /// </summary>
  142. /// <param name="text"></param>
  143. void DrawWordBounds()
  144. {
  145. TMP_TextInfo textInfo = m_TextComponent.textInfo;
  146. for (int i = 0; i < textInfo.wordCount; i++)
  147. {
  148. TMP_WordInfo wInfo = textInfo.wordInfo[i];
  149. bool isBeginRegion = false;
  150. Vector3 bottomLeft = Vector3.zero;
  151. Vector3 topLeft = Vector3.zero;
  152. Vector3 bottomRight = Vector3.zero;
  153. Vector3 topRight = Vector3.zero;
  154. float maxAscender = -Mathf.Infinity;
  155. float minDescender = Mathf.Infinity;
  156. Color wordColor = Color.green;
  157. // Iterate through each character of the word
  158. for (int j = 0; j < wInfo.characterCount; j++)
  159. {
  160. int characterIndex = wInfo.firstCharacterIndex + j;
  161. TMP_CharacterInfo currentCharInfo = textInfo.characterInfo[characterIndex];
  162. int currentLine = currentCharInfo.lineNumber;
  163. bool isCharacterVisible = characterIndex > m_TextComponent.maxVisibleCharacters ||
  164. currentCharInfo.lineNumber > m_TextComponent.maxVisibleLines ||
  165. (m_TextComponent.overflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != m_TextComponent.pageToDisplay) ? false : true;
  166. // Track Max Ascender and Min Descender
  167. maxAscender = Mathf.Max(maxAscender, currentCharInfo.ascender);
  168. minDescender = Mathf.Min(minDescender, currentCharInfo.descender);
  169. if (isBeginRegion == false && isCharacterVisible)
  170. {
  171. isBeginRegion = true;
  172. bottomLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0);
  173. topLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0);
  174. //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  175. // If Word is one character
  176. if (wInfo.characterCount == 1)
  177. {
  178. isBeginRegion = false;
  179. topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
  180. bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
  181. bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
  182. topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
  183. // Draw Region
  184. DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, wordColor);
  185. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  186. }
  187. }
  188. // Last Character of Word
  189. if (isBeginRegion && j == wInfo.characterCount - 1)
  190. {
  191. isBeginRegion = false;
  192. topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
  193. bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
  194. bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
  195. topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
  196. // Draw Region
  197. DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, wordColor);
  198. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  199. }
  200. // If Word is split on more than one line.
  201. else if (isBeginRegion && currentLine != textInfo.characterInfo[characterIndex + 1].lineNumber)
  202. {
  203. isBeginRegion = false;
  204. topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
  205. bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
  206. bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
  207. topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
  208. // Draw Region
  209. DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, wordColor);
  210. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  211. maxAscender = -Mathf.Infinity;
  212. minDescender = Mathf.Infinity;
  213. }
  214. }
  215. //Debug.Log(wInfo.GetWord(m_TextMeshPro.textInfo.characterInfo));
  216. }
  217. }
  218. /// <summary>
  219. /// Draw rectangle around each of the links contained in the text.
  220. /// </summary>
  221. /// <param name="text"></param>
  222. void DrawLinkBounds()
  223. {
  224. TMP_TextInfo textInfo = m_TextComponent.textInfo;
  225. for (int i = 0; i < textInfo.linkCount; i++)
  226. {
  227. TMP_LinkInfo linkInfo = textInfo.linkInfo[i];
  228. bool isBeginRegion = false;
  229. Vector3 bottomLeft = Vector3.zero;
  230. Vector3 topLeft = Vector3.zero;
  231. Vector3 bottomRight = Vector3.zero;
  232. Vector3 topRight = Vector3.zero;
  233. float maxAscender = -Mathf.Infinity;
  234. float minDescender = Mathf.Infinity;
  235. Color32 linkColor = Color.cyan;
  236. // Iterate through each character of the link text
  237. for (int j = 0; j < linkInfo.linkTextLength; j++)
  238. {
  239. int characterIndex = linkInfo.linkTextfirstCharacterIndex + j;
  240. TMP_CharacterInfo currentCharInfo = textInfo.characterInfo[characterIndex];
  241. int currentLine = currentCharInfo.lineNumber;
  242. bool isCharacterVisible = characterIndex > m_TextComponent.maxVisibleCharacters ||
  243. currentCharInfo.lineNumber > m_TextComponent.maxVisibleLines ||
  244. (m_TextComponent.overflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != m_TextComponent.pageToDisplay) ? false : true;
  245. // Track Max Ascender and Min Descender
  246. maxAscender = Mathf.Max(maxAscender, currentCharInfo.ascender);
  247. minDescender = Mathf.Min(minDescender, currentCharInfo.descender);
  248. if (isBeginRegion == false && isCharacterVisible)
  249. {
  250. isBeginRegion = true;
  251. bottomLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0);
  252. topLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0);
  253. //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  254. // If Link is one character
  255. if (linkInfo.linkTextLength == 1)
  256. {
  257. isBeginRegion = false;
  258. topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
  259. bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
  260. bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
  261. topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
  262. // Draw Region
  263. DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, linkColor);
  264. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  265. }
  266. }
  267. // Last Character of Link
  268. if (isBeginRegion && j == linkInfo.linkTextLength - 1)
  269. {
  270. isBeginRegion = false;
  271. topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
  272. bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
  273. bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
  274. topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
  275. // Draw Region
  276. DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, linkColor);
  277. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  278. }
  279. // If Link is split on more than one line.
  280. else if (isBeginRegion && currentLine != textInfo.characterInfo[characterIndex + 1].lineNumber)
  281. {
  282. isBeginRegion = false;
  283. topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
  284. bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
  285. bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
  286. topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
  287. // Draw Region
  288. DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, linkColor);
  289. maxAscender = -Mathf.Infinity;
  290. minDescender = Mathf.Infinity;
  291. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  292. }
  293. }
  294. //Debug.Log(wInfo.GetWord(m_TextMeshPro.textInfo.characterInfo));
  295. }
  296. }
  297. /// <summary>
  298. /// Draw Rectangles around each lines of the text.
  299. /// </summary>
  300. /// <param name="text"></param>
  301. void DrawLineBounds()
  302. {
  303. TMP_TextInfo textInfo = m_TextComponent.textInfo;
  304. for (int i = 0; i < textInfo.lineCount; i++)
  305. {
  306. TMP_LineInfo lineInfo = textInfo.lineInfo[i];
  307. bool isLineVisible = (lineInfo.characterCount == 1 && textInfo.characterInfo[lineInfo.firstCharacterIndex].character == 10) ||
  308. i > m_TextComponent.maxVisibleLines ||
  309. (m_TextComponent.overflowMode == TextOverflowModes.Page && textInfo.characterInfo[lineInfo.firstCharacterIndex].pageNumber + 1 != m_TextComponent.pageToDisplay) ? false : true;
  310. if (!isLineVisible) continue;
  311. //if (!ShowLinesOnlyVisibleCharacters)
  312. //{
  313. // Get Bottom Left and Top Right position of each line
  314. float ascender = lineInfo.ascender;
  315. float descender = lineInfo.descender;
  316. float baseline = lineInfo.baseline;
  317. float maxAdvance = lineInfo.maxAdvance;
  318. Vector3 bottomLeft = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstCharacterIndex].bottomLeft.x, descender, 0));
  319. Vector3 topLeft = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstCharacterIndex].bottomLeft.x, ascender, 0));
  320. Vector3 topRight = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.lastCharacterIndex].topRight.x, ascender, 0));
  321. Vector3 bottomRight = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.lastCharacterIndex].topRight.x, descender, 0));
  322. DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, Color.green);
  323. Vector3 bottomOrigin = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstCharacterIndex].origin, descender, 0));
  324. Vector3 topOrigin = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstCharacterIndex].origin, ascender, 0));
  325. Vector3 bottomAdvance = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstCharacterIndex].origin + maxAdvance, descender, 0));
  326. Vector3 topAdvance = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstCharacterIndex].origin + maxAdvance, ascender, 0));
  327. DrawDottedRectangle(bottomOrigin, topOrigin, topAdvance, bottomAdvance, Color.green);
  328. Vector3 baselineStart = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstCharacterIndex].bottomLeft.x, baseline, 0));
  329. Vector3 baselineEnd = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.lastCharacterIndex].topRight.x, baseline, 0));
  330. Gizmos.color = Color.cyan;
  331. Gizmos.DrawLine(baselineStart, baselineEnd);
  332. // Draw LineExtents
  333. Gizmos.color = Color.grey;
  334. Gizmos.DrawLine(m_Transform.TransformPoint(lineInfo.lineExtents.min), m_Transform.TransformPoint(lineInfo.lineExtents.max));
  335. //}
  336. //else
  337. //{
  338. //// Get Bottom Left and Top Right position of each line
  339. //float ascender = lineInfo.ascender;
  340. //float descender = lineInfo.descender;
  341. //Vector3 bottomLeft = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstVisibleCharacterIndex].bottomLeft.x, descender, 0));
  342. //Vector3 topLeft = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstVisibleCharacterIndex].bottomLeft.x, ascender, 0));
  343. //Vector3 topRight = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.lastVisibleCharacterIndex].topRight.x, ascender, 0));
  344. //Vector3 bottomRight = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.lastVisibleCharacterIndex].topRight.x, descender, 0));
  345. //DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, Color.green);
  346. //Vector3 baselineStart = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.firstVisibleCharacterIndex].bottomLeft.x, textInfo.characterInfo[lineInfo.firstVisibleCharacterIndex].baseLine, 0));
  347. //Vector3 baselineEnd = m_Transform.TransformPoint(new Vector3(textInfo.characterInfo[lineInfo.lastVisibleCharacterIndex].topRight.x, textInfo.characterInfo[lineInfo.lastVisibleCharacterIndex].baseLine, 0));
  348. //Gizmos.color = Color.cyan;
  349. //Gizmos.DrawLine(baselineStart, baselineEnd);
  350. //}
  351. }
  352. }
  353. /// <summary>
  354. /// Draw Rectangle around the bounds of the text object.
  355. /// </summary>
  356. void DrawBounds()
  357. {
  358. Bounds meshBounds = m_TextComponent.bounds;
  359. // Get Bottom Left and Top Right position of each word
  360. Vector3 bottomLeft = m_TextComponent.transform.position + (meshBounds.center - meshBounds.extents);
  361. Vector3 topRight = m_TextComponent.transform.position + (meshBounds.center + meshBounds.extents);
  362. DrawRectangle(bottomLeft, topRight, new Color(1, 0.5f, 0));
  363. }
  364. void DrawTextBounds()
  365. {
  366. Bounds textBounds = m_TextComponent.textBounds;
  367. Vector3 bottomLeft = m_TextComponent.transform.position + (textBounds.center - textBounds.extents);
  368. Vector3 topRight = m_TextComponent.transform.position + (textBounds.center + textBounds.extents);
  369. DrawRectangle(bottomLeft, topRight, new Color(0f, 0.5f, 0.5f));
  370. }
  371. // Draw Rectangles
  372. void DrawRectangle(Vector3 BL, Vector3 TR, Color color)
  373. {
  374. Gizmos.color = color;
  375. Gizmos.DrawLine(new Vector3(BL.x, BL.y, 0), new Vector3(BL.x, TR.y, 0));
  376. Gizmos.DrawLine(new Vector3(BL.x, TR.y, 0), new Vector3(TR.x, TR.y, 0));
  377. Gizmos.DrawLine(new Vector3(TR.x, TR.y, 0), new Vector3(TR.x, BL.y, 0));
  378. Gizmos.DrawLine(new Vector3(TR.x, BL.y, 0), new Vector3(BL.x, BL.y, 0));
  379. }
  380. // Draw Rectangles
  381. void DrawRectangle(Vector3 bl, Vector3 tl, Vector3 tr, Vector3 br, Color color)
  382. {
  383. Gizmos.color = color;
  384. Gizmos.DrawLine(bl, tl);
  385. Gizmos.DrawLine(tl, tr);
  386. Gizmos.DrawLine(tr, br);
  387. Gizmos.DrawLine(br, bl);
  388. }
  389. // Draw Rectangles
  390. void DrawDottedRectangle(Vector3 bl, Vector3 tl, Vector3 tr, Vector3 br, Color color)
  391. {
  392. var cam = Camera.current;
  393. float dotSpacing = (cam.WorldToScreenPoint(br).x - cam.WorldToScreenPoint(bl).x) / 75f;
  394. UnityEditor.Handles.color = color;
  395. UnityEditor.Handles.DrawDottedLine(bl, tl, dotSpacing);
  396. UnityEditor.Handles.DrawDottedLine(tl, tr, dotSpacing);
  397. UnityEditor.Handles.DrawDottedLine(tr, br, dotSpacing);
  398. UnityEditor.Handles.DrawDottedLine(br, bl, dotSpacing);
  399. }
  400. #endif
  401. }
  402. }