[Unity] UI Text with clickable words

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

  1. Scoper

    Indie Author

    Joined:
    Nov 5, 2015
    Messages:
    80
    Likes Received:
    19
    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

    Indie Author

    Joined:
    Nov 5, 2015
    Messages:
    80
    Likes Received:
    19
    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

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer