Unity/Study
유니티 실시간 딜레이 계산
홍삼맛
2024. 1. 24. 21:38
구현 소개
유니티 에디터 플레이 모드가 아니더라도 저장된 데이터로 실시간 딜레이 구현하기
기능 구현
DateTime
DateTime timerStart = DateTime.Now;
DateTime timerEnd = timerStart.Add(new TimeSpan(0, 10, 0));
- DateTime.Now를 통해 현재 시간을 확보 TimeSpan으로 10초의 딜레이 생성
Delay Coroutine
while (true)
{
if (totalSecondsLeft > 1)
{
if (totalSecondsLeft >= 60)
{
TimeSpan ts = TimeSpan.FromSeconds(totalSecondsLeft);
timeCheckText.text = $"{ts.Minutes}분 {ts.Seconds}초";
}
else
{
timeCheckText.text = Mathf.FloorToInt((float)totalSecondsLeft) + "초";
}
totalSecondsLeft -= Time.deltaTime;
yield return null;
}
else
{
// 주어진 시간 소진 시 => 정지
break;
}
}
- 불러온 시간 데이터를 통해 UI 텍스트로 "분/초" 를 표현한다. 시간이 다되면 딜레이 계산 정지