How to make your text clickable šŸ’› Unity TextMeshPro and the link tag

Christina Creates Games
5 Dec 202207:27

Summary

TLDRThis tutorial delves into the versatile Text Mesh Pro link tag, exploring its potential beyond URL links to enhance game interactions. It demonstrates creating a tooltip system for a text box, showing how to use custom tags for gradient effects and handling mouse clicks to display ingredient information. The video guides viewers through setting up scripts for tooltip control, handling text interactions, and utilizing the link tag with unique keywords. It also touches on implementing animations and subscribing to events for a dynamic user experience, concluding with a call to action for feedback and further learning.

Takeaways

  • šŸ”— The Text Mesh Pro link tag is not just for URLs but can be used to integrate with game systems.
  • šŸ“š The tutorial series will explore advanced uses of the link tag, starting with a tooltip example.
  • šŸŽÆ The first example project demonstrates creating a tooltip for text or sprites in a textbox when clicked.
  • šŸ› ļø Three scripts are created: one for tooltip info, one for textbox interaction, and one for controlling the tooltip.
  • šŸ“ The link tag syntax is introduced, with emphasis on unique 'keywords' that trigger actions.
  • šŸ‘€ The 'LinkHandlerForTMPText' class is explained, detailing how to attach it to a GameObject and find necessary elements.
  • šŸ¾ Implementing the 'IPointerClickHandler' interface makes text elements clickable and able to detect links.
  • šŸ“” A system for detecting mouse position and linking it to a tooltip is described, including the use of events.
  • šŸ“œ The 'Tooltip Handler' script is detailed, showing how to manage the display of tooltips with keywords and sprites.
  • šŸ–¼ļø A 'TooltipInfos' struct is introduced to link keywords with corresponding graphics for the tooltip.
  • šŸ›‘ A method for closing the tooltip, either by clicking or reversing an animation, is suggested.

Q & A

  • What is the primary purpose of the link tag in Text Mesh Pro discussed in the script?

    -The primary purpose of the link tag in Text Mesh Pro is to enable interactive elements within the game, such as displaying tooltips when a word or a sprite in the textbox is clicked, rather than just linking to external websites.

  • What is the first example project discussed in the video?

    -The first example project is about creating a tooltip that appears when a word or sprite in a textbox is clicked, displaying the name of the ingredient and some visualization.

  • What are the three small scripts mentioned in the script that will be created?

    -The three small scripts to be created are: one for holding information for the tooltip, one for handling interactions with the textbox, and one to control the tooltip.

  • How does the script determine if there is a link under the cursor when clicked?

    -The script uses the `TMP_Text.GetLinkedTextComponent` method to check if there is a link under the cursor when clicked, which returns an int value representing the index of the link, not a boolean.

  • What is the purpose of the `LinkHandlerForTMPText` class in the script?

    -The `LinkHandlerForTMPText` class is a MonoBehaviour that handles the detection of link tags within the Text Mesh Pro component and manages the interactions when a user clicks on these tags.

  • Why is the camera required in the script and how is it handled?

    -The camera is required to calculate the mouse position relative to the UI elements. It is handled by assigning it to the script either automatically or by manually setting it in the Unity Inspector.

  • What interface does the script implement to make text elements clickable?

    -The script implements the `IPointerClickHandler` interface to make text elements clickable and respond to user clicks.

  • How does the script notify the tooltip controller to show the tooltip box?

    -The script uses an event and a delegate to notify the tooltip controller to show the tooltip box, passing a string parameter that contains the keyword associated with the clicked text.

  • What is the structure of the `TooltipInfos` script mentioned in the script?

    -The `TooltipInfos` script is a Serializable struct that contains a keyword and a Sprite, used to associate specific keywords with corresponding graphics for display in the tooltip.

  • How can the tooltip be closed in the system described?

    -The tooltip can be closed by either playing the popup animation in reverse, setting the tooltip's active state to false, or triggering a click event that sets the tooltip inactive.

  • What additional step is suggested to make clickable elements stand out in the text?

    -To make clickable elements stand out, the tutorial suggests tagging them with a distinct style, which can be learned more about in a separate tutorial focused on styles.

Outlines

00:00

šŸ”— Introduction to Text Mesh Pro's Link Tag

