How do you access a Rich text link tag in a Label element?

So UI toolkit supports Rich text now and you can format text using tags.

But how would you implement something like this which works for a standard Text Mesh Pro component
to find out when the mouse is over a link tag?
I can register an event to get when the mouse is over the whole label but I want a particular word.

using UnityEngine;
using TMPro;
using UnityEngine.EventSystems;


[RequireComponent(typeof(TMP_Text))]
public class OpenHyperlink : MonoBehaviour, IPointerClickHandler
{
    private TMP_Text m_textMeshPro;
    void Start()
    {
        m_textMeshPro = GetComponent<TMP_Text>();
    }
    public void OnPointerClick(PointerEventData eventData)
    {

        int linkIndex = TMP_TextUtilities.FindIntersectingLink(m_textMeshPro, Input.mousePosition, null);
        if (linkIndex != -1)
        {
            TMP_LinkInfo linkInfo = m_textMeshPro.textInfo.linkInfo[linkIndex];
            Application.OpenURL(linkInfo.GetLinkID());
        }
    }
}

Hi @monark !

This thread covers what’s currently supported for interactions with fragments of text.

1 Like