Unity
[Unity] 현실 시간에 기반한 타이머 만들기
Guk-blog
2020. 11. 18. 15:09
728x90
반응형
보통 타이머로 쓰는 방식은
float timer =0;
while(true){
timer+= Time.DeltaTime;
//or
timer+= Time.UnscaledTime;
}
일텐데 이걸 노래나 실제 시간과 비교해보면 값이 다른 걸 확인할 수 있다
실제 시간과 동일하게 진행되는 값이 필요하다면 아래 방식을 쓰면 된다
using System.Diagnostics;
StopWatch stopwatch = new StopWatch();
float timer = 0;
void TimerStart(){
stopwatch.Start();
}
void TimerStop(){
stopwatch.Stop();
}
void TimerReset(){
stopwatch.Reset();
}
void Update(){
timer = (float)stopwatch.Elapsed.TotalMinutes;
UnityEngine.Debug.Log(timer);
}
Stopwatch를 사용하면 Debug가 UnityEngine과 충돌이 되기 때문에 UnityEngine.Debug 로 사용해줘야한다
728x90
반응형