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

Hyperlinks in Unity.UI Text

Discussion in 'UGUI & TextMesh Pro' started by DanSuperGP, Feb 11, 2015.

  1. LilGames

    LilGames

    Joined:
    Mar 30, 2015
    Posts:
    565
    Thank you for this code! One correction however: It should be:
    Code (csharp):
    1. Application.OpenURL(inLinkID);
     
  2. Hobby-Game-Developer

    Hobby-Game-Developer

    Joined:
    Aug 27, 2017
    Posts:
    14
    Hey,

    i've created a method, to find any links in a dynamically created string inside a TextMeshProGUI Component. Not only https, also http, ftp, file, anything you search:

    Code (CSharp):
    1. using TMPro;
    2. using System.Linq;
    3. public string SearchLink(Notiz note)
    4.     {
    5.         string[] textSplitter = new string[0];
    6.         List<string> originalLinks = new();
    7.         List<string> links = new();
    8.         List<string> hyperlinkTypes = new() { "https://", "http://", "ftp://", "file://" };
    9.         string finalString = note.NOTE;
    10.         textSplitter = note.NOTE.Split(' ', '\n');
    11.         for (int i = 0; i < textSplitter.Length; i++)
    12.         {
    13.             int found = 0;
    14.             bool alreadyFound = false;
    15.             for (int ii = 0; ii < hyperlinkTypes.Count; ii++)
    16.             {
    17.                 found = 0;
    18.                 alreadyFound = false;
    19.                 for (int iii = 0; iii < hyperlinkTypes[ii].Length; iii++)
    20.                 {
    21.                     if (iii < textSplitter[i].Length)
    22.                     {
    23.                         if (hyperlinkTypes[ii][iii] == textSplitter[i][iii])
    24.                         {
    25.                             found++;
    26.                         }
    27.                         else
    28.                         {
    29.                             found = 0;
    30.                         }
    31.                     }
    32.                     if (found == hyperlinkTypes[ii].Length)
    33.                     {
    34.                         links.Add("<color=#0000ffff><link=" + '"' + textSplitter[i] + '"' + '>' + textSplitter[i] + "</link></color>");
    35.                         originalLinks.Add(textSplitter[i]);
    36.                         alreadyFound = true;
    37.                         break;
    38.                     }
    39.                     else if (found == 0)
    40.                     {
    41.                         break;
    42.                     }
    43.                 }
    44.                 if (alreadyFound)
    45.                 {
    46.                     break;
    47.                 }
    48.             }
    49.         }
    50.         for (int l = 0; l < links.Count; l++)
    51.         {
    52.             finalString = finalString.Replace(originalLinks[l], links[l]);
    53.         }
    54.         return finalString;
    55.     }
    56.  
    On a nother Noteobject with a script and the TextMeshProGUI Component, i use a codeline i found online. Most of you already know it. But it was only shown for static strings, not for any strings you enter:

    Code (CSharp):
    1.  
    2. using TMPro;
    3. using UnityEngine.EventSystems;
    4. public void OnPointerClick(PointerEventData eventData)
    5.     {
    6.         if(eventData.button == PointerEventData.InputButton.Left)
    7.         {
    8.             int index = TMP_TextUtilities.FindIntersectingLink(gameObject.GetComponent<TextMeshProUGUI>(),Input.mousePosition,null);
    9.             if(index > -1)
    10.             {
    11.                 Application.OpenURL(gameObject.GetComponent<TextMeshProUGUI>().textInfo.linkInfo[index].GetLinkID());
    12.             }
    13.         }
    14.     }
    If it doesn't work for you, it's probably because of missing namespaces or weird links or missing spaces!
    It works like on whatsapp for example. You have to have one space or new line between the link. Otherwise it will not detect the link.

    If nothing works (i encountered some problems), try to use ".com" or "/" at the end of the link. And if you try out your link system, try to use simple links, like https://google.com . If that doesn't work, you can be sure, that something's wrong.

    Modify it as you please and have fun with it.

    Check out my facebook-page:

    https://www.facebook.com/HobbyGameDeveloper/

    If you have any questions or problems, i might assist you there! Thanks!