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>
and it looks like Image 1.
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)
Any ideas? Thanks.
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.
1 Like
Thanks. Got it to work from those examples:
Vector2 CalcLinkCenterPosition()
{
Transform m_Transform = gameObject.GetComponent<Transform>();
Vector3 bottomLeft = Vector3.zero;
Vector3 topRight = Vector3.zero;
float maxAscender = -Mathf.Infinity;
float minDescender = Mathf.Infinity;
TMP_TextInfo textInfo = text.textInfo;
TMP_LinkInfo linkInfo = textInfo.linkInfo[0];
TMP_CharacterInfo currentCharInfo = textInfo.characterInfo[linkInfo.linkTextfirstCharacterIndex];
maxAscender = Mathf.Max(maxAscender, currentCharInfo.ascender);
minDescender = Mathf.Min(minDescender, currentCharInfo.descender);
bottomLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0);
bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
float width = topRight.x - bottomLeft.x;
float height = topRight.y - bottomLeft.y;
Vector2 centerPosition = bottomLeft;
centerPosition.x += width / 2;
centerPosition.y += height / 2;
return centerPosition;
}
4 Likes