TMP_TextEventHandler.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using UnityEngine.EventSystems;
  4. using System;
  5. namespace TMPro
  6. {
  7. public class TMP_TextEventHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  8. {
  9. [Serializable]
  10. public class CharacterSelectionEvent : UnityEvent<char, int> { }
  11. [Serializable]
  12. public class SpriteSelectionEvent : UnityEvent<char, int> { }
  13. [Serializable]
  14. public class WordSelectionEvent : UnityEvent<string, int, int> { }
  15. [Serializable]
  16. public class LineSelectionEvent : UnityEvent<string, int, int> { }
  17. [Serializable]
  18. public class LinkSelectionEvent : UnityEvent<string, string, int> { }
  19. /// <summary>
  20. /// Event delegate triggered when pointer is over a character.
  21. /// </summary>
  22. public CharacterSelectionEvent onCharacterSelection
  23. {
  24. get { return m_OnCharacterSelection; }
  25. set { m_OnCharacterSelection = value; }
  26. }
  27. [SerializeField]
  28. private CharacterSelectionEvent m_OnCharacterSelection = new CharacterSelectionEvent();
  29. /// <summary>
  30. /// Event delegate triggered when pointer is over a sprite.
  31. /// </summary>
  32. public SpriteSelectionEvent onSpriteSelection
  33. {
  34. get { return m_OnSpriteSelection; }
  35. set { m_OnSpriteSelection = value; }
  36. }
  37. [SerializeField]
  38. private SpriteSelectionEvent m_OnSpriteSelection = new SpriteSelectionEvent();
  39. /// <summary>
  40. /// Event delegate triggered when pointer is over a word.
  41. /// </summary>
  42. public WordSelectionEvent onWordSelection
  43. {
  44. get { return m_OnWordSelection; }
  45. set { m_OnWordSelection = value; }
  46. }
  47. [SerializeField]
  48. private WordSelectionEvent m_OnWordSelection = new WordSelectionEvent();
  49. /// <summary>
  50. /// Event delegate triggered when pointer is over a line.
  51. /// </summary>
  52. public LineSelectionEvent onLineSelection
  53. {
  54. get { return m_OnLineSelection; }
  55. set { m_OnLineSelection = value; }
  56. }
  57. [SerializeField]
  58. private LineSelectionEvent m_OnLineSelection = new LineSelectionEvent();
  59. /// <summary>
  60. /// Event delegate triggered when pointer is over a link.
  61. /// </summary>
  62. public LinkSelectionEvent onLinkSelection
  63. {
  64. get { return m_OnLinkSelection; }
  65. set { m_OnLinkSelection = value; }
  66. }
  67. [SerializeField]
  68. private LinkSelectionEvent m_OnLinkSelection = new LinkSelectionEvent();
  69. private TMP_Text m_TextComponent;
  70. private Camera m_Camera;
  71. private Canvas m_Canvas;
  72. private int m_selectedLink = -1;
  73. private int m_lastCharIndex = -1;
  74. private int m_lastWordIndex = -1;
  75. private int m_lastLineIndex = -1;
  76. void Awake()
  77. {
  78. // Get a reference to the text component.
  79. m_TextComponent = gameObject.GetComponent<TMP_Text>();
  80. // Get a reference to the camera rendering the text taking into consideration the text component type.
  81. if (m_TextComponent.GetType() == typeof(TextMeshProUGUI))
  82. {
  83. m_Canvas = gameObject.GetComponentInParent<Canvas>();
  84. if (m_Canvas != null)
  85. {
  86. if (m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
  87. m_Camera = null;
  88. else
  89. m_Camera = m_Canvas.worldCamera;
  90. }
  91. }
  92. else
  93. {
  94. m_Camera = Camera.main;
  95. }
  96. }
  97. void LateUpdate()
  98. {
  99. if (TMP_TextUtilities.IsIntersectingRectTransform(m_TextComponent.rectTransform, Input.mousePosition, m_Camera))
  100. {
  101. #region Example of Character or Sprite Selection
  102. int charIndex = TMP_TextUtilities.FindIntersectingCharacter(m_TextComponent, Input.mousePosition, m_Camera, true);
  103. if (charIndex != -1 && charIndex != m_lastCharIndex)
  104. {
  105. m_lastCharIndex = charIndex;
  106. TMP_TextElementType elementType = m_TextComponent.textInfo.characterInfo[charIndex].elementType;
  107. // Send event to any event listeners depending on whether it is a character or sprite.
  108. if (elementType == TMP_TextElementType.Character)
  109. SendOnCharacterSelection(m_TextComponent.textInfo.characterInfo[charIndex].character, charIndex);
  110. else if (elementType == TMP_TextElementType.Sprite)
  111. SendOnSpriteSelection(m_TextComponent.textInfo.characterInfo[charIndex].character, charIndex);
  112. }
  113. #endregion
  114. #region Example of Word Selection
  115. // Check if Mouse intersects any words and if so assign a random color to that word.
  116. int wordIndex = TMP_TextUtilities.FindIntersectingWord(m_TextComponent, Input.mousePosition, m_Camera);
  117. if (wordIndex != -1 && wordIndex != m_lastWordIndex)
  118. {
  119. m_lastWordIndex = wordIndex;
  120. // Get the information about the selected word.
  121. TMP_WordInfo wInfo = m_TextComponent.textInfo.wordInfo[wordIndex];
  122. // Send the event to any listeners.
  123. SendOnWordSelection(wInfo.GetWord(), wInfo.firstCharacterIndex, wInfo.characterCount);
  124. }
  125. #endregion
  126. #region Example of Line Selection
  127. // Check if Mouse intersects any words and if so assign a random color to that word.
  128. int lineIndex = TMP_TextUtilities.FindIntersectingLine(m_TextComponent, Input.mousePosition, m_Camera);
  129. if (lineIndex != -1 && lineIndex != m_lastLineIndex)
  130. {
  131. m_lastLineIndex = lineIndex;
  132. // Get the information about the selected word.
  133. TMP_LineInfo lineInfo = m_TextComponent.textInfo.lineInfo[lineIndex];
  134. // Send the event to any listeners.
  135. char[] buffer = new char[lineInfo.characterCount];
  136. for (int i = 0; i < lineInfo.characterCount && i < m_TextComponent.textInfo.characterInfo.Length; i++)
  137. {
  138. buffer[i] = m_TextComponent.textInfo.characterInfo[i + lineInfo.firstCharacterIndex].character;
  139. }
  140. string lineText = new string(buffer);
  141. SendOnLineSelection(lineText, lineInfo.firstCharacterIndex, lineInfo.characterCount);
  142. }
  143. #endregion
  144. #region Example of Link Handling
  145. // Check if mouse intersects with any links.
  146. int linkIndex = TMP_TextUtilities.FindIntersectingLink(m_TextComponent, Input.mousePosition, m_Camera);
  147. // Handle new Link selection.
  148. if (linkIndex != -1 && linkIndex != m_selectedLink)
  149. {
  150. m_selectedLink = linkIndex;
  151. // Get information about the link.
  152. TMP_LinkInfo linkInfo = m_TextComponent.textInfo.linkInfo[linkIndex];
  153. // Send the event to any listeners.
  154. SendOnLinkSelection(linkInfo.GetLinkID(), linkInfo.GetLinkText(), linkIndex);
  155. }
  156. #endregion
  157. }
  158. }
  159. public void OnPointerEnter(PointerEventData eventData)
  160. {
  161. //Debug.Log("OnPointerEnter()");
  162. }
  163. public void OnPointerExit(PointerEventData eventData)
  164. {
  165. //Debug.Log("OnPointerExit()");
  166. }
  167. private void SendOnCharacterSelection(char character, int characterIndex)
  168. {
  169. if (onCharacterSelection != null)
  170. onCharacterSelection.Invoke(character, characterIndex);
  171. }
  172. private void SendOnSpriteSelection(char character, int characterIndex)
  173. {
  174. if (onSpriteSelection != null)
  175. onSpriteSelection.Invoke(character, characterIndex);
  176. }
  177. private void SendOnWordSelection(string word, int charIndex, int length)
  178. {
  179. if (onWordSelection != null)
  180. onWordSelection.Invoke(word, charIndex, length);
  181. }
  182. private void SendOnLineSelection(string line, int charIndex, int length)
  183. {
  184. if (onLineSelection != null)
  185. onLineSelection.Invoke(line, charIndex, length);
  186. }
  187. private void SendOnLinkSelection(string linkID, string linkText, int linkIndex)
  188. {
  189. if (onLinkSelection != null)
  190. onLinkSelection.Invoke(linkID, linkText, linkIndex);
  191. }
  192. }
  193. }