Skip to content

Instantly share code, notes, and snippets.

@sansuke05
Created December 22, 2019 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sansuke05/bcd01d87b90b1dfd0af1adb6ac1977dd to your computer and use it in GitHub Desktop.
Save sansuke05/bcd01d87b90b1dfd0af1adb6ac1977dd to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour {
private float totalTime;
[SerializeField]
private int min;
[SerializeField]
private float sec;
private float oldSec;
private Text timerText;
// Start is called before the first frame update
void Start() {
totalTime = min * 60 + sec;
oldSec = 0f;
timerText = GetComponentInChildren<Text>();
}
// Update is called once per frame
void Update() {
if(totalTime <= 0f) {
return;
}
totalTime = min * 60 + sec;
totalTime -= Time.deltaTime;
min = (int) totalTime / 60;
sec = totalTime - min * 60;
if((int)sec != (int)oldSec) {
timerText.text = min.ToString("00") + ":" + ((int)sec).ToString("00");
}
oldSec = sec;
if (totalTime <= 0f) {
Debug.Log("制限時間終了");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment