Skip to content

Instantly share code, notes, and snippets.

@ryo-ma
Created January 15, 2018 09:26
Show Gist options
  • Select an option

  • Save ryo-ma/c5c14bf2f4eff6a16b9a819c9c84e7eb to your computer and use it in GitHub Desktop.

Select an option

Save ryo-ma/c5c14bf2f4eff6a16b9a819c9c84e7eb to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class LimitTimer : MonoBehaviour
{
public Text timerText
public int minute;
public float seconds;
private float totalTime;
private float oldSeconds;
private float startTime;
void Start()
{
this.totalTime = this.minute * 60 + this.seconds;
this.oldSeconds = 0f;
this.startTime = Time.realtimeSinceStartup;
}
void Update()
{
float remainTime = this.totalTime - (Time.realtimeSinceStartup - this.startTime);
if (remainTime <= 0f)
{
return;
}
float remainMinute = (int)remainTime/ 60;
float remainSeconds = remainTime - remainMinute * 60;
if ((int)remainSeconds != (int)oldSeconds)
{
this.timerText.text = String.Format("limit {0}:{1}", remainMinute.ToString("00"), ((int)remainSeconds).ToString("00"));
}
this.oldSeconds = remainSeconds;
if (remainTime <= 0f)
{
Debug.Log("time finished");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment