Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

TextMesh Pro Clickable link within a text

Discussion in 'UGUI & TextMesh Pro' started by Necronomicron, Jun 11, 2020.

  1. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    106
    Hello!
    I'm trying to make something like this:
    The docs explain literally nothing.
    This just doesn't work:
    Click <link="http://example.com/">this</link>to be happy.

    And I have no clue what are those IDs and where I have to put them.
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,081
    Take a look at example "12 - Link Example" and "12a - Text Interactions" included in the TMP Examples & Extras and the scripts these examples use.

    The link tag format is <link="ID">visible text</link>.

    This ID should be unique to allow your code to implement whatever logic is desired when interacting with this link which in end simply define a region of text.

    Since the desired interaction can be anything. Ie. maybe you want something to happen on mouse over or maybe on click and the result of this interaction is to play a sound or open a window or change the color of the text or whatever else, the <link="ID">link description</link> is generic as to allow you to implement any type of logic.

    Again the link simply allows you to define a section of text and to tag it with a unique ID.
     
    Necronomicron likes this.
  3. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    106
    Does something have to happen when I click on the link in 12a? Because nothing happens except some messages in the console.
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,081
    In 12a, take a look at the console output as you hover over characters, words, lines, sprites, etc.

    Note: A message is output only when hover over new / different elements.
     
  5. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    106
    Too much extra work for such a simple task if you ask me. Besides nothing works for me.
    OnPointerClick()
    in
    TMP_TextSelector_B
    seems to be deprecated.
    charIndex
    is always -1. Same for
    linkIndex
    .
     
    Last edited: Jun 11, 2020
  6. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,081
    Make sure you are using the latest release of the TMP package which is Preview 14.

    As per the following summary comment
    <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null if using ScreenSpace Overlay.</param>

    In terms of too much extra work for such a simple task. I agree that it is a complex task which on the surface would appear like a simple task but you have to consider all the potential interaction a user might want.
    - What should we do when someone clicks a link or hover? Play a sound? Instantiate an object? Open a bowser? Does the platform you target support a bowser? Do we open a window? How big... There is no way to know what your expected behavior and requirements are when interacting with a link.

    As such, the <link=ID> simply allows you to define a region of text for which you can implement any potential handling based on your needs.

    Example 12a does show a simpler way to implement the behaviors you might seek using Events.
     
    Necronomicron likes this.
  7. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    106
    I've already figured out it was because camera parameter isn't null, but where is it from? I don't have this description in IDE.
     
  8. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,081
    The IDE / Intellisense should bring that up for the Camera parameter of the function.

    P.S. I certainly acknowledge the documentation on TMP is horrible and we have folks working it.

    In the mean time, always do a quick search on the forum as there have been many posts about most topics. If your search fails to produce the result you seek, always feel free to post on the forum where I'll be more than happy to provide assistance if other experience TMP users haven't already done so.
     
  9. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    106
    Well, it didn't. At least for
    FindIntersectingLink()
    . I have the latest TMPro and I use Visual Studio.
     
    Last edited: Jun 11, 2020
  10. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,130
    I don't understand how this is supposed to work. I just want to know when the user clicks certain words in a TMP canvas text field so I can pop up an image.

    In Example 12a, there are two TMP fields, one uses a mesh renderer, and the other uses a canvas renderer. Only the mesh renderer TMP text shows the console output, the canvas renderer TMP field does nothing, as far as I can tell. I don't see why one works and one doesn't, and I'm not sure how the one that works outputs anything to the console. The TMP field in the example has a script attached, TMP_TextEventHandler, but there are no scripts set for any of the selection events. The output comes from another script, TMP_TextEventCheck. Are both these scripts necessary? I've also noticed that these are NOT selection event as the output says, just rollovers. Nothing different seems to happen when a link is clicked.

    How do I detect a click on a link in a TMP canvas text field so that I can call a function when it's clicked? Is there any sample code that works and show how this is done?
     
  11. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,081
    The Event Processor object in that scene has a reference to the text component that uses the mesh renderer. If you replace that reference by other text component (the one using the Canvas Renderer) then it will work and output in the console.

    The TMP_TextEventHandler.cs contains the logic related to what happens when the mouse intersects with the RectTransform of the text object and then with characters, sprites, words, lines, and links. This logic is contained in the LateUpdate function.

    If you look specifically at the "Example of Character or Sprite Selection", you will see the it uses the
    TMP_TextUtilities.FindIntersectingCharacter(). There are similar functions for words, etc.

    In this example, it is only concerned with the mouse intersecting with a character so you would need to add the part about mouse intersecting + clicking as an additional check.

    You would mirror the implementation of "Example of Link Handling" in LateUpdate() but add the additional check for clicking in addition to existing mouse position check.

    You could revise the conditional check as follows:
    Code (csharp):
    1.  
    2. // Handle new Link selection.
    3. if (linkIndex != -1 && Input.GetKeyDown(KeyCode.Mouse0))
    4. {
    5.     m_selectedLink = linkIndex;
    6.  
    7.     // Get information about the link.
    8.     TMP_LinkInfo linkInfo = m_TextComponent.textInfo.linkInfo[linkIndex];
    9.  
    10.     // Send the event to any listeners.
    11.     SendOnLinkSelection(linkInfo.GetLinkID(), linkInfo.GetLinkText(), linkIndex);
    12. }
    13.  
     
  12. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,130
    I attached two scripts to my TMP text field: TMP_TextEventCheck and TMP_TextEventHandler, and set the correct reference. Now, when I move the mouse over the field, a few words, lines, and characters generate console output, apparently at random, but only a few, and it seems to depend on how the field is scrolled. Whatever text happens to be about a 1/3 of the way down the field at the time, as I scroll, different lines/words get flagged. And the ONE LINK I have in the text does nothing, no output when the mouse goes over the link.

    I could save a lot of my time by putting a transparent button over the link, as I've done in the past, and call it a day. But I'd really like to do it the right way and get this to work, and save the hassle of moving the button every time the text changes.
     
  13. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,081
    In order to avoid spamming the console when the mouse position is over a character, word, line, link, etc. A check is made to only handle newer interactions.

    In this portion of code from the TMP_TextEventHandler.cs, you can see that it will only report new characters that it encounters. Removing the charIndex != m_lastCharIndex would result in constent events being raised while the mouse is over a character. Obviously, you can modify / fit this logic to your needs.

    Code (csharp):
    1.  
    2. // Example of Character or Sprite Selection
    3. int charIndex = TMP_TextUtilities.FindIntersectingCharacter(m_TextComponent, Input.mousePosition, m_Camera, true);
    4. if (charIndex != -1 && charIndex != m_lastCharIndex)
    5. {
    6.     m_lastCharIndex = charIndex;
    7.  
    8.     TMP_TextElementType elementType = m_TextComponent.textInfo.characterInfo[charIndex].elementType;
    9.  
    10.     // Send event to any event listeners depending on whether it is a character or sprite.
    11.     if (elementType == TMP_TextElementType.Character)
    12.         SendOnCharacterSelection(m_TextComponent.textInfo.characterInfo[charIndex].character, charIndex);
    13.     else if (elementType == TMP_TextElementType.Sprite)
    14.         SendOnSpriteSelection(m_TextComponent.textInfo.characterInfo[charIndex].character, charIndex);
    15. }
    16.  
    I would need a better understanding of how your have this setup. Is the text object in some scroll view or something?

    I would suggest first experimenting with a non scrolling text object to make sure everything works as expected. Then add the scrolling part to figure out why that might be an issue.

    If you can't figure it out, please consider providing me with some simple scene / project for me to look at which would enable me to (1) make sure everything is working as expected on the TMP side and then provider pointers as to why you are observing reported behavior.
     
unityunity