[Unity] UI Text with clickable words

Discussion in 'Game Development (Technical)' started by Scoper, Jan 5, 2017.

  1. Scoper

    Scoper
    Expand Collapse
    Indie Author

    Joined:
    Nov 5, 2015
    Messages:
    62
    Likes Received:
    17
    In Unity 5.5, I need to display some text to the player with certain words highlighted in blue. If the player clicks one of these words the game displays the explanation for that word.

    Any ideas on how I could achieve that?
    The Unity UI.Text component allows rich text including text color, but it does not allow html-style links or anything similar.
    I also checked out the Unity Asset Store, but could find nothing useful.
     
    #1 Scoper, Jan 5, 2017
    Last edited: Jan 5, 2017
  2. Scoper

    Scoper
    Expand Collapse
    Indie Author

    Joined:
    Nov 5, 2015
    Messages:
    62
    Likes Received:
    17
    I found a solution. Someone made a subclass of the Text component that supports clickable links. http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/#post-1753530
    I had to modify it to get it to work, but the essence is quite simple: Get the cachedTextGenerator.verts property of Text and compare mouse position to the vertices of characters in the string. The mouse position in the text recttransform can be calculated with RectTransformUtility.ScreenPointToLocalPointInRectangle and scaled to compensate for CanvasScaler. That allows me to test if the mouse is over a keyword.
     

Share This Page