안녕하세요. 달리는애아빠입니다.
초를 입력 받아 분초로 표현하는 알고리즘입니다.
해당하는 코드를 응용하면 많은 활용도가 있으니 잘 적용해보시길 바랍니다.
using System; //초를 입력받아 분,초 표시 public string getParseTime(float time) { string t = TimeSpan.FromSeconds(time).ToString("mm\\:ss"); string[] tokens = t.Split(':'); return tokens[0] + ":" + tokens[1]; } //초를 입력받아 분,초,밀리세컨트 public string getParseTime(float time) { string t = TimeSpan.FromSeconds(time).ToString("mm\\:ss\\:ff"); string[] tokens = t.Split(':'); return tokens[0] + ":" + tokens[1] + ":" + tokens[2]; } | cs |
public int GetMinute(float time) { return (int)((time / 60) % 60); } public int GetSecond(float time) { return (int)(time % 60); } public string GetMilliseconds(float time) { string milliseconds = string.Format("{0:.00}", (time % 1)); milliseconds = milliseconds.Replace(".", ""); return milliseconds; } | cs |
2가지 형태로 상황에 맞게 응용하시면 시간 파싱 부분은 크게 어려움 없이 사용하시리라 봅니다.
결과
미약하게나마 유용한 정보가 되었다면 구독 공감 버튼은 작성자에게 큰 힘이 됩니다. 감사합니다 ^^
1. 소스
타이머입니다.
스톱 워치로 작업 했지만, 중간에 정지 기능을 사용하지 않으면, 그냥 타이머로도 사용 가능합니다.
멤버 변수 |
private float m_TotalSeconds; |
메서드 |
string StockwatchTimer() { m_TotalSeconds += Time.deltaTime; TimeSpan timespan = TimeSpan.FromSeconds(m_TotalSeconds); string timer = string.Format("{0:00}:{1:00}:{2:00}.{3:00}", timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds); return timer;
|
2. 데모
스페이스키를 눌려야 시작합니다.
그리고 또 스페이키를 누려면 멈추기, 다시 누려면 시작합니다.
using UnityEngine; using UnityEngine.UI; using System; public class DemoStockwatch : MonoBehaviour { public string m_Timer = @"00:00:00.000"; public KeyCode m_KcdPlay = KeyCode.Space; private bool m_IsPlaying; public float m_TotalSeconds; // 만약 시간에 따라서 이벤트를 발생하려면, 이 값을 사용하면 됩니다. public Text m_Text; void Update() { if (Input.GetKeyDown(m_KcdPlay)) m_IsPlaying = !m_IsPlaying; if (m_IsPlaying) { m_Timer = StockwatchTimer(); } if (m_Text) m_Text.text = m_Timer; } string StockwatchTimer() { m_TotalSeconds += Time.deltaTime; TimeSpan timespan = TimeSpan.FromSeconds(m_TotalSeconds); string timer = string.Format("{0:00}:{1:00}:{2:00}.{3:00}", timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds); return timer; } }3. 결과
1. 소스
카운트 다운 되는 타이머를 만든 소스입니다.
멤버 변수로 m_TotalSeconds를 사용하며, 혹시 Component에 추가 후 수정을 하고 싶으면, 꼭 인스펙트 창에서 값을 수정하시기 바랍니다.
멤버 변수 |
public float m_TotalSeconds = 5 * 60; // 카운트 다운 전체 초(5분 X 60초), 인스펙트 창에서 수정해야 함. |
메서드 |
private string CountdownTimer(bool IsUpdate = true) { if(IsUpdate) m_TotalSeconds -= Time.deltaTime; TimeSpan timespan = TimeSpan.FromSeconds(m_TotalSeconds); return timer; |
2. 데모
아래 데모 소스를 만든 후, 인스펙트 창에서 Text에 카운트가 찍힐 Text UI를 할당 한 후 사용하시기 바랍니다.
이 소스는 m_IsPlaying가 처음에 false로 잡혀 있기 때문에, Space 키를 눌려야 시작하며, 다시 Space 키를 누르면 타이머가 멈추게 되어 있습니다.
만약 카운트 다운이 0이 되었을 때 어떤 이벤트를 원하면, 아래 빨간 색으로 되어 있는 [이벤트]부분에 넣으면 됩니다.
스페이스키를 눌려야 시작합니다.
그리고 또 스페이키를 누려면 멈추기, 다시 누려면 시작합니다.
using UnityEngine; using UnityEngine.UI; using System; public class Countdown : MonoBehaviour { public string m_Timer = @"00:00:00.000"; public KeyCode m_KcdPlay = KeyCode.Space; private bool m_IsPlaying; public float m_TotalSeconds = 5 * 60; // 카운트 다운 전체 초(5분 X 60초), 인스펙트 창에서 수정해야 함. public Text m_Text; private void Start() { m_Timer = CountdownTimer(false); // Text에 초기값을 넣어 주기 위해 } private void Update() { if (Input.GetKeyDown(m_KcdPlay)) m_IsPlaying = !m_IsPlaying; if (m_IsPlaying) { m_Timer = CountdownTimer(); } // m_TotalSeconds이 줄어들때, 완전히 0에 맞출수 없기 때문에 if (m_TotalSeconds <= 0) { SetZero(); //... 여기에 카운트 다운이 종료 될때 [이벤트]를 넣으면 됩니다. } if (m_Text) m_Text.text = m_Timer; } private string CountdownTimer(bool IsUpdate = true) { if(IsUpdate) m_TotalSeconds -= Time.deltaTime; TimeSpan timespan = TimeSpan.FromSeconds(m_TotalSeconds); string timer = string.Format("{0:00}:{1:00}:{2:00}.{3:000}", timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds); return timer; } private void SetZero() { m_Timer = @"00:00:00.000"; m_TotalSeconds = 0; m_IsPlaying = false; } }