Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to detect if mouse over TMP_Text sprites.

Discussion in 'UGUI & TextMesh Pro' started by Xahutek, Jan 28, 2023.

  1. Xahutek

    Xahutek

    Joined:
    Feb 27, 2020
    Posts:
    3
    Hey there,
    I'm trying to make a Tooltip system for a game. I already made a component I can attach together with TMP_Text that enables me to show tooltips when hovering words like this:
    Code (CSharp):
    1. //On a script attached to the TMP_Text object.
    2. public void OnPointerMove(PointerEventData eventData)
    3.     {
    4.         int wordIndex = TMP_TextUtilities.FindIntersectingWord(text, Input.mousePosition, Camera.main);
    5.      
    6.         if (wordIndex != -1)
    7.         {
    8.             TMP_WordInfo wordInfo = text.textInfo.wordInfo[wordIndex];
    9.  
    10.             word = wordInfo.GetWord();
    11.  
    12.             //the word is used by my Tooltip system
    13.         }
    14.         else word = "";
    I would like to do the same thing for hovering over icons that are displayed as sprites in the text.
    I could not find a TMP_TextUtilities.FindIntersectingSpriteIndex(...) method or something similar that could give me any info on what if any sprite I am hovering over.

    How would I go about implementing this?
    I've been trying to get behind this for a few hours now but no answer I found seemed to lead anyhwere for me.