nightward/Assets/Scripts/Util/TMPImageResizer.cs

26 lines
903 B
C#
Raw Normal View History

2025-11-25 19:58:58 +00:00
using TMPro;
using UnityEngine;
[RequireComponent(typeof(RectTransform))]
public class TMPImageResizer : MonoBehaviour
{
public RectTransform myRect;
public TMP_Text tmpText; // 글자를 가진 TextMeshProUGUI
public RectTransform imageRect; // 크기를 조절할 Image의 RectTransform
public Vector2 padding = new Vector2(20, 20); // 텍스트 주변 여백
public Vector2 myRectPadding = new Vector2(0, 20);
void Update()
{
if (tmpText == null || imageRect == null) return;
// TMP 실제 크기
Vector2 size = tmpText.textBounds.size;
float currentWidth = size.x + padding.x;
float currentHeight = size.y + padding.y;
// Image 크기 변경
imageRect.sizeDelta = new Vector2(currentWidth, currentHeight);
myRect.sizeDelta = new Vector2(myRect.sizeDelta.x, currentHeight + myRectPadding.y);
}
}