Ei kuvausta
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_TextUtilities.cs 104KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro
  4. {
  5. public enum CaretPosition { None, Left, Right }
  6. /// <summary>
  7. /// Structure which contains the character index and position of caret relative to the character.
  8. /// </summary>
  9. public struct CaretInfo
  10. {
  11. public int index;
  12. public CaretPosition position;
  13. public CaretInfo(int index, CaretPosition position)
  14. {
  15. this.index = index;
  16. this.position = position;
  17. }
  18. }
  19. public static class TMP_TextUtilities
  20. {
  21. private static Vector3[] m_rectWorldCorners = new Vector3[4];
  22. // TEXT INPUT COMPONENT RELATED FUNCTIONS
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. /// <param name="textComponent">A reference to the text object.</param>
  27. /// <param name="position">Position to check for intersection.</param>
  28. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  29. /// <returns></returns>
  30. //public static CaretInfo GetCursorInsertionIndex(TMP_Text textComponent, Vector3 position, Camera camera)
  31. //{
  32. // int index = TMP_TextUtilities.FindNearestCharacter(textComponent, position, camera, false);
  33. // RectTransform rectTransform = textComponent.rectTransform;
  34. // // Convert position into Worldspace coordinates
  35. // ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  36. // TMP_CharacterInfo cInfo = textComponent.textInfo.characterInfo[index];
  37. // // Get Bottom Left and Top Right position of the current character
  38. // Vector3 bl = rectTransform.TransformPoint(cInfo.bottomLeft);
  39. // //Vector3 tl = rectTransform.TransformPoint(new Vector3(cInfo.bottomLeft.x, cInfo.topRight.y, 0));
  40. // Vector3 tr = rectTransform.TransformPoint(cInfo.topRight);
  41. // //Vector3 br = rectTransform.TransformPoint(new Vector3(cInfo.topRight.x, cInfo.bottomLeft.y, 0));
  42. // float insertPosition = (position.x - bl.x) / (tr.x - bl.x);
  43. // if (insertPosition < 0.5f)
  44. // return new CaretInfo(index, CaretPosition.Left);
  45. // else
  46. // return new CaretInfo(index, CaretPosition.Right);
  47. //}
  48. /// <summary>
  49. /// Function returning the index of the character whose origin is closest to the cursor.
  50. /// </summary>
  51. /// <param name="textComponent">A reference to the text object.</param>
  52. /// <param name="position">Position to check for intersection.</param>
  53. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  54. /// <returns></returns>
  55. public static int GetCursorIndexFromPosition(TMP_Text textComponent, Vector3 position, Camera camera)
  56. {
  57. int index = TMP_TextUtilities.FindNearestCharacter(textComponent, position, camera, false);
  58. RectTransform rectTransform = textComponent.rectTransform;
  59. // Convert position into Worldspace coordinates
  60. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  61. TMP_CharacterInfo cInfo = textComponent.textInfo.characterInfo[index];
  62. // Get Bottom Left and Top Right position of the current character
  63. Vector3 bl = rectTransform.TransformPoint(cInfo.bottomLeft);
  64. Vector3 tr = rectTransform.TransformPoint(cInfo.topRight);
  65. float insertPosition = (position.x - bl.x) / (tr.x - bl.x);
  66. if (insertPosition < 0.5f)
  67. return index;
  68. else
  69. return index + 1;
  70. }
  71. /// <summary>
  72. /// Function returning the index of the character whose origin is closest to the cursor.
  73. /// </summary>
  74. /// <param name="textComponent">A reference to the text object.</param>
  75. /// <param name="position">Position to check for intersection.</param>
  76. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  77. /// <param name="cursor">The position of the cursor insertion position relative to the position.</param>
  78. /// <returns></returns>
  79. //public static int GetCursorIndexFromPosition(TMP_Text textComponent, Vector3 position, Camera camera, out CaretPosition cursor)
  80. //{
  81. // int index = TMP_TextUtilities.FindNearestCharacter(textComponent, position, camera, false);
  82. // RectTransform rectTransform = textComponent.rectTransform;
  83. // // Convert position into Worldspace coordinates
  84. // ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  85. // TMP_CharacterInfo cInfo = textComponent.textInfo.characterInfo[index];
  86. // // Get Bottom Left and Top Right position of the current character
  87. // Vector3 bl = rectTransform.TransformPoint(cInfo.bottomLeft);
  88. // Vector3 tr = rectTransform.TransformPoint(cInfo.topRight);
  89. // float insertPosition = (position.x - bl.x) / (tr.x - bl.x);
  90. // if (insertPosition < 0.5f)
  91. // {
  92. // cursor = CaretPosition.Left;
  93. // return index;
  94. // }
  95. // else
  96. // {
  97. // cursor = CaretPosition.Right;
  98. // return index;
  99. // }
  100. //}
  101. /// <summary>
  102. /// Function returning the index of the character whose origin is closest to the cursor.
  103. /// </summary>
  104. /// <param name="textComponent">A reference to the text object.</param>
  105. /// <param name="position">Position to check for intersection.</param>
  106. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  107. /// <param name="cursor">The position of the cursor insertion position relative to the position.</param>
  108. /// <returns></returns>
  109. public static int GetCursorIndexFromPosition(TMP_Text textComponent, Vector3 position, Camera camera, out CaretPosition cursor)
  110. {
  111. int line = FindNearestLine(textComponent, position, camera);
  112. // Return if text doesn't contain any lines of text.
  113. if (line == -1)
  114. {
  115. cursor = CaretPosition.Left;
  116. return 0;
  117. }
  118. int index = FindNearestCharacterOnLine(textComponent, position, line, camera, false);
  119. // Special handling if line contains only one character.
  120. if (textComponent.textInfo.lineInfo[line].characterCount == 1)
  121. {
  122. cursor = CaretPosition.Left;
  123. return index;
  124. }
  125. RectTransform rectTransform = textComponent.rectTransform;
  126. // Convert position into Worldspace coordinates
  127. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  128. TMP_CharacterInfo cInfo = textComponent.textInfo.characterInfo[index];
  129. // Get Bottom Left and Top Right position of the current character
  130. Vector3 bl = rectTransform.TransformPoint(cInfo.bottomLeft);
  131. Vector3 tr = rectTransform.TransformPoint(cInfo.topRight);
  132. float insertPosition = (position.x - bl.x) / (tr.x - bl.x);
  133. if (insertPosition < 0.5f)
  134. {
  135. cursor = CaretPosition.Left;
  136. return index;
  137. }
  138. else
  139. {
  140. cursor = CaretPosition.Right;
  141. return index;
  142. }
  143. }
  144. /// <summary>
  145. /// Function returning the line nearest to the position.
  146. /// </summary>
  147. /// <param name="textComponent"></param>
  148. /// <param name="position"></param>
  149. /// <param name="camera"></param>
  150. /// <returns></returns>
  151. public static int FindNearestLine(TMP_Text text, Vector3 position, Camera camera)
  152. {
  153. RectTransform rectTransform = text.rectTransform;
  154. float distance = Mathf.Infinity;
  155. int closest = -1;
  156. // Convert position into Worldspace coordinates
  157. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  158. for (int i = 0; i < text.textInfo.lineCount; i++)
  159. {
  160. TMP_LineInfo lineInfo = text.textInfo.lineInfo[i];
  161. float ascender = rectTransform.TransformPoint(new Vector3(0, lineInfo.ascender, 0)).y;
  162. float descender = rectTransform.TransformPoint(new Vector3(0, lineInfo.descender, 0)).y;
  163. if (ascender > position.y && descender < position.y)
  164. {
  165. //Debug.Log("Position is on line " + i);
  166. return i;
  167. }
  168. float d0 = Mathf.Abs(ascender - position.y);
  169. float d1 = Mathf.Abs(descender - position.y);
  170. float d = Mathf.Min(d0, d1);
  171. if (d < distance)
  172. {
  173. distance = d;
  174. closest = i;
  175. }
  176. }
  177. //Debug.Log("Closest line to position is " + closest);
  178. return closest;
  179. }
  180. /// <summary>
  181. /// Function returning the nearest character to position on a given line.
  182. /// </summary>
  183. /// <param name="text"></param>
  184. /// <param name="position"></param>
  185. /// <param name="line"></param>
  186. /// <param name="camera"></param>
  187. /// <returns></returns>
  188. public static int FindNearestCharacterOnLine(TMP_Text text, Vector3 position, int line, Camera camera, bool visibleOnly)
  189. {
  190. RectTransform rectTransform = text.rectTransform;
  191. // Convert position into Worldspace coordinates
  192. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  193. int firstCharacter = text.textInfo.lineInfo[line].firstCharacterIndex;
  194. int lastCharacter = text.textInfo.lineInfo[line].lastCharacterIndex;
  195. float distanceSqr = Mathf.Infinity;
  196. int closest = lastCharacter;
  197. for (int i = firstCharacter; i < lastCharacter; i++)
  198. {
  199. // Get current character info.
  200. TMP_CharacterInfo cInfo = text.textInfo.characterInfo[i];
  201. if (visibleOnly && !cInfo.isVisible) continue;
  202. // Ignore Carriage Returns <CR>
  203. if (cInfo.character == '\r')
  204. continue;
  205. // Get Bottom Left and Top Right position of the current character
  206. Vector3 bl = rectTransform.TransformPoint(cInfo.bottomLeft);
  207. Vector3 tl = rectTransform.TransformPoint(new Vector3(cInfo.bottomLeft.x, cInfo.topRight.y, 0));
  208. Vector3 tr = rectTransform.TransformPoint(cInfo.topRight);
  209. Vector3 br = rectTransform.TransformPoint(new Vector3(cInfo.topRight.x, cInfo.bottomLeft.y, 0));
  210. if (PointIntersectRectangle(position, bl, tl, tr, br))
  211. {
  212. closest = i;
  213. break;
  214. }
  215. // Find the closest corner to position.
  216. float dbl = DistanceToLine(bl, tl, position);
  217. float dtl = DistanceToLine(tl, tr, position);
  218. float dtr = DistanceToLine(tr, br, position);
  219. float dbr = DistanceToLine(br, bl, position);
  220. float d = dbl < dtl ? dbl : dtl;
  221. d = d < dtr ? d : dtr;
  222. d = d < dbr ? d : dbr;
  223. if (distanceSqr > d)
  224. {
  225. distanceSqr = d;
  226. closest = i;
  227. }
  228. }
  229. return closest;
  230. }
  231. /// <summary>
  232. /// Function used to determine if the position intersects with the RectTransform.
  233. /// </summary>
  234. /// <param name="rectTransform">A reference to the RectTranform of the text object.</param>
  235. /// <param name="position">Position to check for intersection.</param>
  236. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  237. /// <returns></returns>
  238. public static bool IsIntersectingRectTransform(RectTransform rectTransform, Vector3 position, Camera camera)
  239. {
  240. // Convert position into Worldspace coordinates
  241. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  242. rectTransform.GetWorldCorners(m_rectWorldCorners);
  243. if (PointIntersectRectangle(position, m_rectWorldCorners[0], m_rectWorldCorners[1], m_rectWorldCorners[2], m_rectWorldCorners[3]))
  244. {
  245. return true;
  246. }
  247. return false;
  248. }
  249. // CHARACTER HANDLING
  250. /// <summary>
  251. /// Function returning the index of the character at the given position (if any).
  252. /// </summary>
  253. /// <param name="text">A reference to the TextMeshPro component.</param>
  254. /// <param name="position">Position to check for intersection.</param>
  255. /// <param name="camera">The scene camera which is rendering the text or whichever one might be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  256. /// <param name="visibleOnly">Only check for visible characters.</param>
  257. /// <returns></returns>
  258. public static int FindIntersectingCharacter(TMP_Text text, Vector3 position, Camera camera, bool visibleOnly)
  259. {
  260. RectTransform rectTransform = text.rectTransform;
  261. // Convert position into Worldspace coordinates
  262. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  263. for (int i = 0; i < text.textInfo.characterCount; i++)
  264. {
  265. // Get current character info.
  266. TMP_CharacterInfo cInfo = text.textInfo.characterInfo[i];
  267. if (visibleOnly && !cInfo.isVisible) continue;
  268. // Get Bottom Left and Top Right position of the current character
  269. Vector3 bl = rectTransform.TransformPoint(cInfo.bottomLeft);
  270. Vector3 tl = rectTransform.TransformPoint(new Vector3(cInfo.bottomLeft.x, cInfo.topRight.y, 0));
  271. Vector3 tr = rectTransform.TransformPoint(cInfo.topRight);
  272. Vector3 br = rectTransform.TransformPoint(new Vector3(cInfo.topRight.x, cInfo.bottomLeft.y, 0));
  273. if (PointIntersectRectangle(position, bl, tl, tr, br))
  274. return i;
  275. }
  276. return -1;
  277. }
  278. /// <summary>
  279. /// Function returning the index of the character at the given position (if any).
  280. /// </summary>
  281. /// <param name="text">A reference to the TextMeshPro UGUI component.</param>
  282. /// <param name="position">Position to check for intersection.</param>
  283. /// <param name="camera">The camera which is rendering the text object.</param>
  284. /// <param name="visibleOnly">Only check for visible characters.</param>
  285. /// <returns></returns>
  286. //public static int FindIntersectingCharacter(TextMeshPro text, Vector3 position, Camera camera, bool visibleOnly)
  287. //{
  288. // Transform textTransform = text.transform;
  289. // // Convert position into Worldspace coordinates
  290. // ScreenPointToWorldPointInRectangle(textTransform, position, camera, out position);
  291. // for (int i = 0; i < text.textInfo.characterCount; i++)
  292. // {
  293. // // Get current character info.
  294. // TMP_CharacterInfo cInfo = text.textInfo.characterInfo[i];
  295. // if ((visibleOnly && !cInfo.isVisible) || (text.OverflowMode == TextOverflowModes.Page && cInfo.pageNumber + 1 != text.pageToDisplay))
  296. // continue;
  297. // // Get Bottom Left and Top Right position of the current character
  298. // Vector3 bl = textTransform.TransformPoint(cInfo.bottomLeft);
  299. // Vector3 tl = textTransform.TransformPoint(new Vector3(cInfo.bottomLeft.x, cInfo.topRight.y, 0));
  300. // Vector3 tr = textTransform.TransformPoint(cInfo.topRight);
  301. // Vector3 br = textTransform.TransformPoint(new Vector3(cInfo.topRight.x, cInfo.bottomLeft.y, 0));
  302. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  303. // return i;
  304. // }
  305. // return -1;
  306. //}
  307. /// <summary>
  308. /// Function to find the nearest character to position.
  309. /// </summary>
  310. /// <param name="text">A reference to the TMP Text component.</param>
  311. /// <param name="position">Position to check for intersection.</param>
  312. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  313. /// <param name="visibleOnly">Only check for visible characters.</param>
  314. /// <returns></returns>
  315. public static int FindNearestCharacter(TMP_Text text, Vector3 position, Camera camera, bool visibleOnly)
  316. {
  317. RectTransform rectTransform = text.rectTransform;
  318. float distanceSqr = Mathf.Infinity;
  319. int closest = 0;
  320. // Convert position into Worldspace coordinates
  321. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  322. for (int i = 0; i < text.textInfo.characterCount; i++)
  323. {
  324. // Get current character info.
  325. TMP_CharacterInfo cInfo = text.textInfo.characterInfo[i];
  326. if (visibleOnly && !cInfo.isVisible) continue;
  327. // Get Bottom Left and Top Right position of the current character
  328. Vector3 bl = rectTransform.TransformPoint(cInfo.bottomLeft);
  329. Vector3 tl = rectTransform.TransformPoint(new Vector3(cInfo.bottomLeft.x, cInfo.topRight.y, 0));
  330. Vector3 tr = rectTransform.TransformPoint(cInfo.topRight);
  331. Vector3 br = rectTransform.TransformPoint(new Vector3(cInfo.topRight.x, cInfo.bottomLeft.y, 0));
  332. if (PointIntersectRectangle(position, bl, tl, tr, br))
  333. return i;
  334. // Find the closest corner to position.
  335. float dbl = DistanceToLine(bl, tl, position);
  336. float dtl = DistanceToLine(tl, tr, position);
  337. float dtr = DistanceToLine(tr, br, position);
  338. float dbr = DistanceToLine(br, bl, position);
  339. float d = dbl < dtl ? dbl : dtl;
  340. d = d < dtr ? d : dtr;
  341. d = d < dbr ? d : dbr;
  342. if (distanceSqr > d)
  343. {
  344. distanceSqr = d;
  345. closest = i;
  346. }
  347. }
  348. return closest;
  349. }
  350. /// <summary>
  351. /// Function to find the nearest character to position.
  352. /// </summary>
  353. /// <param name="text">A reference to the TextMeshPro UGUI component.</param>
  354. /// <param name="position">Position to check for intersection.</param>
  355. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  356. /// <param name="visibleOnly">Only check for visible characters.</param>
  357. /// <returns></returns>
  358. //public static int FindNearestCharacter(TextMeshProUGUI text, Vector3 position, Camera camera, bool visibleOnly)
  359. //{
  360. // RectTransform rectTransform = text.rectTransform;
  361. // float distanceSqr = Mathf.Infinity;
  362. // int closest = 0;
  363. // // Convert position into Worldspace coordinates
  364. // ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  365. // for (int i = 0; i < text.textInfo.characterCount; i++)
  366. // {
  367. // // Get current character info.
  368. // TMP_CharacterInfo cInfo = text.textInfo.characterInfo[i];
  369. // if ((visibleOnly && !cInfo.isVisible) || (text.OverflowMode == TextOverflowModes.Page && cInfo.pageNumber + 1 != text.pageToDisplay))
  370. // continue;
  371. // // Get Bottom Left and Top Right position of the current character
  372. // Vector3 bl = rectTransform.TransformPoint(cInfo.bottomLeft);
  373. // Vector3 tl = rectTransform.TransformPoint(new Vector3(cInfo.bottomLeft.x, cInfo.topRight.y, 0));
  374. // Vector3 tr = rectTransform.TransformPoint(cInfo.topRight);
  375. // Vector3 br = rectTransform.TransformPoint(new Vector3(cInfo.topRight.x, cInfo.bottomLeft.y, 0));
  376. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  377. // return i;
  378. // // Find the closest corner to position.
  379. // float dbl = DistanceToLine(bl, tl, position);
  380. // float dtl = DistanceToLine(tl, tr, position);
  381. // float dtr = DistanceToLine(tr, br, position);
  382. // float dbr = DistanceToLine(br, bl, position);
  383. // float d = dbl < dtl ? dbl : dtl;
  384. // d = d < dtr ? d : dtr;
  385. // d = d < dbr ? d : dbr;
  386. // if (distanceSqr > d)
  387. // {
  388. // distanceSqr = d;
  389. // closest = i;
  390. // }
  391. // }
  392. // //Debug.Log("Returning nearest character at index: " + closest);
  393. // return closest;
  394. //}
  395. /// <summary>
  396. /// Function to find the nearest character to position.
  397. /// </summary>
  398. /// <param name="text">A reference to the TextMeshPro component.</param>
  399. /// <param name="position">Position to check for intersection.</param>
  400. /// <param name="camera">The camera which is rendering the text object.</param>
  401. /// <param name="visibleOnly">Only check for visible characters.</param>
  402. /// <returns></returns>
  403. //public static int FindNearestCharacter(TextMeshPro text, Vector3 position, Camera camera, bool visibleOnly)
  404. //{
  405. // Transform textTransform = text.transform;
  406. // float distanceSqr = Mathf.Infinity;
  407. // int closest = 0;
  408. // // Convert position into Worldspace coordinates
  409. // ScreenPointToWorldPointInRectangle(textTransform, position, camera, out position);
  410. // for (int i = 0; i < text.textInfo.characterCount; i++)
  411. // {
  412. // // Get current character info.
  413. // TMP_CharacterInfo cInfo = text.textInfo.characterInfo[i];
  414. // if ((visibleOnly && !cInfo.isVisible) || (text.OverflowMode == TextOverflowModes.Page && cInfo.pageNumber + 1 != text.pageToDisplay))
  415. // continue;
  416. // // Get Bottom Left and Top Right position of the current character
  417. // Vector3 bl = textTransform.TransformPoint(cInfo.bottomLeft);
  418. // Vector3 tl = textTransform.TransformPoint(new Vector3(cInfo.bottomLeft.x, cInfo.topRight.y, 0));
  419. // Vector3 tr = textTransform.TransformPoint(cInfo.topRight);
  420. // Vector3 br = textTransform.TransformPoint(new Vector3(cInfo.topRight.x, cInfo.bottomLeft.y, 0));
  421. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  422. // return i;
  423. // // Find the closest corner to position.
  424. // float dbl = DistanceToLine(bl, tl, position); // (position - bl).sqrMagnitude;
  425. // float dtl = DistanceToLine(tl, tr, position); // (position - tl).sqrMagnitude;
  426. // float dtr = DistanceToLine(tr, br, position); // (position - tr).sqrMagnitude;
  427. // float dbr = DistanceToLine(br, bl, position); // (position - br).sqrMagnitude;
  428. // float d = dbl < dtl ? dbl : dtl;
  429. // d = d < dtr ? d : dtr;
  430. // d = d < dbr ? d : dbr;
  431. // if (distanceSqr > d)
  432. // {
  433. // distanceSqr = d;
  434. // closest = i;
  435. // }
  436. // }
  437. // //Debug.Log("Returning nearest character at index: " + closest);
  438. // return closest;
  439. //}
  440. // WORD HANDLING
  441. /// <summary>
  442. /// Function returning the index of the word at the given position (if any).
  443. /// </summary>
  444. /// <param name="text">A reference to the TMP_Text component.</param>
  445. /// <param name="position">Position to check for intersection.</param>
  446. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  447. /// <returns></returns>
  448. public static int FindIntersectingWord(TMP_Text text, Vector3 position, Camera camera)
  449. {
  450. RectTransform rectTransform = text.rectTransform;
  451. // Convert position into Worldspace coordinates
  452. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  453. for (int i = 0; i < text.textInfo.wordCount; i++)
  454. {
  455. TMP_WordInfo wInfo = text.textInfo.wordInfo[i];
  456. bool isBeginRegion = false;
  457. Vector3 bl = Vector3.zero;
  458. Vector3 tl = Vector3.zero;
  459. Vector3 br = Vector3.zero;
  460. Vector3 tr = Vector3.zero;
  461. float maxAscender = -Mathf.Infinity;
  462. float minDescender = Mathf.Infinity;
  463. // Iterate through each character of the word
  464. for (int j = 0; j < wInfo.characterCount; j++)
  465. {
  466. int characterIndex = wInfo.firstCharacterIndex + j;
  467. TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  468. int currentLine = currentCharInfo.lineNumber;
  469. bool isCharacterVisible = currentCharInfo.isVisible;
  470. // Track maximum Ascender and minimum Descender for each word.
  471. maxAscender = Mathf.Max(maxAscender, currentCharInfo.ascender);
  472. minDescender = Mathf.Min(minDescender, currentCharInfo.descender);
  473. if (isBeginRegion == false && isCharacterVisible)
  474. {
  475. isBeginRegion = true;
  476. bl = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0);
  477. tl = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0);
  478. //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  479. // If Word is one character
  480. if (wInfo.characterCount == 1)
  481. {
  482. isBeginRegion = false;
  483. br = new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0);
  484. tr = new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0);
  485. // Transform coordinates to be relative to transform and account min descender and max ascender.
  486. bl = rectTransform.TransformPoint(new Vector3(bl.x, minDescender, 0));
  487. tl = rectTransform.TransformPoint(new Vector3(tl.x, maxAscender, 0));
  488. tr = rectTransform.TransformPoint(new Vector3(tr.x, maxAscender, 0));
  489. br = rectTransform.TransformPoint(new Vector3(br.x, minDescender, 0));
  490. // Check for Intersection
  491. if (PointIntersectRectangle(position, bl, tl, tr, br))
  492. return i;
  493. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  494. }
  495. }
  496. // Last Character of Word
  497. if (isBeginRegion && j == wInfo.characterCount - 1)
  498. {
  499. isBeginRegion = false;
  500. br = new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0);
  501. tr = new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0);
  502. // Transform coordinates to be relative to transform and account min descender and max ascender.
  503. bl = rectTransform.TransformPoint(new Vector3(bl.x, minDescender, 0));
  504. tl = rectTransform.TransformPoint(new Vector3(tl.x, maxAscender, 0));
  505. tr = rectTransform.TransformPoint(new Vector3(tr.x, maxAscender, 0));
  506. br = rectTransform.TransformPoint(new Vector3(br.x, minDescender, 0));
  507. // Check for Intersection
  508. if (PointIntersectRectangle(position, bl, tl, tr, br))
  509. return i;
  510. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  511. }
  512. // If Word is split on more than one line.
  513. else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  514. {
  515. isBeginRegion = false;
  516. br = new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0);
  517. tr = new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0);
  518. // Transform coordinates to be relative to transform and account min descender and max ascender.
  519. bl = rectTransform.TransformPoint(new Vector3(bl.x, minDescender, 0));
  520. tl = rectTransform.TransformPoint(new Vector3(tl.x, maxAscender, 0));
  521. tr = rectTransform.TransformPoint(new Vector3(tr.x, maxAscender, 0));
  522. br = rectTransform.TransformPoint(new Vector3(br.x, minDescender, 0));
  523. maxAscender = -Mathf.Infinity;
  524. minDescender = Mathf.Infinity;
  525. // Check for Intersection
  526. if (PointIntersectRectangle(position, bl, tl, tr, br))
  527. return i;
  528. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  529. }
  530. }
  531. //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  532. }
  533. return -1;
  534. }
  535. /// <summary>
  536. /// Function returning the index of the word at the given position (if any).
  537. /// </summary>
  538. /// <param name="text">A reference to the TextMeshPro UGUI component.</param>
  539. /// <param name="position">Position to check for intersection.</param>
  540. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  541. /// <returns></returns>
  542. //public static int FindIntersectingWord(TextMeshProUGUI text, Vector3 position, Camera camera)
  543. //{
  544. // RectTransform rectTransform = text.rectTransform;
  545. // // Convert position into Worldspace coordinates
  546. // ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  547. // for (int i = 0; i < text.textInfo.wordCount; i++)
  548. // {
  549. // TMP_WordInfo wInfo = text.textInfo.wordInfo[i];
  550. // bool isBeginRegion = false;
  551. // Vector3 bl = Vector3.zero;
  552. // Vector3 tl = Vector3.zero;
  553. // Vector3 br = Vector3.zero;
  554. // Vector3 tr = Vector3.zero;
  555. // float maxAscender = -Mathf.Infinity;
  556. // float minDescender = Mathf.Infinity;
  557. // // Iterate through each character of the word
  558. // for (int j = 0; j < wInfo.characterCount; j++)
  559. // {
  560. // int characterIndex = wInfo.firstCharacterIndex + j;
  561. // TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  562. // int currentLine = currentCharInfo.lineNumber;
  563. // bool isCharacterVisible = characterIndex > text.maxVisibleCharacters ||
  564. // currentCharInfo.lineNumber > text.maxVisibleLines ||
  565. // (text.OverflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != text.pageToDisplay) ? false : true;
  566. // // Track maximum Ascender and minimum Descender for each word.
  567. // maxAscender = Mathf.Max(maxAscender, currentCharInfo.ascender);
  568. // minDescender = Mathf.Min(minDescender, currentCharInfo.descender);
  569. // if (isBeginRegion == false && isCharacterVisible)
  570. // {
  571. // isBeginRegion = true;
  572. // bl = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0);
  573. // tl = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0);
  574. // //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  575. // // If Word is one character
  576. // if (wInfo.characterCount == 1)
  577. // {
  578. // isBeginRegion = false;
  579. // br = new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0);
  580. // tr = new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0);
  581. // // Transform coordinates to be relative to transform and account min descender and max ascender.
  582. // bl = rectTransform.TransformPoint(new Vector3(bl.x, minDescender, 0));
  583. // tl = rectTransform.TransformPoint(new Vector3(tl.x, maxAscender, 0));
  584. // tr = rectTransform.TransformPoint(new Vector3(tr.x, maxAscender, 0));
  585. // br = rectTransform.TransformPoint(new Vector3(br.x, minDescender, 0));
  586. // // Check for Intersection
  587. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  588. // return i;
  589. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  590. // }
  591. // }
  592. // // Last Character of Word
  593. // if (isBeginRegion && j == wInfo.characterCount - 1)
  594. // {
  595. // isBeginRegion = false;
  596. // br = new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0);
  597. // tr = new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0);
  598. // // Transform coordinates to be relative to transform and account min descender and max ascender.
  599. // bl = rectTransform.TransformPoint(new Vector3(bl.x, minDescender, 0));
  600. // tl = rectTransform.TransformPoint(new Vector3(tl.x, maxAscender, 0));
  601. // tr = rectTransform.TransformPoint(new Vector3(tr.x, maxAscender, 0));
  602. // br = rectTransform.TransformPoint(new Vector3(br.x, minDescender, 0));
  603. // // Check for Intersection
  604. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  605. // return i;
  606. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  607. // }
  608. // // If Word is split on more than one line.
  609. // else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  610. // {
  611. // isBeginRegion = false;
  612. // br = new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0);
  613. // tr = new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0);
  614. // // Transform coordinates to be relative to transform and account min descender and max ascender.
  615. // bl = rectTransform.TransformPoint(new Vector3(bl.x, minDescender, 0));
  616. // tl = rectTransform.TransformPoint(new Vector3(tl.x, maxAscender, 0));
  617. // tr = rectTransform.TransformPoint(new Vector3(tr.x, maxAscender, 0));
  618. // br = rectTransform.TransformPoint(new Vector3(br.x, minDescender, 0));
  619. // maxAscender = -Mathf.Infinity;
  620. // minDescender = Mathf.Infinity;
  621. // // Check for Intersection
  622. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  623. // return i;
  624. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  625. // }
  626. // }
  627. // //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  628. // }
  629. // return -1;
  630. //}
  631. /// <summary>
  632. /// Function returning the index of the word at the given position (if any).
  633. /// </summary>
  634. /// <param name="text">A reference to the TextMeshPro component.</param>
  635. /// <param name="position">Position to check for intersection.</param>
  636. /// <param name="camera">The camera which is rendering the text object.</param>
  637. /// <returns></returns>
  638. //public static int FindIntersectingWord(TextMeshPro text, Vector3 position, Camera camera)
  639. //{
  640. // Transform textTransform = text.transform;
  641. // // Convert position into Worldspace coordinates
  642. // ScreenPointToWorldPointInRectangle(textTransform, position, camera, out position);
  643. // for (int i = 0; i < text.textInfo.wordCount; i++)
  644. // {
  645. // TMP_WordInfo wInfo = text.textInfo.wordInfo[i];
  646. // bool isBeginRegion = false;
  647. // Vector3 bl = Vector3.zero;
  648. // Vector3 tl = Vector3.zero;
  649. // Vector3 br = Vector3.zero;
  650. // Vector3 tr = Vector3.zero;
  651. // float maxAscender = -Mathf.Infinity;
  652. // float minDescender = Mathf.Infinity;
  653. // // Iterate through each character of the word
  654. // for (int j = 0; j < wInfo.characterCount; j++)
  655. // {
  656. // int characterIndex = wInfo.firstCharacterIndex + j;
  657. // TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  658. // int currentLine = currentCharInfo.lineNumber;
  659. // bool isCharacterVisible = characterIndex > text.maxVisibleCharacters ||
  660. // currentCharInfo.lineNumber > text.maxVisibleLines ||
  661. // (text.OverflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != text.pageToDisplay) ? false : true;
  662. // // Track maximum Ascender and minimum Descender for each word.
  663. // maxAscender = Mathf.Max(maxAscender, currentCharInfo.ascender);
  664. // minDescender = Mathf.Min(minDescender, currentCharInfo.descender);
  665. // if (isBeginRegion == false && isCharacterVisible)
  666. // {
  667. // isBeginRegion = true;
  668. // bl = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0);
  669. // tl = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0);
  670. // //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  671. // // If Word is one character
  672. // if (wInfo.characterCount == 1)
  673. // {
  674. // isBeginRegion = false;
  675. // br = new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0);
  676. // tr = new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0);
  677. // // Transform coordinates to be relative to transform and account min descender and max ascender.
  678. // bl = textTransform.TransformPoint(new Vector3(bl.x, minDescender, 0));
  679. // tl = textTransform.TransformPoint(new Vector3(tl.x, maxAscender, 0));
  680. // tr = textTransform.TransformPoint(new Vector3(tr.x, maxAscender, 0));
  681. // br = textTransform.TransformPoint(new Vector3(br.x, minDescender, 0));
  682. // // Check for Intersection
  683. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  684. // return i;
  685. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  686. // }
  687. // }
  688. // // Last Character of Word
  689. // if (isBeginRegion && j == wInfo.characterCount - 1)
  690. // {
  691. // isBeginRegion = false;
  692. // br = new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0);
  693. // tr = new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0);
  694. // // Transform coordinates to be relative to transform and account min descender and max ascender.
  695. // bl = textTransform.TransformPoint(new Vector3(bl.x, minDescender, 0));
  696. // tl = textTransform.TransformPoint(new Vector3(tl.x, maxAscender, 0));
  697. // tr = textTransform.TransformPoint(new Vector3(tr.x, maxAscender, 0));
  698. // br = textTransform.TransformPoint(new Vector3(br.x, minDescender, 0));
  699. // // Check for Intersection
  700. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  701. // return i;
  702. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  703. // }
  704. // // If Word is split on more than one line.
  705. // else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  706. // {
  707. // isBeginRegion = false;
  708. // br = new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0);
  709. // tr = new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0);
  710. // // Transform coordinates to be relative to transform and account min descender and max ascender.
  711. // bl = textTransform.TransformPoint(new Vector3(bl.x, minDescender, 0));
  712. // tl = textTransform.TransformPoint(new Vector3(tl.x, maxAscender, 0));
  713. // tr = textTransform.TransformPoint(new Vector3(tr.x, maxAscender, 0));
  714. // br = textTransform.TransformPoint(new Vector3(br.x, minDescender, 0));
  715. // // Reset maxAscender and minDescender for next word segment.
  716. // maxAscender = -Mathf.Infinity;
  717. // minDescender = Mathf.Infinity;
  718. // // Check for Intersection
  719. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  720. // return i;
  721. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  722. // }
  723. // }
  724. // }
  725. // return -1;
  726. //}
  727. /// <summary>
  728. /// Function returning the index of the word at the given position (if any).
  729. /// </summary>
  730. /// <param name="text">A reference to the TMP_Text component.</param>
  731. /// <param name="position"></param>
  732. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  733. /// <returns></returns>
  734. public static int FindNearestWord(TMP_Text text, Vector3 position, Camera camera)
  735. {
  736. RectTransform rectTransform = text.rectTransform;
  737. float distanceSqr = Mathf.Infinity;
  738. int closest = 0;
  739. // Convert position into Worldspace coordinates
  740. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  741. for (int i = 0; i < text.textInfo.wordCount; i++)
  742. {
  743. TMP_WordInfo wInfo = text.textInfo.wordInfo[i];
  744. bool isBeginRegion = false;
  745. Vector3 bl = Vector3.zero;
  746. Vector3 tl = Vector3.zero;
  747. Vector3 br = Vector3.zero;
  748. Vector3 tr = Vector3.zero;
  749. // Iterate through each character of the word
  750. for (int j = 0; j < wInfo.characterCount; j++)
  751. {
  752. int characterIndex = wInfo.firstCharacterIndex + j;
  753. TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  754. int currentLine = currentCharInfo.lineNumber;
  755. bool isCharacterVisible = currentCharInfo.isVisible;
  756. if (isBeginRegion == false && isCharacterVisible)
  757. {
  758. isBeginRegion = true;
  759. bl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
  760. tl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));
  761. //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  762. // If Word is one character
  763. if (wInfo.characterCount == 1)
  764. {
  765. isBeginRegion = false;
  766. br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  767. tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  768. // Check for Intersection
  769. if (PointIntersectRectangle(position, bl, tl, tr, br))
  770. return i;
  771. // Find the closest line segment to position.
  772. float dbl = DistanceToLine(bl, tl, position);
  773. float dtl = DistanceToLine(tl, tr, position);
  774. float dtr = DistanceToLine(tr, br, position);
  775. float dbr = DistanceToLine(br, bl, position);
  776. float d = dbl < dtl ? dbl : dtl;
  777. d = d < dtr ? d : dtr;
  778. d = d < dbr ? d : dbr;
  779. if (distanceSqr > d)
  780. {
  781. distanceSqr = d;
  782. closest = i;
  783. }
  784. }
  785. }
  786. // Last Character of Word
  787. if (isBeginRegion && j == wInfo.characterCount - 1)
  788. {
  789. isBeginRegion = false;
  790. br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  791. tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  792. // Check for Intersection
  793. if (PointIntersectRectangle(position, bl, tl, tr, br))
  794. return i;
  795. // Find the closest line segment to position.
  796. float dbl = DistanceToLine(bl, tl, position);
  797. float dtl = DistanceToLine(tl, tr, position);
  798. float dtr = DistanceToLine(tr, br, position);
  799. float dbr = DistanceToLine(br, bl, position);
  800. float d = dbl < dtl ? dbl : dtl;
  801. d = d < dtr ? d : dtr;
  802. d = d < dbr ? d : dbr;
  803. if (distanceSqr > d)
  804. {
  805. distanceSqr = d;
  806. closest = i;
  807. }
  808. }
  809. // If Word is split on more than one line.
  810. else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  811. {
  812. isBeginRegion = false;
  813. br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  814. tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  815. // Check for Intersection
  816. if (PointIntersectRectangle(position, bl, tl, tr, br))
  817. return i;
  818. // Find the closest line segment to position.
  819. float dbl = DistanceToLine(bl, tl, position);
  820. float dtl = DistanceToLine(tl, tr, position);
  821. float dtr = DistanceToLine(tr, br, position);
  822. float dbr = DistanceToLine(br, bl, position);
  823. float d = dbl < dtl ? dbl : dtl;
  824. d = d < dtr ? d : dtr;
  825. d = d < dbr ? d : dbr;
  826. if (distanceSqr > d)
  827. {
  828. distanceSqr = d;
  829. closest = i;
  830. }
  831. }
  832. }
  833. //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  834. }
  835. return closest;
  836. }
  837. /// <summary>
  838. /// Function returning the index of the word at the given position (if any).
  839. /// </summary>
  840. /// <param name="text">A reference to the TextMeshPro UGUI component.</param>
  841. /// <param name="position"></param>
  842. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  843. /// <returns></returns>
  844. //public static int FindNearestWord(TextMeshProUGUI text, Vector3 position, Camera camera)
  845. //{
  846. // RectTransform rectTransform = text.rectTransform;
  847. // float distanceSqr = Mathf.Infinity;
  848. // int closest = 0;
  849. // // Convert position into Worldspace coordinates
  850. // ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  851. // for (int i = 0; i < text.textInfo.wordCount; i++)
  852. // {
  853. // TMP_WordInfo wInfo = text.textInfo.wordInfo[i];
  854. // bool isBeginRegion = false;
  855. // Vector3 bl = Vector3.zero;
  856. // Vector3 tl = Vector3.zero;
  857. // Vector3 br = Vector3.zero;
  858. // Vector3 tr = Vector3.zero;
  859. // // Iterate through each character of the word
  860. // for (int j = 0; j < wInfo.characterCount; j++)
  861. // {
  862. // int characterIndex = wInfo.firstCharacterIndex + j;
  863. // TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  864. // int currentLine = currentCharInfo.lineNumber;
  865. // bool isCharacterVisible = characterIndex > text.maxVisibleCharacters ||
  866. // currentCharInfo.lineNumber > text.maxVisibleLines ||
  867. // (text.OverflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != text.pageToDisplay) ? false : true;
  868. // if (isBeginRegion == false && isCharacterVisible)
  869. // {
  870. // isBeginRegion = true;
  871. // bl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
  872. // tl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));
  873. // //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  874. // // If Word is one character
  875. // if (wInfo.characterCount == 1)
  876. // {
  877. // isBeginRegion = false;
  878. // br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  879. // tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  880. // // Check for Intersection
  881. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  882. // return i;
  883. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  884. // }
  885. // }
  886. // // Last Character of Word
  887. // if (isBeginRegion && j == wInfo.characterCount - 1)
  888. // {
  889. // isBeginRegion = false;
  890. // br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  891. // tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  892. // // Check for Intersection
  893. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  894. // return i;
  895. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  896. // }
  897. // // If Word is split on more than one line.
  898. // else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  899. // {
  900. // isBeginRegion = false;
  901. // br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  902. // tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  903. // // Check for Intersection
  904. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  905. // return i;
  906. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  907. // }
  908. // }
  909. // // Find the closest line segment to position.
  910. // float dbl = DistanceToLine(bl, tl, position); // (position - bl).sqrMagnitude;
  911. // float dtl = DistanceToLine(tl, tr, position); // (position - tl).sqrMagnitude;
  912. // float dtr = DistanceToLine(tr, br, position); // (position - tr).sqrMagnitude;
  913. // float dbr = DistanceToLine(br, bl, position); // (position - br).sqrMagnitude;
  914. // float d = dbl < dtl ? dbl : dtl;
  915. // d = d < dtr ? d : dtr;
  916. // d = d < dbr ? d : dbr;
  917. // if (distanceSqr > d)
  918. // {
  919. // distanceSqr = d;
  920. // closest = i;
  921. // }
  922. // //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  923. // }
  924. // return closest;
  925. //}
  926. /// <summary>
  927. /// Function returning the index of the word at the given position (if any).
  928. /// </summary>
  929. /// <param name="text">A reference to the TextMeshPro UGUI component.</param>
  930. /// <param name="position">Position to check for intersection.</param>
  931. /// <param name="camera">The camera which is rendering the text object.</param>
  932. /// <returns></returns>
  933. //public static int FindNearestWord(TextMeshPro text, Vector3 position, Camera camera)
  934. //{
  935. // Transform textTransform = text.transform;
  936. // float distanceSqr = Mathf.Infinity;
  937. // int closest = 0;
  938. // // Convert position into Worldspace coordinates
  939. // ScreenPointToWorldPointInRectangle(textTransform, position, camera, out position);
  940. // for (int i = 0; i < text.textInfo.wordCount; i++)
  941. // {
  942. // TMP_WordInfo wInfo = text.textInfo.wordInfo[i];
  943. // bool isBeginRegion = false;
  944. // Vector3 bl = Vector3.zero;
  945. // Vector3 tl = Vector3.zero;
  946. // Vector3 br = Vector3.zero;
  947. // Vector3 tr = Vector3.zero;
  948. // // Iterate through each character of the word
  949. // for (int j = 0; j < wInfo.characterCount; j++)
  950. // {
  951. // int characterIndex = wInfo.firstCharacterIndex + j;
  952. // TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  953. // int currentLine = currentCharInfo.lineNumber;
  954. // bool isCharacterVisible = characterIndex > text.maxVisibleCharacters ||
  955. // currentCharInfo.lineNumber > text.maxVisibleLines ||
  956. // (text.OverflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != text.pageToDisplay) ? false : true;
  957. // if (isBeginRegion == false && isCharacterVisible)
  958. // {
  959. // isBeginRegion = true;
  960. // bl = textTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
  961. // tl = textTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));
  962. // //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  963. // // If Word is one character
  964. // if (wInfo.characterCount == 1)
  965. // {
  966. // isBeginRegion = false;
  967. // br = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  968. // tr = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  969. // // Check for Intersection
  970. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  971. // return i;
  972. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  973. // }
  974. // }
  975. // // Last Character of Word
  976. // if (isBeginRegion && j == wInfo.characterCount - 1)
  977. // {
  978. // isBeginRegion = false;
  979. // br = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  980. // tr = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  981. // // Check for Intersection
  982. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  983. // return i;
  984. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  985. // }
  986. // // If Word is split on more than one line.
  987. // else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  988. // {
  989. // isBeginRegion = false;
  990. // br = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  991. // tr = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  992. // // Check for Intersection
  993. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  994. // return i;
  995. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  996. // }
  997. // }
  998. // // Find the closest line segment to position.
  999. // float dbl = DistanceToLine(bl, tl, position);
  1000. // float dtl = DistanceToLine(tl, tr, position);
  1001. // float dtr = DistanceToLine(tr, br, position);
  1002. // float dbr = DistanceToLine(br, bl, position);
  1003. // float d = dbl < dtl ? dbl : dtl;
  1004. // d = d < dtr ? d : dtr;
  1005. // d = d < dbr ? d : dbr;
  1006. // if (distanceSqr > d)
  1007. // {
  1008. // distanceSqr = d;
  1009. // closest = i;
  1010. // }
  1011. // //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  1012. // }
  1013. // return closest;
  1014. //}
  1015. /// <summary>
  1016. /// Function returning the line intersecting the position.
  1017. /// </summary>
  1018. /// <param name="textComponent"></param>
  1019. /// <param name="position"></param>
  1020. /// <param name="camera"></param>
  1021. /// <returns></returns>
  1022. public static int FindIntersectingLine(TMP_Text text, Vector3 position, Camera camera)
  1023. {
  1024. RectTransform rectTransform = text.rectTransform;
  1025. int closest = -1;
  1026. // Convert position into Worldspace coordinates
  1027. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  1028. for (int i = 0; i < text.textInfo.lineCount; i++)
  1029. {
  1030. TMP_LineInfo lineInfo = text.textInfo.lineInfo[i];
  1031. float ascender = rectTransform.TransformPoint(new Vector3(0, lineInfo.ascender, 0)).y;
  1032. float descender = rectTransform.TransformPoint(new Vector3(0, lineInfo.descender, 0)).y;
  1033. if (ascender > position.y && descender < position.y)
  1034. {
  1035. //Debug.Log("Position is on line " + i);
  1036. return i;
  1037. }
  1038. }
  1039. //Debug.Log("Closest line to position is " + closest);
  1040. return closest;
  1041. }
  1042. /// <summary>
  1043. /// Function returning the index of the Link at the given position (if any).
  1044. /// </summary>
  1045. /// <param name="text">A reference to the TMP_Text component.</param>
  1046. /// <param name="position">Position to check for intersection.</param>
  1047. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  1048. /// <returns></returns>
  1049. public static int FindIntersectingLink(TMP_Text text, Vector3 position, Camera camera)
  1050. {
  1051. Transform rectTransform = text.transform;
  1052. // Convert position into Worldspace coordinates
  1053. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  1054. for (int i = 0; i < text.textInfo.linkCount; i++)
  1055. {
  1056. TMP_LinkInfo linkInfo = text.textInfo.linkInfo[i];
  1057. bool isBeginRegion = false;
  1058. Vector3 bl = Vector3.zero;
  1059. Vector3 tl = Vector3.zero;
  1060. Vector3 br = Vector3.zero;
  1061. Vector3 tr = Vector3.zero;
  1062. // Iterate through each character of the word
  1063. for (int j = 0; j < linkInfo.linkTextLength; j++)
  1064. {
  1065. int characterIndex = linkInfo.linkTextfirstCharacterIndex + j;
  1066. TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  1067. int currentLine = currentCharInfo.lineNumber;
  1068. // Check if Link characters are on the current page
  1069. if (text.overflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != text.pageToDisplay) continue;
  1070. if (isBeginRegion == false)
  1071. {
  1072. isBeginRegion = true;
  1073. bl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
  1074. tl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));
  1075. //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  1076. // If Word is one character
  1077. if (linkInfo.linkTextLength == 1)
  1078. {
  1079. isBeginRegion = false;
  1080. br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1081. tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1082. // Check for Intersection
  1083. if (PointIntersectRectangle(position, bl, tl, tr, br))
  1084. return i;
  1085. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1086. }
  1087. }
  1088. // Last Character of Word
  1089. if (isBeginRegion && j == linkInfo.linkTextLength - 1)
  1090. {
  1091. isBeginRegion = false;
  1092. br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1093. tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1094. // Check for Intersection
  1095. if (PointIntersectRectangle(position, bl, tl, tr, br))
  1096. return i;
  1097. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1098. }
  1099. // If Word is split on more than one line.
  1100. else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  1101. {
  1102. isBeginRegion = false;
  1103. br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1104. tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1105. // Check for Intersection
  1106. if (PointIntersectRectangle(position, bl, tl, tr, br))
  1107. return i;
  1108. //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1109. }
  1110. }
  1111. //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  1112. }
  1113. return -1;
  1114. }
  1115. /// <summary>
  1116. /// Function returning the index of the Link at the given position (if any).
  1117. /// </summary>
  1118. /// <param name="text">A reference to the TextMeshPro UGUI component.</param>
  1119. /// <param name="position">Position to check for intersection.</param>
  1120. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  1121. /// <returns></returns>
  1122. //public static int FindIntersectingLink(TextMeshProUGUI text, Vector3 position, Camera camera)
  1123. //{
  1124. // Transform rectTransform = text.transform;
  1125. // // Convert position into Worldspace coordinates
  1126. // ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  1127. // for (int i = 0; i < text.textInfo.linkCount; i++)
  1128. // {
  1129. // TMP_LinkInfo linkInfo = text.textInfo.linkInfo[i];
  1130. // bool isBeginRegion = false;
  1131. // Vector3 bl = Vector3.zero;
  1132. // Vector3 tl = Vector3.zero;
  1133. // Vector3 br = Vector3.zero;
  1134. // Vector3 tr = Vector3.zero;
  1135. // // Iterate through each character of the word
  1136. // for (int j = 0; j < linkInfo.linkTextLength; j++)
  1137. // {
  1138. // int characterIndex = linkInfo.linkTextfirstCharacterIndex + j;
  1139. // TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  1140. // int currentLine = currentCharInfo.lineNumber;
  1141. // // Check if Link characters are on the current page
  1142. // if (text.OverflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != text.pageToDisplay) continue;
  1143. // if (isBeginRegion == false)
  1144. // {
  1145. // isBeginRegion = true;
  1146. // bl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
  1147. // tl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));
  1148. // //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  1149. // // If Word is one character
  1150. // if (linkInfo.linkTextLength == 1)
  1151. // {
  1152. // isBeginRegion = false;
  1153. // br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1154. // tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1155. // // Check for Intersection
  1156. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1157. // return i;
  1158. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1159. // }
  1160. // }
  1161. // // Last Character of Word
  1162. // if (isBeginRegion && j == linkInfo.linkTextLength - 1)
  1163. // {
  1164. // isBeginRegion = false;
  1165. // br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1166. // tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1167. // // Check for Intersection
  1168. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1169. // return i;
  1170. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1171. // }
  1172. // // If Word is split on more than one line.
  1173. // else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  1174. // {
  1175. // isBeginRegion = false;
  1176. // br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1177. // tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1178. // // Check for Intersection
  1179. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1180. // return i;
  1181. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1182. // }
  1183. // }
  1184. // //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  1185. // }
  1186. // return -1;
  1187. //}
  1188. /// <summary>
  1189. /// Function returning the index of the Link at the given position (if any).
  1190. /// </summary>
  1191. /// <param name="text">A reference to the TextMeshPro component.</param>
  1192. /// <param name="position">Position to check for intersection.</param>
  1193. /// <param name="camera">The camera which is rendering the text object.</param>
  1194. /// <returns></returns>
  1195. //public static int FindIntersectingLink(TextMeshPro text, Vector3 position, Camera camera)
  1196. //{
  1197. // Transform textTransform = text.transform;
  1198. // // Convert position into Worldspace coordinates
  1199. // ScreenPointToWorldPointInRectangle(textTransform, position, camera, out position);
  1200. // for (int i = 0; i < text.textInfo.linkCount; i++)
  1201. // {
  1202. // TMP_LinkInfo linkInfo = text.textInfo.linkInfo[i];
  1203. // bool isBeginRegion = false;
  1204. // Vector3 bl = Vector3.zero;
  1205. // Vector3 tl = Vector3.zero;
  1206. // Vector3 br = Vector3.zero;
  1207. // Vector3 tr = Vector3.zero;
  1208. // // Iterate through each character of the word
  1209. // for (int j = 0; j < linkInfo.linkTextLength; j++)
  1210. // {
  1211. // int characterIndex = linkInfo.linkTextfirstCharacterIndex + j;
  1212. // TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  1213. // int currentLine = currentCharInfo.lineNumber;
  1214. // // Check if Link characters are on the current page
  1215. // if (text.OverflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != text.pageToDisplay) continue;
  1216. // if (isBeginRegion == false)
  1217. // {
  1218. // isBeginRegion = true;
  1219. // bl = textTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
  1220. // tl = textTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));
  1221. // //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  1222. // // If Word is one character
  1223. // if (linkInfo.linkTextLength == 1)
  1224. // {
  1225. // isBeginRegion = false;
  1226. // br = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1227. // tr = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1228. // // Check for Intersection
  1229. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1230. // return i;
  1231. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1232. // }
  1233. // }
  1234. // // Last Character of Word
  1235. // if (isBeginRegion && j == linkInfo.linkTextLength - 1)
  1236. // {
  1237. // isBeginRegion = false;
  1238. // br = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1239. // tr = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1240. // // Check for Intersection
  1241. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1242. // return i;
  1243. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1244. // }
  1245. // // If Word is split on more than one line.
  1246. // else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  1247. // {
  1248. // isBeginRegion = false;
  1249. // br = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1250. // tr = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1251. // // Check for Intersection
  1252. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1253. // return i;
  1254. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1255. // }
  1256. // }
  1257. // //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  1258. // }
  1259. // return -1;
  1260. //}
  1261. /// <summary>
  1262. /// Function returning the index of the word at the given position (if any).
  1263. /// </summary>
  1264. /// <param name="text">A reference to the TMP_Text component.</param>
  1265. /// <param name="position">Position to check for intersection.</param>
  1266. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  1267. /// <returns></returns>
  1268. public static int FindNearestLink(TMP_Text text, Vector3 position, Camera camera)
  1269. {
  1270. RectTransform rectTransform = text.rectTransform;
  1271. // Convert position into Worldspace coordinates
  1272. ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  1273. float distanceSqr = Mathf.Infinity;
  1274. int closest = 0;
  1275. for (int i = 0; i < text.textInfo.linkCount; i++)
  1276. {
  1277. TMP_LinkInfo linkInfo = text.textInfo.linkInfo[i];
  1278. bool isBeginRegion = false;
  1279. Vector3 bl = Vector3.zero;
  1280. Vector3 tl = Vector3.zero;
  1281. Vector3 br = Vector3.zero;
  1282. Vector3 tr = Vector3.zero;
  1283. // Iterate through each character of the link
  1284. for (int j = 0; j < linkInfo.linkTextLength; j++)
  1285. {
  1286. int characterIndex = linkInfo.linkTextfirstCharacterIndex + j;
  1287. TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  1288. int currentLine = currentCharInfo.lineNumber;
  1289. // Check if Link characters are on the current page
  1290. if (text.overflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != text.pageToDisplay) continue;
  1291. if (isBeginRegion == false)
  1292. {
  1293. isBeginRegion = true;
  1294. //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  1295. bl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
  1296. tl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));
  1297. // If Link is one character
  1298. if (linkInfo.linkTextLength == 1)
  1299. {
  1300. isBeginRegion = false;
  1301. br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1302. tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1303. // Check for Intersection
  1304. if (PointIntersectRectangle(position, bl, tl, tr, br))
  1305. return i;
  1306. // Find the closest line segment to position.
  1307. float dbl = DistanceToLine(bl, tl, position);
  1308. float dtl = DistanceToLine(tl, tr, position);
  1309. float dtr = DistanceToLine(tr, br, position);
  1310. float dbr = DistanceToLine(br, bl, position);
  1311. float d = dbl < dtl ? dbl : dtl;
  1312. d = d < dtr ? d : dtr;
  1313. d = d < dbr ? d : dbr;
  1314. if (distanceSqr > d)
  1315. {
  1316. distanceSqr = d;
  1317. closest = i;
  1318. }
  1319. }
  1320. }
  1321. // Last Character of Word
  1322. if (isBeginRegion && j == linkInfo.linkTextLength - 1)
  1323. {
  1324. isBeginRegion = false;
  1325. br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1326. tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1327. // Check for Intersection
  1328. if (PointIntersectRectangle(position, bl, tl, tr, br))
  1329. return i;
  1330. // Find the closest line segment to position.
  1331. float dbl = DistanceToLine(bl, tl, position);
  1332. float dtl = DistanceToLine(tl, tr, position);
  1333. float dtr = DistanceToLine(tr, br, position);
  1334. float dbr = DistanceToLine(br, bl, position);
  1335. float d = dbl < dtl ? dbl : dtl;
  1336. d = d < dtr ? d : dtr;
  1337. d = d < dbr ? d : dbr;
  1338. if (distanceSqr > d)
  1339. {
  1340. distanceSqr = d;
  1341. closest = i;
  1342. }
  1343. }
  1344. // If Link is split on more than one line.
  1345. else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  1346. {
  1347. isBeginRegion = false;
  1348. br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1349. tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1350. // Check for Intersection
  1351. if (PointIntersectRectangle(position, bl, tl, tr, br))
  1352. return i;
  1353. // Find the closest line segment to position.
  1354. float dbl = DistanceToLine(bl, tl, position);
  1355. float dtl = DistanceToLine(tl, tr, position);
  1356. float dtr = DistanceToLine(tr, br, position);
  1357. float dbr = DistanceToLine(br, bl, position);
  1358. float d = dbl < dtl ? dbl : dtl;
  1359. d = d < dtr ? d : dtr;
  1360. d = d < dbr ? d : dbr;
  1361. if (distanceSqr > d)
  1362. {
  1363. distanceSqr = d;
  1364. closest = i;
  1365. }
  1366. }
  1367. }
  1368. //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  1369. }
  1370. return closest;
  1371. }
  1372. /// <summary>
  1373. /// Function returning the index of the word at the given position (if any).
  1374. /// </summary>
  1375. /// <param name="text">A reference to the TextMeshPro UGUI component.</param>
  1376. /// <param name="position">Position to check for intersection.</param>
  1377. /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param>
  1378. /// <returns></returns>
  1379. //public static int FindNearestLink(TextMeshProUGUI text, Vector3 position, Camera camera)
  1380. //{
  1381. // RectTransform rectTransform = text.rectTransform;
  1382. // // Convert position into Worldspace coordinates
  1383. // ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position);
  1384. // float distanceSqr = Mathf.Infinity;
  1385. // int closest = 0;
  1386. // for (int i = 0; i < text.textInfo.linkCount; i++)
  1387. // {
  1388. // TMP_LinkInfo linkInfo = text.textInfo.linkInfo[i];
  1389. // bool isBeginRegion = false;
  1390. // Vector3 bl = Vector3.zero;
  1391. // Vector3 tl = Vector3.zero;
  1392. // Vector3 br = Vector3.zero;
  1393. // Vector3 tr = Vector3.zero;
  1394. // // Iterate through each character of the word
  1395. // for (int j = 0; j < linkInfo.linkTextLength; j++)
  1396. // {
  1397. // int characterIndex = linkInfo.linkTextfirstCharacterIndex + j;
  1398. // TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  1399. // int currentLine = currentCharInfo.lineNumber;
  1400. // if (isBeginRegion == false)
  1401. // {
  1402. // isBeginRegion = true;
  1403. // bl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
  1404. // tl = rectTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));
  1405. // //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  1406. // // If Word is one character
  1407. // if (linkInfo.linkTextLength == 1)
  1408. // {
  1409. // isBeginRegion = false;
  1410. // br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1411. // tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1412. // // Check for Intersection
  1413. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1414. // return i;
  1415. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1416. // }
  1417. // }
  1418. // // Last Character of Word
  1419. // if (isBeginRegion && j == linkInfo.linkTextLength - 1)
  1420. // {
  1421. // isBeginRegion = false;
  1422. // br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1423. // tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1424. // // Check for Intersection
  1425. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1426. // return i;
  1427. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1428. // }
  1429. // // If Word is split on more than one line.
  1430. // else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  1431. // {
  1432. // isBeginRegion = false;
  1433. // br = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1434. // tr = rectTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1435. // // Check for Intersection
  1436. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1437. // return i;
  1438. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1439. // }
  1440. // }
  1441. // // Find the closest line segment to position.
  1442. // float dbl = DistanceToLine(bl, tl, position); // (position - bl).sqrMagnitude;
  1443. // float dtl = DistanceToLine(tl, tr, position); // (position - tl).sqrMagnitude;
  1444. // float dtr = DistanceToLine(tr, br, position); // (position - tr).sqrMagnitude;
  1445. // float dbr = DistanceToLine(br, bl, position); // (position - br).sqrMagnitude;
  1446. // float d = dbl < dtl ? dbl : dtl;
  1447. // d = d < dtr ? d : dtr;
  1448. // d = d < dbr ? d : dbr;
  1449. // if (distanceSqr > d)
  1450. // {
  1451. // distanceSqr = d;
  1452. // closest = i;
  1453. // }
  1454. // //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  1455. // }
  1456. // return closest;
  1457. //}
  1458. /// <summary>
  1459. /// Function returning the index of the word at the given position (if any).
  1460. /// </summary>
  1461. /// <param name="text">A reference to the TextMeshPro component.</param>
  1462. /// <param name="position">Position to check for intersection.</param>
  1463. /// <param name="camera">The camera which is rendering the text object.</param>
  1464. /// <returns></returns>
  1465. //public static int FindNearestLink(TextMeshPro text, Vector3 position, Camera camera)
  1466. //{
  1467. // Transform textTransform = text.transform;
  1468. // // Convert position into Worldspace coordinates
  1469. // ScreenPointToWorldPointInRectangle(textTransform, position, camera, out position);
  1470. // float distanceSqr = Mathf.Infinity;
  1471. // int closest = 0;
  1472. // for (int i = 0; i < text.textInfo.linkCount; i++)
  1473. // {
  1474. // TMP_LinkInfo linkInfo = text.textInfo.linkInfo[i];
  1475. // bool isBeginRegion = false;
  1476. // Vector3 bl = Vector3.zero;
  1477. // Vector3 tl = Vector3.zero;
  1478. // Vector3 br = Vector3.zero;
  1479. // Vector3 tr = Vector3.zero;
  1480. // // Iterate through each character of the word
  1481. // for (int j = 0; j < linkInfo.linkTextLength; j++)
  1482. // {
  1483. // int characterIndex = linkInfo.linkTextfirstCharacterIndex + j;
  1484. // TMP_CharacterInfo currentCharInfo = text.textInfo.characterInfo[characterIndex];
  1485. // int currentLine = currentCharInfo.lineNumber;
  1486. // if (isBeginRegion == false)
  1487. // {
  1488. // isBeginRegion = true;
  1489. // bl = textTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0));
  1490. // tl = textTransform.TransformPoint(new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0));
  1491. // //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  1492. // // If Word is one character
  1493. // if (linkInfo.linkTextLength == 1)
  1494. // {
  1495. // isBeginRegion = false;
  1496. // br = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1497. // tr = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1498. // // Check for Intersection
  1499. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1500. // return i;
  1501. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1502. // }
  1503. // }
  1504. // // Last Character of Word
  1505. // if (isBeginRegion && j == linkInfo.linkTextLength - 1)
  1506. // {
  1507. // isBeginRegion = false;
  1508. // br = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1509. // tr = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1510. // // Check for Intersection
  1511. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1512. // return i;
  1513. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1514. // }
  1515. // // If Word is split on more than one line.
  1516. // else if (isBeginRegion && currentLine != text.textInfo.characterInfo[characterIndex + 1].lineNumber)
  1517. // {
  1518. // isBeginRegion = false;
  1519. // br = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.descender, 0));
  1520. // tr = textTransform.TransformPoint(new Vector3(currentCharInfo.topRight.x, currentCharInfo.ascender, 0));
  1521. // // Check for Intersection
  1522. // if (PointIntersectRectangle(position, bl, tl, tr, br))
  1523. // return i;
  1524. // //Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  1525. // }
  1526. // }
  1527. // // Find the closest line segment to position.
  1528. // float dbl = DistanceToLine(bl, tl, position);
  1529. // float dtl = DistanceToLine(tl, tr, position);
  1530. // float dtr = DistanceToLine(tr, br, position);
  1531. // float dbr = DistanceToLine(br, bl, position);
  1532. // float d = dbl < dtl ? dbl : dtl;
  1533. // d = d < dtr ? d : dtr;
  1534. // d = d < dbr ? d : dbr;
  1535. // if (distanceSqr > d)
  1536. // {
  1537. // distanceSqr = d;
  1538. // closest = i;
  1539. // }
  1540. // //Debug.Log("Word at Index: " + i + " is located at (" + bl + ", " + tl + ", " + tr + ", " + br + ").");
  1541. // }
  1542. // return closest;
  1543. //}
  1544. /// <summary>
  1545. /// Function to check if a Point is contained within a Rectangle.
  1546. /// </summary>
  1547. /// <param name="m"></param>
  1548. /// <param name="a"></param>
  1549. /// <param name="b"></param>
  1550. /// <param name="c"></param>
  1551. /// <param name="d"></param>
  1552. /// <returns></returns>
  1553. private static bool PointIntersectRectangle(Vector3 m, Vector3 a, Vector3 b, Vector3 c, Vector3 d)
  1554. {
  1555. Vector3 ab = b - a;
  1556. Vector3 am = m - a;
  1557. Vector3 bc = c - b;
  1558. Vector3 bm = m - b;
  1559. float abamDot = Vector3.Dot(ab, am);
  1560. float bcbmDot = Vector3.Dot(bc, bm);
  1561. return 0 <= abamDot && abamDot <= Vector3.Dot(ab, ab) && 0 <= bcbmDot && bcbmDot <= Vector3.Dot(bc, bc);
  1562. }
  1563. /// <summary>
  1564. /// Method to convert ScreenPoint to WorldPoint aligned with Rectangle
  1565. /// </summary>
  1566. /// <param name="transform"></param>
  1567. /// <param name="screenPoint"></param>
  1568. /// <param name="cam"></param>
  1569. /// <param name="worldPoint"></param>
  1570. /// <returns></returns>
  1571. public static bool ScreenPointToWorldPointInRectangle(Transform transform, Vector2 screenPoint, Camera cam, out Vector3 worldPoint)
  1572. {
  1573. worldPoint = (Vector3)Vector2.zero;
  1574. Ray ray = RectTransformUtility.ScreenPointToRay(cam, screenPoint);
  1575. float enter;
  1576. if (!new Plane(transform.rotation * Vector3.back, transform.position).Raycast(ray, out enter))
  1577. return false;
  1578. worldPoint = ray.GetPoint(enter);
  1579. return true;
  1580. }
  1581. private struct LineSegment
  1582. {
  1583. public Vector3 Point1;
  1584. public Vector3 Point2;
  1585. public LineSegment(Vector3 p1, Vector3 p2)
  1586. {
  1587. Point1 = p1;
  1588. Point2 = p2;
  1589. }
  1590. }
  1591. /// <summary>
  1592. /// Function returning the point of intersection between a line and a plane.
  1593. /// </summary>
  1594. /// <param name="line"></param>
  1595. /// <param name="point"></param>
  1596. /// <param name="normal"></param>
  1597. /// <param name="intersectingPoint"></param>
  1598. /// <returns></returns>
  1599. private static bool IntersectLinePlane(LineSegment line, Vector3 point, Vector3 normal, out Vector3 intersectingPoint)
  1600. {
  1601. intersectingPoint = Vector3.zero;
  1602. Vector3 u = line.Point2 - line.Point1;
  1603. Vector3 w = line.Point1 - point;
  1604. float D = Vector3.Dot(normal, u);
  1605. float N = -Vector3.Dot(normal, w);
  1606. if (Mathf.Abs(D) < Mathf.Epsilon) // if line is parallel & co-planar to plane
  1607. {
  1608. if (N == 0)
  1609. return true;
  1610. else
  1611. return false;
  1612. }
  1613. float sI = N / D;
  1614. if (sI < 0 || sI > 1) // Line parallel to plane
  1615. return false;
  1616. intersectingPoint = line.Point1 + sI * u;
  1617. return true;
  1618. }
  1619. /// <summary>
  1620. /// Function returning the Square Distance from a Point to a Line.
  1621. /// </summary>
  1622. /// <param name="a"></param>
  1623. /// <param name="b"></param>
  1624. /// <param name="point"></param>
  1625. /// <returns></returns>
  1626. public static float DistanceToLine(Vector3 a, Vector3 b, Vector3 point)
  1627. {
  1628. Vector3 n = b - a;
  1629. Vector3 pa = a - point;
  1630. float c = Vector3.Dot( n, pa );
  1631. // Closest point is a
  1632. if ( c > 0.0f )
  1633. return Vector3.Dot( pa, pa );
  1634. Vector3 bp = point - b;
  1635. // Closest point is b
  1636. if (Vector3.Dot( n, bp ) > 0.0f )
  1637. return Vector3.Dot( bp, bp );
  1638. // Closest point is between a and b
  1639. Vector3 e = pa - n * (c / Vector3.Dot( n, n ));
  1640. return Vector3.Dot( e, e );
  1641. }
  1642. /// <summary>
  1643. /// Function returning the Square Distance from a Point to a Line and Direction.
  1644. /// </summary>
  1645. /// <param name="a"></param>
  1646. /// <param name="b"></param>
  1647. /// <param name="point"></param>
  1648. /// <param name="direction">-1 left, 0 in between, 1 right</param>
  1649. /// <returns></returns>
  1650. //public static float DistanceToLineDirectional(Vector3 a, Vector3 b, Vector3 point, ref int direction)
  1651. //{
  1652. // Vector3 n = b - a;
  1653. // Vector3 pa = a - point;
  1654. // float c = Vector3.Dot(n, pa);
  1655. // direction = -1;
  1656. // // Closest point is a
  1657. // if (c > 0.0f)
  1658. // return Vector3.Dot(pa, pa);
  1659. // Vector3 bp = point - b;
  1660. // direction = 1;
  1661. // // Closest point is b
  1662. // if (Vector3.Dot(n, bp) > 0.0f)
  1663. // return Vector3.Dot(bp, bp);
  1664. // // Closest point is between a and b
  1665. // Vector3 e = pa - n * (c / Vector3.Dot(n, n));
  1666. // direction = 0;
  1667. // return Vector3.Dot(e, e);
  1668. //}
  1669. /// <summary>
  1670. /// Table used to convert character to lowercase.
  1671. /// </summary>
  1672. const string k_lookupStringL = "-------------------------------- !-#$%&-()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[-]^_`abcdefghijklmnopqrstuvwxyz{|}~-";
  1673. /// <summary>
  1674. /// Table used to convert character to uppercase.
  1675. /// </summary>
  1676. const string k_lookupStringU = "-------------------------------- !-#$%&-()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[-]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~-";
  1677. /// <summary>
  1678. /// Get lowercase version of this ASCII character.
  1679. /// </summary>
  1680. public static char ToLowerFast(char c)
  1681. {
  1682. if (c > k_lookupStringL.Length - 1)
  1683. return c;
  1684. return k_lookupStringL[c];
  1685. }
  1686. /// <summary>
  1687. /// Get uppercase version of this ASCII character.
  1688. /// </summary>
  1689. public static char ToUpperFast(char c)
  1690. {
  1691. if (c > k_lookupStringU.Length - 1)
  1692. return c;
  1693. return k_lookupStringU[c];
  1694. }
  1695. /// <summary>
  1696. /// Get uppercase version of this ASCII character.
  1697. /// </summary>
  1698. internal static uint ToUpperASCIIFast(uint c)
  1699. {
  1700. if (c > k_lookupStringU.Length - 1)
  1701. return c;
  1702. return k_lookupStringU[(int)c];
  1703. }
  1704. /// <summary>
  1705. /// Returns the case insensitive hashcode for the given string.
  1706. /// </summary>
  1707. /// <param name="s"></param>
  1708. /// <returns></returns>
  1709. public static int GetHashCode(string s)
  1710. {
  1711. if (string.IsNullOrEmpty(s))
  1712. return 0;
  1713. int hashCode = 0;
  1714. for (int i = 0; i < s.Length; i++)
  1715. hashCode = ((hashCode << 5) + hashCode) ^ ToUpperFast(s[i]);
  1716. return hashCode;
  1717. }
  1718. /// <summary>
  1719. /// Function which returns a simple hashcode from a string.
  1720. /// </summary>
  1721. /// <returns></returns>
  1722. public static int GetSimpleHashCode(string s)
  1723. {
  1724. int hashCode = 0;
  1725. for (int i = 0; i < s.Length; i++)
  1726. hashCode = ((hashCode << 5) + hashCode) ^ s[i];
  1727. return hashCode;
  1728. }
  1729. /// <summary>
  1730. /// Function which returns a simple hashcode from a string converted to lowercase.
  1731. /// </summary>
  1732. /// <returns></returns>
  1733. public static uint GetSimpleHashCodeLowercase(string s)
  1734. {
  1735. uint hashCode = 5381;
  1736. for (int i = 0; i < s.Length; i++)
  1737. hashCode = (hashCode << 5) + hashCode ^ ToLowerFast(s[i]);
  1738. return hashCode;
  1739. }
  1740. /// <summary>
  1741. /// Function which returns a simple hash code from a string converted to uppercase.
  1742. /// </summary>
  1743. /// <param name="s">The string from which to compute the hash code.</param>
  1744. /// <returns>The computed hash code.</returns>
  1745. public static uint GetHashCodeCaseInSensitive(string s)
  1746. {
  1747. uint hashCode = 0;
  1748. for (int i = 0; i < s.Length; i++)
  1749. hashCode = (hashCode << 5) + hashCode ^ ToUpperFast(s[i]);
  1750. return hashCode;
  1751. }
  1752. /// <summary>
  1753. /// Function to convert Hex to Int
  1754. /// </summary>
  1755. /// <param name="hex"></param>
  1756. /// <returns></returns>
  1757. public static int HexToInt(char hex)
  1758. {
  1759. switch (hex)
  1760. {
  1761. case '0': return 0;
  1762. case '1': return 1;
  1763. case '2': return 2;
  1764. case '3': return 3;
  1765. case '4': return 4;
  1766. case '5': return 5;
  1767. case '6': return 6;
  1768. case '7': return 7;
  1769. case '8': return 8;
  1770. case '9': return 9;
  1771. case 'A': return 10;
  1772. case 'B': return 11;
  1773. case 'C': return 12;
  1774. case 'D': return 13;
  1775. case 'E': return 14;
  1776. case 'F': return 15;
  1777. case 'a': return 10;
  1778. case 'b': return 11;
  1779. case 'c': return 12;
  1780. case 'd': return 13;
  1781. case 'e': return 14;
  1782. case 'f': return 15;
  1783. }
  1784. return 15;
  1785. }
  1786. /// <summary>
  1787. /// Function to convert a properly formatted string which contains an hex value to its decimal value.
  1788. /// </summary>
  1789. /// <param name="s"></param>
  1790. /// <returns></returns>
  1791. public static int StringHexToInt(string s)
  1792. {
  1793. int value = 0;
  1794. for (int i = 0; i < s.Length; i++)
  1795. {
  1796. value += HexToInt(s[i]) * (int)Mathf.Pow(16, (s.Length - 1) - i);
  1797. }
  1798. return value;
  1799. }
  1800. }
  1801. }