Last active
December 7, 2015 03:54
-
-
Save sukedon/b45b1f07f46f217e57f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class CountDownUIComponent : MonoBehaviour { | |
#region 残り時間フィールド | |
/// <summary> | |
/// 分ラベル | |
/// </summary> | |
[SerializeField] | |
private UILabel minuteLabel; | |
/// <summary> | |
/// 秒ラベル | |
/// </summary> | |
[SerializeField] | |
private UILabel secondLabel; | |
/// <summary> | |
/// gvgの時間 | |
/// </summary> | |
[SerializeField] | |
private float remainTime = 600; | |
#endregion | |
// Use this for initialization | |
void Start() | |
{ | |
StartCoroutine(Timer()); | |
} | |
#region タイマー | |
private IEnumerator Timer() | |
{ | |
float timer = remainTime; | |
while (true) | |
{ | |
timer -= Time.deltaTime; | |
if (timer < 0) timer = remainTime; | |
minuteLabel.text = GetFormattedMinute(timer); | |
secondLabel.text = GetFormattedSecond(timer); | |
yield return null; | |
} | |
} | |
/// <summary> | |
/// 整形された分文字列を取得 | |
/// </summary> | |
/// <param name="time">ミリ秒</param> | |
/// <returns>nフォーマットの文字列</returns> | |
private string GetFormattedMinute(float millisecondTime) | |
{ | |
int minute = Mathf.FloorToInt(millisecondTime / 60f); | |
return string.Format("{0}", minute); | |
} | |
/// <summary> | |
/// 整形された秒文字列を取得 | |
/// </summary> | |
/// <param name="time">ミリ秒</param> | |
/// <returns>nフォーマットの文字列</returns> | |
private string GetFormattedSecond(float millisecondTime) | |
{ | |
int second = Mathf.FloorToInt(millisecondTime % 60f); | |
return string.Format("{0:D2}", second); | |
} | |
/// <summary> | |
/// 整形された時間文字列を取得 | |
/// </summary> | |
/// <param name="time">ミリ秒</param> | |
/// <returns>n:nnフォーマットの文字列</returns> | |
private string GetFormattedTime(float time) | |
{ | |
int minute = Mathf.FloorToInt(time / 60f); | |
int second = Mathf.FloorToInt(time % 60f); | |
string formattedTime = string.Format("{0}:{1:D2}", minute, second); | |
return formattedTime; | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment