2024-08-14 17:24:50 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2024-08-13 15:19:31 +08:00
|
|
|
|
using TMPro;
|
2024-08-12 17:34:04 +08:00
|
|
|
|
using UnityEngine;
|
2024-08-13 16:11:48 +08:00
|
|
|
|
using UnityEngine.Events;
|
2024-08-12 17:34:04 +08:00
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
|
|
|
|
|
|
|
[RequireComponent(typeof(TMP_Text))]
|
|
|
|
|
|
public class LinkText : MonoBehaviour, IPointerClickHandler
|
|
|
|
|
|
{
|
2024-08-13 16:11:48 +08:00
|
|
|
|
List<UnityAction> ClickAction;
|
|
|
|
|
|
|
2024-08-14 17:24:50 +08:00
|
|
|
|
UnityAction otherAreaClickAction;
|
|
|
|
|
|
|
2024-08-13 16:11:48 +08:00
|
|
|
|
public void SetLinkAction(List<UnityAction> unityActions)
|
|
|
|
|
|
{
|
|
|
|
|
|
ClickAction = unityActions;
|
|
|
|
|
|
}
|
2024-08-14 17:24:50 +08:00
|
|
|
|
public void SetOtherAreaClicked(UnityAction callback)
|
|
|
|
|
|
{
|
|
|
|
|
|
otherAreaClickAction = callback;
|
|
|
|
|
|
}
|
2024-08-13 16:11:48 +08:00
|
|
|
|
|
2024-08-12 17:34:04 +08:00
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
|
|
|
|
{
|
2024-08-14 17:24:50 +08:00
|
|
|
|
DebugUtil.Log("OnPointerClick called.");
|
2024-08-12 17:34:04 +08:00
|
|
|
|
TMP_Text pTextMeshPro = GetComponent<TMP_Text>();
|
|
|
|
|
|
|
|
|
|
|
|
int linkIndex =
|
|
|
|
|
|
TMP_TextUtilities.FindIntersectingLink(pTextMeshPro, eventData.position, eventData.pressEventCamera);
|
|
|
|
|
|
// If you are not in a Canvas using Screen Overlay, put your camera instead of null
|
|
|
|
|
|
|
|
|
|
|
|
if (linkIndex != -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
TMP_LinkInfo linkInfo = pTextMeshPro.textInfo.linkInfo[linkIndex];
|
|
|
|
|
|
DebugUtil.Log("OnPointerClick called.");
|
|
|
|
|
|
DebugUtil.Log("Link ID: \"" + linkInfo.GetLinkID() + "\" Link Text: \"" + linkInfo.GetLinkText());
|
2024-08-13 15:19:31 +08:00
|
|
|
|
//Application.OpenURL(linkInfo.GetLinkID());
|
|
|
|
|
|
|
2024-08-13 16:11:48 +08:00
|
|
|
|
if (ClickAction != null && linkIndex < ClickAction.Count)
|
2024-08-13 15:19:31 +08:00
|
|
|
|
{
|
2024-08-13 16:11:48 +08:00
|
|
|
|
ClickAction[linkIndex]();
|
2024-08-13 15:19:31 +08:00
|
|
|
|
}
|
2024-08-13 16:11:48 +08:00
|
|
|
|
else
|
2024-08-13 15:19:31 +08:00
|
|
|
|
{
|
2024-08-13 16:11:48 +08:00
|
|
|
|
DebugUtil.LogError("Link index out of range.");
|
2024-08-13 15:19:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-14 17:24:50 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
otherAreaClickAction?.Invoke();
|
|
|
|
|
|
}
|
2024-08-12 17:34:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|