I am trying to add hyperlinks in my text by using text mesh pro asset , I have added the tag for it and the Scripts TMP_text_Selector_B to it and TMP_text_Info Debug to it and have added different ids to each link textx, now the problem is that when I click my other texts also it is opening the other links if some one is having a solution for how to add hyperlinks for multiple texts in a line it would be of great help
Was figuring this out today as I was running into similar issues.
Here is an example of the script attached to the same gameObject that has my TextMeshProUGUI component.
using UnityEngine;
using UnityEngine.EventSystems;
using TMPro;
public class MyTextMeshLinkHandler : MonoBehaviour, IPointerClickHandler
{
TextMeshProUGUI textMeshPro;
Canvas canvas;
Camera camera;
void Awake ()
{
textMeshPro = gameObject.GetComponent<TextMeshProUGUI> ();
canvas = gameObject.GetComponentInParent<Canvas> ();
if (canvas.renderMode == RenderMode.ScreenSpaceOverlay) {
camera = null;
} else {
camera = canvas.worldCamera;
}
}
public void OnPointerClick (PointerEventData eventData)
{
int linkIndex = TMP_TextUtilities.FindIntersectingLink (textMeshPro, Input.mousePosition, camera);
if (linkIndex != -1) {
TMP_LinkInfo linkInfo = textMeshPro.textInfo.linkInfo [linkIndex];
switch (linkInfo.GetLinkID ()) {
case "id_1":
Application.OpenURL ("http://answers.unity3d.com");
break;
case "id_2":
//Do something else
break;
}
}
}
}
And the content in the Text Input Box for your TextMeshProUGUI component would have something like this.
Hello, I get help from <link="id_1">AnswersUnity</link> and this <link="id_2">Does something else</link>
Just a side note this worked on desktop and iPhone too.
This works better:
TMP_TextUtilities.FindIntersectingLink( textMeshProUGUI, pointerEventData.position, pointerEventData.pressEventCamera );