Unity 倒计时简单实现

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 倒计时简单实现)