유니티

[유니티] 문자열로 변환하기 (ToString)

유니티 게임 개발 2024. 12. 22. 23:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ZZZPractice : MonoBehaviour
{
    public Text timeText; // Text 변수에는 문자열만 들어감
    float time; // 실수이기 때문에 문자열에 못 들어감

    void Update()
    {
        time += Time.deltaTime;
        timeText.text = time.ToString(); // 실수를 문자열로 변경해서 넣음
    }
}

 

ToString은 보통 UI의 텍스트에 들어갈 시간이나 숫자를 넣을 때 사용한다.

 

 

time.ToString
=
time이라는 실수 값을 문자열로 변경해라.

 

 

 

참고사항

 

 

ToString("F2");

= 소수점 두 번째 자리까지 표시해줘.

 

ToString("N2");

= 소수점 두번째 자리까지 표시해주고, 천 단위 콤마 찍어줘.