void ResizeWords(TMP_WordInfo wInfo) { for (int charIndex = wInfo.firstCharacterIndex; charIndex < wInfo.firstCharacterIndex + wInfo.characterCount; charIndex++) { // Get the index of the material / sub text object used by this character. int materialIndex = m_TextMeshPro.textInfo.characterInfo[charIndex].materialReferenceIndex; // Get the index of the first vertex of the selected character. int vertexIndex = m_TextMeshPro.textInfo.characterInfo[charIndex].vertexIndex; // Get a reference to the vertices array. Vector3[] vertices = m_TextMeshPro.textInfo.meshInfo[materialIndex].vertices; // Determine the center point of the character. Vector2 charMidBasline = (vertices[vertexIndex + 0] + vertices[vertexIndex + 2]) / 2; // Need to translate all 4 vertices of the character to aligned with middle of character / baseline. // This is needed so the matrix TRS is applied at the origin for each character. Vector3 offset = charMidBasline; // Translate the character to the middle baseline. vertices[vertexIndex + 0] = vertices[vertexIndex + 0] - offset; vertices[vertexIndex + 1] = vertices[vertexIndex + 1] - offset; vertices[vertexIndex + 2] = vertices[vertexIndex + 2] - offset; vertices[vertexIndex + 3] = vertices[vertexIndex + 3] - offset; float zoomFactor = 1.5f; // Setup the Matrix for the scale change. m_matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one * zoomFactor); // Apply Matrix operation on the given character. vertices[vertexIndex + 0] = m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 0]); vertices[vertexIndex + 1] = m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 1]); vertices[vertexIndex + 2] = m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 2]); vertices[vertexIndex + 3] = m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 3]); // Translate the character back to its original position. vertices[vertexIndex + 0] = vertices[vertexIndex + 0] + offset; vertices[vertexIndex + 1] = vertices[vertexIndex + 1] + offset; vertices[vertexIndex + 2] = vertices[vertexIndex + 2] + offset; vertices[vertexIndex + 3] = vertices[vertexIndex + 3] + offset; // Get a reference to the meshInfo of the selected character. TMP_MeshInfo meshInfo = m_TextMeshPro.textInfo.meshInfo[materialIndex]; // Get the index of the last character's vertex attributes. int lastVertexIndex = vertices.Length - 4; // Swap the current character's vertex attributes with those of the last element in the vertex attribute arrays. // We do this to make sure this character is rendered last and over other characters. meshInfo.SwapVertexData(vertexIndex, lastVertexIndex); } // Need to update the appropriate m_TextMeshPro.UpdateVertexData(TMP_VertexDataUpdateFlags.All); }
private void LateUpdate() { if (this.isHoveringObject) { int num = TMP_TextUtilities.FindIntersectingCharacter(this.m_TextMeshPro, Input.mousePosition, this.m_Camera, true); if (num == -1 || num != this.m_lastIndex) { this.RestoreCachedVertexAttributes(this.m_lastIndex); this.m_lastIndex = -1; } if (num != -1 && num != this.m_lastIndex && (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))) { this.m_lastIndex = num; int materialReferenceIndex = this.m_TextMeshPro.textInfo.characterInfo[num].materialReferenceIndex; int vertexIndex = this.m_TextMeshPro.textInfo.characterInfo[num].vertexIndex; Vector3[] vertices = this.m_TextMeshPro.textInfo.meshInfo[materialReferenceIndex].vertices; Vector2 v = (vertices[vertexIndex] + vertices[vertexIndex + 2]) / 2f; Vector3 b = v; vertices[vertexIndex] -= b; vertices[vertexIndex + 1] = vertices[vertexIndex + 1] - b; vertices[vertexIndex + 2] = vertices[vertexIndex + 2] - b; vertices[vertexIndex + 3] = vertices[vertexIndex + 3] - b; float d = 1.5f; this.m_matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one * d); vertices[vertexIndex] = this.m_matrix.MultiplyPoint3x4(vertices[vertexIndex]); vertices[vertexIndex + 1] = this.m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 1]); vertices[vertexIndex + 2] = this.m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 2]); vertices[vertexIndex + 3] = this.m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 3]); vertices[vertexIndex] += b; vertices[vertexIndex + 1] = vertices[vertexIndex + 1] + b; vertices[vertexIndex + 2] = vertices[vertexIndex + 2] + b; vertices[vertexIndex + 3] = vertices[vertexIndex + 3] + b; Color32 color = new Color32(byte.MaxValue, byte.MaxValue, 192, byte.MaxValue); Color32[] colors = this.m_TextMeshPro.textInfo.meshInfo[materialReferenceIndex].colors32; colors[vertexIndex] = color; colors[vertexIndex + 1] = color; colors[vertexIndex + 2] = color; colors[vertexIndex + 3] = color; TMP_MeshInfo tmp_MeshInfo = this.m_TextMeshPro.textInfo.meshInfo[materialReferenceIndex]; int dst = vertices.Length - 4; tmp_MeshInfo.SwapVertexData(vertexIndex, dst); this.m_TextMeshPro.UpdateVertexData(TMP_VertexDataUpdateFlags.All); } int num2 = TMP_TextUtilities.FindIntersectingWord(this.m_TextMeshPro, Input.mousePosition, this.m_Camera); if (this.m_TextPopup_RectTransform != null && this.m_selectedWord != -1 && (num2 == -1 || num2 != this.m_selectedWord)) { TMP_WordInfo tmp_WordInfo = this.m_TextMeshPro.textInfo.wordInfo[this.m_selectedWord]; for (int i = 0; i < tmp_WordInfo.characterCount; i++) { int num3 = tmp_WordInfo.firstCharacterIndex + i; int materialReferenceIndex2 = this.m_TextMeshPro.textInfo.characterInfo[num3].materialReferenceIndex; int vertexIndex2 = this.m_TextMeshPro.textInfo.characterInfo[num3].vertexIndex; Color32[] colors2 = this.m_TextMeshPro.textInfo.meshInfo[materialReferenceIndex2].colors32; Color32 color2 = colors2[vertexIndex2].Tint(1.33333f); colors2[vertexIndex2] = color2; colors2[vertexIndex2 + 1] = color2; colors2[vertexIndex2 + 2] = color2; colors2[vertexIndex2 + 3] = color2; } this.m_TextMeshPro.UpdateVertexData(TMP_VertexDataUpdateFlags.All); this.m_selectedWord = -1; } if (num2 != -1 && num2 != this.m_selectedWord && !Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.RightShift)) { this.m_selectedWord = num2; TMP_WordInfo tmp_WordInfo2 = this.m_TextMeshPro.textInfo.wordInfo[num2]; for (int j = 0; j < tmp_WordInfo2.characterCount; j++) { int num4 = tmp_WordInfo2.firstCharacterIndex + j; int materialReferenceIndex3 = this.m_TextMeshPro.textInfo.characterInfo[num4].materialReferenceIndex; int vertexIndex3 = this.m_TextMeshPro.textInfo.characterInfo[num4].vertexIndex; Color32[] colors3 = this.m_TextMeshPro.textInfo.meshInfo[materialReferenceIndex3].colors32; Color32 color3 = colors3[vertexIndex3].Tint(0.75f); colors3[vertexIndex3] = color3; colors3[vertexIndex3 + 1] = color3; colors3[vertexIndex3 + 2] = color3; colors3[vertexIndex3 + 3] = color3; } this.m_TextMeshPro.UpdateVertexData(TMP_VertexDataUpdateFlags.All); } int num5 = TMP_TextUtilities.FindIntersectingLink(this.m_TextMeshPro, Input.mousePosition, this.m_Camera); if ((num5 == -1 && this.m_selectedLink != -1) || num5 != this.m_selectedLink) { this.m_TextPopup_RectTransform.gameObject.SetActive(false); this.m_selectedLink = -1; } if (num5 != -1 && num5 != this.m_selectedLink) { this.m_selectedLink = num5; TMP_LinkInfo tmp_LinkInfo = this.m_TextMeshPro.textInfo.linkInfo[num5]; Vector3 zero = Vector3.zero; RectTransformUtility.ScreenPointToWorldPointInRectangle(this.m_TextMeshPro.rectTransform, Input.mousePosition, this.m_Camera, out zero); string linkID = tmp_LinkInfo.GetLinkID(); if (linkID != null) { if (!(linkID == "id_01")) { if (linkID == "id_02") { this.m_TextPopup_RectTransform.position = zero; this.m_TextPopup_RectTransform.gameObject.SetActive(true); this.m_TextPopup_TMPComponent.text = "You have selected link <#ffff00> ID 02"; } } else { this.m_TextPopup_RectTransform.position = zero; this.m_TextPopup_RectTransform.gameObject.SetActive(true); this.m_TextPopup_TMPComponent.text = "You have selected link <#ffff00> ID 01"; } } } } else if (this.m_lastIndex != -1) { this.RestoreCachedVertexAttributes(this.m_lastIndex); this.m_lastIndex = -1; } }
void LateUpdate() { if (isHoveringObject) { // Check if Mouse Intersects any of the characters. If so, assign a random color. #region Handle Character Selection int charIndex = TMP_TextUtilities.FindIntersectingCharacter(m_TextMeshPro, Input.mousePosition, m_Camera, true); // Undo Swap and Vertex Attribute changes. if (charIndex == -1 || charIndex != m_lastIndex) { RestoreCachedVertexAttributes(m_lastIndex); m_lastIndex = -1; } if (charIndex != -1 && charIndex != m_lastIndex && (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))) { m_lastIndex = charIndex; // Get the index of the material / sub text object used by this character. int materialIndex = m_TextMeshPro.textInfo.characterInfo[charIndex].materialReferenceIndex; // Get the index of the first vertex of the selected character. int vertexIndex = m_TextMeshPro.textInfo.characterInfo[charIndex].vertexIndex; // Get a reference to the vertices array. Vector3[] vertices = m_TextMeshPro.textInfo.meshInfo[materialIndex].vertices; // Determine the center point of the character. Vector2 charMidBasline = (vertices[vertexIndex + 0] + vertices[vertexIndex + 2]) / 2; // Need to translate all 4 vertices of the character to aligned with middle of character / baseline. // This is needed so the matrix TRS is applied at the origin for each character. Vector3 offset = charMidBasline; // Translate the character to the middle baseline. vertices[vertexIndex + 0] = vertices[vertexIndex + 0] - offset; vertices[vertexIndex + 1] = vertices[vertexIndex + 1] - offset; vertices[vertexIndex + 2] = vertices[vertexIndex + 2] - offset; vertices[vertexIndex + 3] = vertices[vertexIndex + 3] - offset; float zoomFactor = 1.5f; // Setup the Matrix for the scale change. m_matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one * zoomFactor); // Apply Matrix operation on the given character. vertices[vertexIndex + 0] = m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 0]); vertices[vertexIndex + 1] = m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 1]); vertices[vertexIndex + 2] = m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 2]); vertices[vertexIndex + 3] = m_matrix.MultiplyPoint3x4(vertices[vertexIndex + 3]); // Translate the character back to its original position. vertices[vertexIndex + 0] = vertices[vertexIndex + 0] + offset; vertices[vertexIndex + 1] = vertices[vertexIndex + 1] + offset; vertices[vertexIndex + 2] = vertices[vertexIndex + 2] + offset; vertices[vertexIndex + 3] = vertices[vertexIndex + 3] + offset; // Change Vertex Colors of the highlighted character Color32 c = new Color32(255, 255, 192, 255); // Get a reference to the vertex color Color32[] vertexColors = m_TextMeshPro.textInfo.meshInfo[materialIndex].colors32; vertexColors[vertexIndex + 0] = c; vertexColors[vertexIndex + 1] = c; vertexColors[vertexIndex + 2] = c; vertexColors[vertexIndex + 3] = c; // Get a reference to the meshInfo of the selected character. TMP_MeshInfo meshInfo = m_TextMeshPro.textInfo.meshInfo[materialIndex]; // Get the index of the last character's vertex attributes. int lastVertexIndex = vertices.Length - 4; // Swap the current character's vertex attributes with those of the last element in the vertex attribute arrays. // We do this to make sure this character is rendered last and over other characters. meshInfo.SwapVertexData(vertexIndex, lastVertexIndex); // Need to update the appropriate m_TextMeshPro.UpdateVertexData(TMP_VertexDataUpdateFlags.All); } #endregion #region Word Selection Handling //Check if Mouse intersects any words and if so assign a random color to that word. int wordIndex = TMP_TextUtilities.FindIntersectingWord(m_TextMeshPro, Input.mousePosition, m_Camera); // Clear previous word selection. if (m_TextPopup_RectTransform != null && m_selectedWord != -1 && (wordIndex == -1 || wordIndex != m_selectedWord)) { TMP_WordInfo wInfo = m_TextMeshPro.textInfo.wordInfo[m_selectedWord]; // Iterate through each of the characters of the word. for (int i = 0; i < wInfo.characterCount; i++) { int characterIndex = wInfo.firstCharacterIndex + i; // Get the index of the material / sub text object used by this character. int meshIndex = m_TextMeshPro.textInfo.characterInfo[characterIndex].materialReferenceIndex; // Get the index of the first vertex of this character. int vertexIndex = m_TextMeshPro.textInfo.characterInfo[characterIndex].vertexIndex; // Get a reference to the vertex color Color32[] vertexColors = m_TextMeshPro.textInfo.meshInfo[meshIndex].colors32; Color32 c = vertexColors[vertexIndex + 0].Tint(1.33333f); vertexColors[vertexIndex + 0] = c; vertexColors[vertexIndex + 1] = c; vertexColors[vertexIndex + 2] = c; vertexColors[vertexIndex + 3] = c; } // Update Geometry m_TextMeshPro.UpdateVertexData(TMP_VertexDataUpdateFlags.All); m_selectedWord = -1; } // Word Selection Handling if (wordIndex != -1 && wordIndex != m_selectedWord && !(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))) { m_selectedWord = wordIndex; TMP_WordInfo wInfo = m_TextMeshPro.textInfo.wordInfo[wordIndex]; // Iterate through each of the characters of the word. for (int i = 0; i < wInfo.characterCount; i++) { int characterIndex = wInfo.firstCharacterIndex + i; // Get the index of the material / sub text object used by this character. int meshIndex = m_TextMeshPro.textInfo.characterInfo[characterIndex].materialReferenceIndex; int vertexIndex = m_TextMeshPro.textInfo.characterInfo[characterIndex].vertexIndex; // Get a reference to the vertex color Color32[] vertexColors = m_TextMeshPro.textInfo.meshInfo[meshIndex].colors32; Color32 c = vertexColors[vertexIndex + 0].Tint(0.75f); vertexColors[vertexIndex + 0] = c; vertexColors[vertexIndex + 1] = c; vertexColors[vertexIndex + 2] = c; vertexColors[vertexIndex + 3] = c; } // Update Geometry m_TextMeshPro.UpdateVertexData(TMP_VertexDataUpdateFlags.All); } #endregion #region Example of Link Handling // Check if mouse intersects with any links. int linkIndex = TMP_TextUtilities.FindIntersectingLink(m_TextMeshPro, Input.mousePosition, m_Camera); // Clear previous link selection if one existed. if ((linkIndex == -1 && m_selectedLink != -1) || linkIndex != m_selectedLink) { m_TextPopup_RectTransform.gameObject.SetActive(false); m_selectedLink = -1; } // Handle new Link selection. if (linkIndex != -1 && linkIndex != m_selectedLink) { m_selectedLink = linkIndex; TMP_LinkInfo linkInfo = m_TextMeshPro.textInfo.linkInfo[linkIndex]; // Debug.Log("Link ID: \"" + linkInfo.GetLinkID() + "\" Link Text: \"" + linkInfo.GetLinkText() + "\""); // Example of how to retrieve the Link ID and Link Text. Vector3 worldPointInRectangle; RectTransformUtility.ScreenPointToWorldPointInRectangle(m_TextMeshPro.rectTransform, Input.mousePosition, m_Camera, out worldPointInRectangle); switch (linkInfo.GetLinkID()) { case "id_01": // 100041637: // id_01 m_TextPopup_RectTransform.position = worldPointInRectangle; m_TextPopup_RectTransform.gameObject.SetActive(true); m_TextPopup_TMPComponent.text = k_LinkText + " ID 01"; break; case "id_02": // 100041638: // id_02 m_TextPopup_RectTransform.position = worldPointInRectangle; m_TextPopup_RectTransform.gameObject.SetActive(true); m_TextPopup_TMPComponent.text = k_LinkText + " ID 02"; break; } } #endregion } else { // Restore any character that may have been modified if (m_lastIndex != -1) { RestoreCachedVertexAttributes(m_lastIndex); m_lastIndex = -1; } } }