TMP_TextSelector_A.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using System.Collections;
  4. namespace TMPro.Examples
  5. {
  6. public class TMP_TextSelector_A : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  7. {
  8. private TextMeshPro m_TextMeshPro;
  9. private Camera m_Camera;
  10. private bool m_isHoveringObject;
  11. private int m_selectedLink = -1;
  12. private int m_lastCharIndex = -1;
  13. private int m_lastWordIndex = -1;
  14. void Awake()
  15. {
  16. m_TextMeshPro = gameObject.GetComponent<TextMeshPro>();
  17. m_Camera = Camera.main;
  18. // Force generation of the text object so we have valid data to work with. This is needed since LateUpdate() will be called before the text object has a chance to generated when entering play mode.
  19. m_TextMeshPro.ForceMeshUpdate();
  20. }
  21. void LateUpdate()
  22. {
  23. m_isHoveringObject = false;
  24. if (TMP_TextUtilities.IsIntersectingRectTransform(m_TextMeshPro.rectTransform, Input.mousePosition, Camera.main))
  25. {
  26. m_isHoveringObject = true;
  27. }
  28. if (m_isHoveringObject)
  29. {
  30. #region Example of Character Selection
  31. int charIndex = TMP_TextUtilities.FindIntersectingCharacter(m_TextMeshPro, Input.mousePosition, Camera.main, true);
  32. if (charIndex != -1 && charIndex != m_lastCharIndex && (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)))
  33. {
  34. //Debug.Log("[" + m_TextMeshPro.textInfo.characterInfo[charIndex].character + "] has been selected.");
  35. m_lastCharIndex = charIndex;
  36. int meshIndex = m_TextMeshPro.textInfo.characterInfo[charIndex].materialReferenceIndex;
  37. int vertexIndex = m_TextMeshPro.textInfo.characterInfo[charIndex].vertexIndex;
  38. Color32 c = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
  39. Color32[] vertexColors = m_TextMeshPro.textInfo.meshInfo[meshIndex].colors32;
  40. vertexColors[vertexIndex + 0] = c;
  41. vertexColors[vertexIndex + 1] = c;
  42. vertexColors[vertexIndex + 2] = c;
  43. vertexColors[vertexIndex + 3] = c;
  44. //m_TextMeshPro.mesh.colors32 = vertexColors;
  45. m_TextMeshPro.textInfo.meshInfo[meshIndex].mesh.colors32 = vertexColors;
  46. }
  47. #endregion
  48. #region Example of Link Handling
  49. // Check if mouse intersects with any links.
  50. int linkIndex = TMP_TextUtilities.FindIntersectingLink(m_TextMeshPro, Input.mousePosition, m_Camera);
  51. // Clear previous link selection if one existed.
  52. if ((linkIndex == -1 && m_selectedLink != -1) || linkIndex != m_selectedLink)
  53. {
  54. //m_TextPopup_RectTransform.gameObject.SetActive(false);
  55. m_selectedLink = -1;
  56. }
  57. // Handle new Link selection.
  58. if (linkIndex != -1 && linkIndex != m_selectedLink)
  59. {
  60. m_selectedLink = linkIndex;
  61. TMP_LinkInfo linkInfo = m_TextMeshPro.textInfo.linkInfo[linkIndex];
  62. // The following provides an example of how to access the link properties.
  63. //Debug.Log("Link ID: \"" + linkInfo.GetLinkID() + "\" Link Text: \"" + linkInfo.GetLinkText() + "\""); // Example of how to retrieve the Link ID and Link Text.
  64. Vector3 worldPointInRectangle;
  65. RectTransformUtility.ScreenPointToWorldPointInRectangle(m_TextMeshPro.rectTransform, Input.mousePosition, m_Camera, out worldPointInRectangle);
  66. switch (linkInfo.GetLinkID())
  67. {
  68. case "id_01": // 100041637: // id_01
  69. //m_TextPopup_RectTransform.position = worldPointInRectangle;
  70. //m_TextPopup_RectTransform.gameObject.SetActive(true);
  71. //m_TextPopup_TMPComponent.text = k_LinkText + " ID 01";
  72. break;
  73. case "id_02": // 100041638: // id_02
  74. //m_TextPopup_RectTransform.position = worldPointInRectangle;
  75. //m_TextPopup_RectTransform.gameObject.SetActive(true);
  76. //m_TextPopup_TMPComponent.text = k_LinkText + " ID 02";
  77. break;
  78. }
  79. }
  80. #endregion
  81. #region Example of Word Selection
  82. // Check if Mouse intersects any words and if so assign a random color to that word.
  83. int wordIndex = TMP_TextUtilities.FindIntersectingWord(m_TextMeshPro, Input.mousePosition, Camera.main);
  84. if (wordIndex != -1 && wordIndex != m_lastWordIndex)
  85. {
  86. m_lastWordIndex = wordIndex;
  87. TMP_WordInfo wInfo = m_TextMeshPro.textInfo.wordInfo[wordIndex];
  88. Vector3 wordPOS = m_TextMeshPro.transform.TransformPoint(m_TextMeshPro.textInfo.characterInfo[wInfo.firstCharacterIndex].bottomLeft);
  89. wordPOS = Camera.main.WorldToScreenPoint(wordPOS);
  90. //Debug.Log("Mouse Position: " + Input.mousePosition.ToString("f3") + " Word Position: " + wordPOS.ToString("f3"));
  91. Color32[] vertexColors = m_TextMeshPro.textInfo.meshInfo[0].colors32;
  92. Color32 c = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
  93. for (int i = 0; i < wInfo.characterCount; i++)
  94. {
  95. int vertexIndex = m_TextMeshPro.textInfo.characterInfo[wInfo.firstCharacterIndex + i].vertexIndex;
  96. vertexColors[vertexIndex + 0] = c;
  97. vertexColors[vertexIndex + 1] = c;
  98. vertexColors[vertexIndex + 2] = c;
  99. vertexColors[vertexIndex + 3] = c;
  100. }
  101. m_TextMeshPro.mesh.colors32 = vertexColors;
  102. }
  103. #endregion
  104. }
  105. }
  106. public void OnPointerEnter(PointerEventData eventData)
  107. {
  108. Debug.Log("OnPointerEnter()");
  109. m_isHoveringObject = true;
  110. }
  111. public void OnPointerExit(PointerEventData eventData)
  112. {
  113. Debug.Log("OnPointerExit()");
  114. m_isHoveringObject = false;
  115. }
  116. }
  117. }