Search Unity

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

Discussion in 'UI Toolkit' started by monark, Sep 7, 2022.

  1. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3. using UnityEngine.EventSystems;
    4.  
    5.  
    6. [RequireComponent(typeof(TMP_Text))]
    7. public class OpenHyperlink : MonoBehaviour, IPointerClickHandler
    8. {
    9.     private TMP_Text m_textMeshPro;
    10.     void Start()
    11.     {
    12.         m_textMeshPro = GetComponent<TMP_Text>();
    13.     }
    14.     public void OnPointerClick(PointerEventData eventData)
    15.     {
    16.  
    17.         int linkIndex = TMP_TextUtilities.FindIntersectingLink(m_textMeshPro, Input.mousePosition, null);
    18.         if (linkIndex != -1)
    19.         {
    20.             TMP_LinkInfo linkInfo = m_textMeshPro.textInfo.linkInfo[linkIndex];
    21.             Application.OpenURL(linkInfo.GetLinkID());
    22.         }
    23.     }
    24. }
     
  2. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @monark!

    This thread covers what's currently supported for interactions with fragments of text.
     
    monark likes this.