NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Localization/LinkText.cs

55 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2024-08-13 15:19:31 +08:00
using TMPro;
using UnityEngine;
2024-08-13 16:11:48 +08:00
using UnityEngine.Events;
using UnityEngine.EventSystems;
[RequireComponent(typeof(TMP_Text))]
public class LinkText : MonoBehaviour, IPointerClickHandler
{
2024-08-13 16:11:48 +08:00
List<UnityAction> ClickAction;
UnityAction otherAreaClickAction;
2024-08-13 16:11:48 +08:00
public void SetLinkAction(List<UnityAction> unityActions)
{
ClickAction = unityActions;
}
public void SetOtherAreaClicked(UnityAction callback)
{
otherAreaClickAction = callback;
}
2024-08-13 16:11:48 +08:00
public void OnPointerClick(PointerEventData eventData)
{
DebugUtil.Log("OnPointerClick called.");
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
}
}
else
{
otherAreaClickAction?.Invoke();
}
}
}