상세 컨텐츠

본문 제목

유니티 타이머 만들기

카테고리 없음

by MJ_119 2025. 1. 7. 05:33

본문

using TMPro;
using UnityEngine;

public class TimeManager : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI Timetext;
    public double time = 60f; // 설정 타이머 시간

    void Start()
    {
        Timetext.text = time.ToString();
    }

    void Update()
    {
        // 남은 시간을 감소시킵니다.
        time -= Time.deltaTime;

        int hours = Mathf.FloorToInt((float)time / 3600);
        int minutes = Mathf.FloorToInt((float)(time % 3600) / 60);
        int seconds = Mathf.FloorToInt((float)time % 60);

        // UI Text 컴포넌트에 남은 시간을 표시합니다.
        Timetext.text = string.Format("{0:D2}:{1:D2}:{2:D2}", hours, minutes, seconds);
    }
}

 

 

1. 스크립트를 빈 오브젝트나 특정 오브젝트에 넣고 텍스트 UI를 캔버스 안에 만들고 인스펙터창에서 할당해준다.