OneShotOneKill/Assets/Script/UGUI/Util/FollowTextEnd.cs

40 lines
1.2 KiB
C#

using UnityEngine;
using TMPro;
public class FollowTextEnd : MonoBehaviour
{
public TextMeshProUGUI textMeshPro; // 따라갈 텍스트
public RectTransform icon; // 따라다닐 아이콘 (이미지, 오브젝트 등)
public float RightOffset = 0.25f;
virtual protected void Update()
{
if (textMeshPro == null || icon == null)
return;
SetIconPosition();
}
void SetIconPosition()
{
// 마지막 글자의 위치를 얻음
Vector3 lastCharPos = GetLastCharacterPosition();
// 아이콘 위치를 조정
icon.position = new Vector3(lastCharPos.x + RightOffset, icon.position.y);
}
Vector3 GetLastCharacterPosition()
{
TMP_TextInfo textInfo = textMeshPro.textInfo;
int lastCharIndex = textMeshPro.text.Length - 1;
if (lastCharIndex < 0 || lastCharIndex >= textInfo.characterCount)
return textMeshPro.transform.position;
TMP_CharacterInfo charInfo = textInfo.characterInfo[lastCharIndex];
Vector3 worldPos = textMeshPro.transform.TransformPoint(charInfo.topRight); // 마지막 글자의 오른쪽 위 좌표
return worldPos;
}
}