暫無描述
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_TextEventHandler.cs 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 Nearest Character
  102. /*int charIndex = TMP_TextUtilities.FindNearestCharacterOnLine(m_TextComponent, Input.mousePosition, 0, m_Camera, false);
  103. if (charIndex != -1 && charIndex != m_lastCharIndex)
  104. {
  105. m_lastCharIndex = charIndex;
  106. }*/
  107. #endregion
  108. #region Example of Character or Sprite Selection
  109. int charIndex = TMP_TextUtilities.FindIntersectingCharacter(m_TextComponent, Input.mousePosition, m_Camera, true);
  110. if (charIndex != -1 && charIndex != m_lastCharIndex)
  111. {
  112. m_lastCharIndex = charIndex;
  113. TMP_TextElementType elementType = m_TextComponent.textInfo.characterInfo[charIndex].elementType;
  114. // Send event to any event listeners depending on whether it is a character or sprite.
  115. if (elementType == TMP_TextElementType.Character)
  116. SendOnCharacterSelection(m_TextComponent.textInfo.characterInfo[charIndex].character, charIndex);
  117. else if (elementType == TMP_TextElementType.Sprite)
  118. SendOnSpriteSelection(m_TextComponent.textInfo.characterInfo[charIndex].character, charIndex);
  119. }
  120. #endregion
  121. #region Example of Word Selection
  122. // Check if Mouse intersects any words and if so assign a random color to that word.
  123. int wordIndex = TMP_TextUtilities.FindIntersectingWord(m_TextComponent, Input.mousePosition, m_Camera);
  124. if (wordIndex != -1 && wordIndex != m_lastWordIndex)
  125. {
  126. m_lastWordIndex = wordIndex;
  127. // Get the information about the selected word.
  128. TMP_WordInfo wInfo = m_TextComponent.textInfo.wordInfo[wordIndex];
  129. // Send the event to any listeners.
  130. SendOnWordSelection(wInfo.GetWord(), wInfo.firstCharacterIndex, wInfo.characterCount);
  131. }
  132. #endregion
  133. #region Example of Line Selection
  134. // Check if Mouse intersects any words and if so assign a random color to that word.
  135. int lineIndex = TMP_TextUtilities.FindIntersectingLine(m_TextComponent, Input.mousePosition, m_Camera);
  136. if (lineIndex != -1 && lineIndex != m_lastLineIndex)
  137. {
  138. m_lastLineIndex = lineIndex;
  139. // Get the information about the selected word.
  140. TMP_LineInfo lineInfo = m_TextComponent.textInfo.lineInfo[lineIndex];
  141. // Send the event to any listeners.
  142. char[] buffer = new char[lineInfo.characterCount];
  143. for (int i = 0; i < lineInfo.characterCount && i < m_TextComponent.textInfo.characterInfo.Length; i++)
  144. {
  145. buffer[i] = m_TextComponent.textInfo.characterInfo[i + lineInfo.firstCharacterIndex].character;
  146. }
  147. string lineText = new string(buffer);
  148. SendOnLineSelection(lineText, lineInfo.firstCharacterIndex, lineInfo.characterCount);
  149. }
  150. #endregion
  151. #region Example of Link Handling
  152. // Check if mouse intersects with any links.
  153. int linkIndex = TMP_TextUtilities.FindIntersectingLink(m_TextComponent, Input.mousePosition, m_Camera);
  154. // Handle new Link selection.
  155. if (linkIndex != -1 && linkIndex != m_selectedLink)
  156. {
  157. m_selectedLink = linkIndex;
  158. // Get information about the link.
  159. TMP_LinkInfo linkInfo = m_TextComponent.textInfo.linkInfo[linkIndex];
  160. // Send the event to any listeners.
  161. SendOnLinkSelection(linkInfo.GetLinkID(), linkInfo.GetLinkText(), linkIndex);
  162. }
  163. #endregion
  164. }
  165. else
  166. {
  167. // Reset all selections given we are hovering outside the text container bounds.
  168. m_selectedLink = -1;
  169. m_lastCharIndex = -1;
  170. m_lastWordIndex = -1;
  171. m_lastLineIndex = -1;
  172. }
  173. }
  174. public void OnPointerEnter(PointerEventData eventData)
  175. {
  176. //Debug.Log("OnPointerEnter()");
  177. }
  178. public void OnPointerExit(PointerEventData eventData)
  179. {
  180. //Debug.Log("OnPointerExit()");
  181. }
  182. private void SendOnCharacterSelection(char character, int characterIndex)
  183. {
  184. if (onCharacterSelection != null)
  185. onCharacterSelection.Invoke(character, characterIndex);
  186. }
  187. private void SendOnSpriteSelection(char character, int characterIndex)
  188. {
  189. if (onSpriteSelection != null)
  190. onSpriteSelection.Invoke(character, characterIndex);
  191. }
  192. private void SendOnWordSelection(string word, int charIndex, int length)
  193. {
  194. if (onWordSelection != null)
  195. onWordSelection.Invoke(word, charIndex, length);
  196. }
  197. private void SendOnLineSelection(string line, int charIndex, int length)
  198. {
  199. if (onLineSelection != null)
  200. onLineSelection.Invoke(line, charIndex, length);
  201. }
  202. private void SendOnLinkSelection(string linkID, string linkText, int linkIndex)
  203. {
  204. if (onLinkSelection != null)
  205. onLinkSelection.Invoke(linkID, linkText, linkIndex);
  206. }
  207. }
  208. }