The video introduces the Text Mesh Pro's link tag, emphasizing its versatility beyond just displaying URLs. It suggests thinking of the link tag as a connector to game systems rather than web links. The tutorial promises to explore the link tag's capabilities through examples, starting with a project that shows a tooltip upon clicking words or sprites in a textbox. The setup includes a textbox with a bread recipe, custom gradient tags using Text Animator, and a tooltip system. The tooltip is a simple GameObject with a background image, a TextMeshPro (TMP) object, and an image component. The video outlines creating three scripts: one for tooltip information, one for textbox interaction, and one for controlling the tooltip. It explains the syntax of the link tag and how to set up a class to handle the link tag, including finding necessary elements like the canvas and camera, and implementing the IPointerClickHandler interface.

05:05

šŸ›  Developing the Tooltip System

This section delves into the development of the tooltip system. It describes creating a script called TooltipHandler that manages the tooltip's appearance and content. The script subscribes to an event triggered by the LinkHandler to display the tooltip with relevant information based on keywords. The TooltipHandler script includes a list of TooltipInfos, which are structs containing keywords and corresponding sprites. The video demonstrates how to enable the tooltip, set its title, and update the image component based on the keyword. It also covers creating a function to close the tooltip, either by playing an animation in reverse or through a click event. The tutorial concludes with instructions on tagging text elements in the TMP component to trigger the tooltip and suggests adding styles to make clickable elements stand out. The presenter encourages feedback and questions, inviting viewers to engage with the content.

Mindmap

Keywords

šŸ’”Text Mesh Pro

Text Mesh Pro (TMP) is a plugin for the Unity engine that enhances the text rendering capabilities of the game development platform. It allows for more advanced text styling and effects compared to the default Unity text component. In the video, the presenter discusses utilizing the link tag feature within TMP to create interactive text elements, such as tooltips, which go beyond simple URL links.

šŸ’”Link tag

The link tag in Text Mesh Pro is a feature that enables text elements to be interactive, allowing them to link to various actions or data within a game. The video focuses on using the link tag to create a tooltip system that displays additional information when certain words or sprites are clicked, demonstrating the tag's utility beyond just linking to websites.

šŸ’”Tooltip

A tooltip is a small, interactive, floating text label that appears when a user hovers over or clicks on an element, typically providing additional information or context. In the video script, the presenter describes creating a tooltip that appears when clicking on ingredients within a text box, showcasing how the link tag in TMP can be used to implement such a feature.

šŸ’”TMP component

The TMP component refers to the TextMesh Pro component in Unity, which is used to add rich text elements to game objects. The video script mentions attaching a LinkHandlerForTMPText script to the same GameObject as the TMP component to handle the interaction with the link tag and display tooltips.

šŸ’”IPointerClickHandler

IPointerClickHandler is an interface in Unity that allows a script to respond to pointer clicks. The video script describes implementing this interface to make text elements clickable, which is crucial for triggering the tooltip functionality when a user clicks on tagged words.

šŸ’”Canvas

In Unity, a Canvas is a UI component that serves as a container for UI elements like text, images, and buttons. The video script mentions locating the TMP text box on a Canvas and using its properties to calculate mouse positions relative to the text elements, which is necessary for detecting clicks on specific words.

šŸ’”LinkInfo

LinkInfo is a structure in Text Mesh Pro that contains information about a link, such as its ID and the text it is associated with. The video script describes using LinkInfo to retrieve details about the clicked link, which is then used to display the appropriate tooltip.

šŸ’”Serializable struct

A Serializable struct in Unity is a custom data structure that can be saved and loaded as part of a scriptableObject or used within the inspector. In the video, the presenter creates a TooltipInfos struct to store keyword-sprite pairs, which is used to associate keywords in the text with corresponding images for the tooltip.

šŸ’”Event

In the context of Unity and programming, an event is a mechanism that allows for communication between different parts of a program. The video script describes setting up an event using a delegate to notify the tooltip controller script to display the tooltip when a link is clicked.

šŸ’”Animation

Animation in Unity refers to the process of creating movement or change over time, often used to enhance the visual appeal of UI elements. The video script mentions using an animation to make the tooltip appear when it is triggered, adding a dynamic visual effect to the tooltip display.

šŸ’”Tagging

Tagging in the context of the video refers to the process of marking specific words or elements within the text for special treatment, such as triggering a tooltip. The presenter explains how to tag elements in the text with a style to make them stand out and be clickable, which is essential for the link tag functionality.

Highlights

Introduction to the link tag in Text Mesh Pro and its powerful capabilities beyond linking to websites.

Plan to explore the link tag in depth over several tutorials, starting with a tooltip example.

Base setup: a big text box with a recipe, using custom tags for gradient effects with Text Animator.

Objective: Display a tooltip box with the ingredient name and a visual representation when clicking on an ingredient.

The tooltip is a simple GameObject with a background image, a Text Mesh Pro component for the title, and an image below.

Creation of three scripts: one for tooltip information, one for handling textbox interactions, and one for controlling the tooltip.

Explanation of the link tag syntax, which requires a closing tag and allows any keyword to be set.

First script: LinkHandlerForTMPText, a MonoBehaviour that will be attached to the GameObject with the TMP component.

Setting up the LinkHandlerForTMPText script with private variables for the TMP text box, Canvas, and Camera.

Implementation of IPointerClickHandler to detect clicks and identify links under the cursor.

Using events to notify the Tooltip Controller to show the tooltip box, with a delegate and a static event.

TooltipHandler script to manage the tooltip display, including searching for the correct keyword and sprite.

Creation of a Serializable struct called TooltipInfos to store keywords and sprites for the tooltip.

Final steps involve populating the list with keywords and sprites, tagging the text, and customizing the tooltip appearance.

Recommendation to style clickable elements to make them stand out, with a suggestion to watch a related tutorial.

Transcripts

play00:00

Hi and welcome šŸ˜Š

play00:02

One of the most mysterious Text Mesh Pro tagsĀ  is the link tag. This tag can enable you to doĀ Ā 

play00:09

a lot you might not have thought possibleĀ  before and not just display a linked URL.Ā Ā 

play00:15

Think about this tag like it links up with yourĀ  gameā€™s systems, less about linking to websites.

play00:22

So, over the next few videos, weā€™ll have a lookĀ  at this mighty tag and a couple of examples ofĀ Ā 

play00:29

what we can do with it. In this video, weā€™llĀ  start with a small example project aboutĀ Ā 

play00:35

displaying some kind of tooltip when a wordĀ  or a sprite in our textbox is being clicked.Ā Ā 

play00:41

In the next tutorial, weā€™ll see how we need toĀ  modify this system to display infos when we hoverĀ Ā 

play00:47

over certain parts and go a bit deeper into whichĀ  kinds of infos we can get by using the link tag.

play00:54

The base setup of my scene is this: I have aĀ  big text box with the recipe for bread here.Ā Ā 

play01:01

These tags here are custom tags IĀ  wrote to have a gradient play byĀ Ā 

play01:05

using the Text Animator and donā€™tĀ  matter for this system to work.

play01:13

I want to have a tooltip box pop up when IĀ  click on the ingredients and I want it toĀ Ā 

play01:18

display the name of the ingredient asĀ  well as some kind of visualisation.

play01:24

The tooltip is a simple Gameobject with aĀ Ā 

play01:26

background image, a TMP object to holdĀ  the title as well as an image below.

play01:33

I also use an animation toĀ  have the tooltip appear,Ā Ā 

play01:36

but thatā€™s really just for the sake ofĀ  having something interesting to look at.

play01:42

Additionally, we will create three smallĀ  scripts: One for holding infos for our tooltip,Ā Ā 

play01:48

one for handling the interactions with theĀ  textbox and one to control the tooltip.

play01:54

The syntax of the link tag looks like this andĀ  it needs a closing tag, too. You can set anyĀ Ā 

play02:01

word you want in here - Iā€™m going to call theseĀ  the keywords. These keywords are one of a kind.

play02:08

Letā€™s get to the code.

play02:11

The first class will beĀ  called LinkHandlerForTMPText.Ā Ā 

play02:15

It is a monobehaviour and Iā€™m goingĀ  to stick it on the same Gameobject myĀ Ā 

play02:19

TMP component is on, so Iā€™ll give it aĀ  requiredComponent of the type TMP_Text.

play02:25

Our class will get three private variables: theĀ  tmpTextBox as well as the Canvas the textboxĀ Ā 

play02:32

is located on and lastly the camera we want toĀ  use. There are multiple ways of handing a cameraĀ Ā 

play02:38

to the script, Iā€™m fond of just hooking it up byĀ  hand so Iā€™ll have this show up in the inspector.

play02:45

In Awake, we will have our script find theĀ  needed elements. We needed to hand it ourĀ Ā 

play02:51

Canvas as well as our Camera (in the case of ourĀ  canvas being not in Screen Space Overlay mode),Ā Ā 

play02:58

because the way our mouse position willĀ  be calculated depends on these values.

play03:04

To have our text elements be clickable,Ā Ā 

play03:06

weā€™ll add the IPointerClickHandlerĀ  interface and implement its member.

play03:12

We need to know our mouseĀ  position. Weā€™ll get it like this.

play03:16

Next, we need to check if thereā€™s actuallyĀ  a link below our cursor when we click.Ā Ā 

play03:21

