[Unity] 보스 Timer 개발

고현규·2024년 2월 27일

코루틴을 이용한 타이머 개발이다.

public void TimerOn()
{
    _time = 60;
    image_Timer.gameObject.SetActive(true);
    StartCoroutine(StartTimer());
}

public void TimerOff()
{
    image_Timer.gameObject.SetActive(false);
    StopCoroutine(StartTimer());
}

IEnumerator StartTimer()
{
    _curTime = _time; 
    while(_curTime > 0)
    {
        _curTime -= Time.deltaTime;
        _minute = (int)_curTime / 60; 
        _second = (int)_curTime % 60;
        _textTime.text = $"{_minute.ToString("00")} : {_second.ToString("00")}";
        yield return null;

        if(_curTime <= 0)
        {
            Debug.Log("시간 종료");
            _curTime = 0;
            image_Timer.gameObject.SetActive(false);
            yield break;
        }
    }
}

이런 식으로 나오는데, 게이지 형태에 숫자가 나타나는 것으로 변경하는 게 좋아보여서 추가 할 예정이다.

[도움 받은 자료 출처] https://velog.io/@slow_cosmos/Unity-%ED%83%80%EC%9D%B4%EB%A8%B8-%EA%B5%AC%ED%98%84

profile
게임 개발과 기획

0개의 댓글