40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
|
|
public class FollowTextFront : MonoBehaviour
|
|
{
|
|
public TextMeshProUGUI textMeshPro; // 따라갈 텍스트
|
|
public RectTransform icon; // 따라다닐 아이콘
|
|
public float LeftOffset = 0.25f;
|
|
|
|
virtual protected void Update()
|
|
{
|
|
if (textMeshPro == null || icon == null)
|
|
return;
|
|
|
|
SetIconPosition();
|
|
}
|
|
|
|
void SetIconPosition()
|
|
{
|
|
// 첫 글자의 위치를 얻음
|
|
Vector3 firstCharPos = GetFirstCharacterPosition();
|
|
|
|
// 아이콘 위치를 조정
|
|
icon.position = new Vector3(firstCharPos.x - LeftOffset, icon.position.y, icon.position.z);
|
|
}
|
|
|
|
Vector3 GetFirstCharacterPosition()
|
|
{
|
|
TMP_TextInfo textInfo = textMeshPro.textInfo;
|
|
|
|
// 텍스트가 비어있으면 기본 위치 반환
|
|
if (textMeshPro.text.Length == 0 || textInfo.characterCount == 0)
|
|
return textMeshPro.transform.position;
|
|
|
|
TMP_CharacterInfo charInfo = textInfo.characterInfo[0]; // 첫 번째 글자
|
|
Vector3 worldPos = textMeshPro.transform.TransformPoint(charInfo.topLeft); // 첫 글자의 왼쪽 위 좌표
|
|
|
|
return worldPos;
|
|
}
|
|
} |