We check for it with this line of code. PleaseĀ  note, it returns an int value, not a bool.

play03:29

So, if our linkedTaggedText is not -1,Ā  which is the default return value ifĀ Ā 

play03:34

there is no link to be found, we needĀ  to get the LinkInfo of our tagged text.

play03:38

Throw a Debug.Log like this intoĀ  your code to check if your taggedĀ Ā 

play03:39

word is being found correctly and give it a test šŸ™‚

play03:40

My tooltip controler is its own script and IĀ  donā€™t want to link them up, so Iā€™ll be usingĀ Ā 

play03:46

an event to notify it to show the tooltip box.Ā  For that, weā€™ll need to add a delegate and anĀ Ā 

play03:53

event using the delegate like this. By settingĀ  it up like you see, we can now Invoke the eventĀ Ā 

play03:59

down here and hand it a string parameter forĀ  the event listener to work with. Donā€™t forgetĀ Ā 

play04:05

to set your event to static, or you wonā€™t beĀ  able to listen to it from another script šŸ˜Š

play04:14

Next up is our Tooltip Handler.Ā  Itā€™s just a basic Monobehaviour.

play04:19

It needs to know about our Tooltip, itsĀ  TMP Component to fill it with a correctĀ Ā 

play04:24

title as well as its Image component.Ā  Iā€™ll add the Image by hand while havingĀ Ā 

play04:30

it search for the TMP component on its own,Ā  because if I were to let it search for itĀ Ā 

play04:35

like I do with the text field, Iā€™d get theĀ  background image. This is quicker for now.

play04:39

In OnEnable, weā€™ll subscribe to theĀ  Event we created in our LinkhandlerĀ Ā 

play04:44

and will unsubscribe from it in OnDisable.

play04:48

The function to display ourĀ  Tooltip takes a string parameter.Ā Ā 

play04:53

If we set up our previous script correctly, itĀ  will get this part of our text handed it it.

play04:59

To be able to look up a keyword and correspondingĀ  graphic, weā€™ll need a new script - thisĀ Ā 

play05:05

time it will be a Serializabled struct calledĀ  TooltipInfos. It contains a keyword and a Sprite.

play05:14

Back in our TooltipHandler, weĀ  will create a List (or an array,Ā Ā 

play05:18

if you like) of TooltipInfos at the topĀ  of the script and in our GetTooltipInfoĀ Ā 

play05:23

function, weā€™ll search through the listā€™sĀ  elements if an entry contains the keyword.Ā Ā 

play05:29

If it does, the Tooltip gets enabled, weā€™llĀ  set the title to the keyword and set theĀ Ā 

play05:34

entryā€™s sprite to the tooltipā€™s imageĀ  component. Weā€™ll return at that point.

play05:39

If our script wasnā€™t able to find an entry,Ā Ā 

play05:42

we want to get notified, soĀ  weā€™ll add this line, as well.

play05:46

To make our system complete, weā€™ll needĀ  some kind of CloseTooltip function.Ā Ā 

play05:51

How you set this up is up to you. For example,Ā  if you add this SetActive(false) and add an EventĀ Ā 

play05:57

Trigger for a click event to your Tooltip, youā€™llĀ  be able to make it disappear by clicking on it.Ā Ā 

play06:03

My version is just playingĀ  the popup animation backwards.

play06:13

All thatā€™s left is filling the createdĀ  list with keywords and sprites and ofĀ Ā 

play06:17

course tagging the elements in ourĀ  text which we want to be affected.

play06:30

Give your system a try!

play06:32

As you can see, the elements you canĀ  click on to trigger a tooltip arenā€™tĀ Ā 

play06:36

looking any different than the rest of the texts.Ā Ā 

play06:39

For them to stand out, youā€™ll need to additionallyĀ  tag them with a style of some sort like this.Ā Ā 

play06:45

Watch this tutorial to learn all about how and whyĀ  to work with styles, itā€™ll be worth your time :)!

play06:50

I hope you enjoyed my tutorial!Ā  Please give it a like if you didĀ Ā 

play07:08

and subscribe if you enjoy learning moreĀ  about design topics for your games =)! Iā€™dĀ Ā 

play07:13

love to read your feedback and donā€™tĀ  hesitate to post your questions, too!

play07:17

Have a great week šŸ˜Š

Rate This
ā˜…
ā˜…
ā˜…
ā˜…
ā˜…

5.0 / 5 (0 votes)

Related Tags
Game DevelopmentText Mesh ProInteractive TooltipsUnity TutorialUI DesignLink TagsScriptingAnimationGame UXTooltip System