Unity
[Unity] 텍스트 길이에 맞춰 UI 사이즈 조절하는법
기억해조
2023. 9. 15. 11:04
private void SettingTextWidth(RectTransform rectTrf,TextMeshProUGUI tmpText, string textValue)
{
tmpText.text = textValue;
Vector2 rectSize = rectTrf.sizeDelta;
rectSize.x = tmpText.preferredWidth;
rectTrf.sizeDelta = rectSize;
}
preferredWidth
를 사용하면 길이를 알 수 있다!..
출처
https://polarbearworld.tistory.com/167
Unity & C# ~ TMPRO에서 텍스트 길이 아는 법
TMPRO에서는 UI의 길이뿐 아니라, 현재 사용한 텍스트의 길이를 확인할 수 있는 명령어가 있다. using TMPro; TMP_Text a; void Start() { float b = a.preferredWidth; //b => a의 계산된 텍스트 폭 }
polarbearworld.tistory.com