Learning
When: 1학년 9월 5일
How:
UI 작업을 하다 재화량의 글자 수에 맞게 재화를 표시하는 박스의 크기를 키우는 기능을 구현해야 했기 때문에 공부하게 되었습니다.
Understanding:
public void SetText(string text)
{
if (text.Length <= 0) return;
float currentX = tmp.preferredWidth;
tmp.SetText(text);
tmp.ForceMeshUpdate();
float plusX = tmp.preferredWidth - currentX;
element.minWidth += plusX;
}
- 설명 : TextMeshProGUI의 text 크기를 구하는 방법은 GetRenderedValues()와 preferredWidth, preferredHeight 등이 있는데, 여기선 높이가 아닌 길이만 바꾸고 싶었기 때문에 나중 길이에 처음 길이를 빼서 커진 값을 element.minWidth에 더하는 방식으로 구현했습니다.
Result: 전체 크기(Vector2)가 필요하다면 GetRenderedValues(), 높이만 필요하다면 preferredHeight, 길이만 필요하다면 preferredWidth를 사용하여 TextMeshProUGUI의 text 길이를 구할 수 있습니다.
'UnityEngine' 카테고리의 다른 글
| [Unity] UI 선택 판정 버그 (0) | 2025.09.12 |
|---|---|
| [Unity] 2D에서 Light를 다루는 기본적인 방법 (0) | 2025.09.09 |
| [Unity] Layout Element (0) | 2025.09.04 |
| [Unity] Spline (0) | 2025.09.03 |
| [Unity] DOTween Seqence (3) | 2025.08.21 |