using UnityEngine;
using System.Collections;
public class TimeRecovery : MonoBehaviour {
public int totalSeconds = 60;
private int leaveSeconds;
private bool onCountDown = false;
private string countDownTitle = "Start";
void Awake()
{
leaveSeconds = totalSeconds;
}
void OnGUI()
{
GUI.Label(new Rect(50, 50, 50, 50), leaveSeconds.ToString());// 倒计时秒数 //
if (GUI.Button(new Rect(150, 50, 80, 30), countDownTitle))
{
if (countDownTitle == "Start")
{
countDownTitle = "Pause";
onCountDown = true;
StartCoroutine(DoCountDown());
}
else
{
countDownTitle = "Start";
onCountDown = false;
StopAllCoroutines();
}
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
IEnumerator DoCountDown()
{
while (leaveSeconds > 0)
{
yield return new WaitForSeconds(1f);
leaveSeconds--;
}
}
}
Unity 倒计时简单实现
最新推荐文章于 2024-04-14 16:47:54 发布