In Unity3D, you can use TextMeshPro or TextMeshProUGUI with embedded links. For example, your text can be:
But this is not enough to get it open on click. You also need to handle the click and execute the action you want, based on the link ID (here the link ID is the URL). To do that, just attach this script to the same object of your TextMeshPro or TextMeshProUGUI component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using TMPro; using UnityEngine; using UnityEngine.EventSystems; [RequireComponent(typeof(TMP_Text))] public class LinkOpener : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { TMP_Text pTextMeshPro = GetComponent<TMP_Text>(); int linkIndex = TMP_TextUtilities.FindIntersectingLink(pTextMeshPro, eventData.position, null); // If you are not in a Canvas using Screen Overlay, put your camera instead of null if (linkIndex != -1) { // was a link clicked? TMP_LinkInfo linkInfo = pTextMeshPro.textInfo.linkInfo[linkIndex]; Application.OpenURL(linkInfo.GetLinkID()); } } } |
This will open a simple URL in the device default browser. If you want to execute other actions within the app itself, just remove the OpenURL call and put the code you want.
By Jame 30/12/2019 - 05:36
Thank you! I was trying to follow another guide but it wasn’t quite working. Your code seemed almost identical but for some reason yours worked flawlessly.
By Roman 04/05/2020 - 19:21
Is there some limitations about Canvas usage? For some reason it doesn’t work at all on Overlay Canvas, and pointer click event even not triggered.
By Memento mori – Programación de videojuegos 2D 01/02/2023 - 10:34
[…] “[Unity3D] Manage links with TextMeshPro” – Nicolas Form [Feel out the Form] – https://www.feelouttheform.net/unity3d-links-textmeshpro/ […]
By Memento mori – Salvador Banderas Rovira 01/02/2023 - 11:28
[…] «[Unity3D] Manage links with TextMeshPro» – Nicolas Form [Feel out the Form] – https://www.feelouttheform.net/unity3d-links-textmeshpro/ […]