Search Unity

TextMesh Pro Possible to get size and position of a link?

Discussion in 'UGUI & TextMesh Pro' started by flashvt, Jan 5, 2019.

  1. flashvt

    flashvt

    Joined:
    Oct 5, 2017
    Posts:
    2
    I currently have a help icon that appears next to some titles in my project The link is a sprite at the end of a string of text, for example: The Title of the Section<link=helplink_1><sprite=1></link>

    and it looks like Image 1.
    Comments_1.png
    When you click on the sprite, a help box pops up. It currently uses pointerEventData.position to position the help box, but I would love to have it based on the center of the sprite. (Image 2)
    Comments_2.png
    Any ideas? Thanks.
     
    Last edited: Jan 5, 2019
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    You should be able to get the position of the sprite / character. Then using the characterInfo[index] of that character / sprite use the bottom left and top right position to determine the appropriate position.

    Also take a look at Example 12 and 12a included with the TMP Examples and Extras.
     
    IgorAherneBusiness likes this.
  3. flashvt

    flashvt

    Joined:
    Oct 5, 2017
    Posts:
    2
    Thanks. Got it to work from those examples:
    Code (CSharp):
    1.  
    2. Vector2 CalcLinkCenterPosition()
    3.     {
    4.         Transform m_Transform = gameObject.GetComponent<Transform>();
    5.  
    6.         Vector3 bottomLeft = Vector3.zero;
    7.         Vector3 topRight = Vector3.zero;
    8.  
    9.         float maxAscender = -Mathf.Infinity;
    10.         float minDescender = Mathf.Infinity;
    11.  
    12.         TMP_TextInfo textInfo = text.textInfo;
    13.         TMP_LinkInfo linkInfo = textInfo.linkInfo[0];
    14.         TMP_CharacterInfo currentCharInfo = textInfo.characterInfo[linkInfo.linkTextfirstCharacterIndex];
    15.  
    16.         maxAscender = Mathf.Max(maxAscender, currentCharInfo.ascender);
    17.         minDescender = Mathf.Min(minDescender, currentCharInfo.descender);
    18.  
    19.         bottomLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0);
    20.  
    21.         bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
    22.         topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
    23.  
    24.         float width = topRight.x - bottomLeft.x;
    25.         float height = topRight.y - bottomLeft.y;
    26.  
    27.         Vector2 centerPosition = bottomLeft;
    28.         centerPosition.x += width / 2;
    29.         centerPosition.y += height / 2;
    30.  
    31.         return centerPosition;
    32.     }