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-03-09 09:56:50 发布
这篇博客介绍了如何在Unity中实现倒计时功能。通过创建一个TimeRecovery脚本,利用C#的协程功能进行倒计时,当点击“Start”按钮时开始倒计时,显示剩余秒数,并在倒计时结束后自动停止。用户可以自定义总秒数并随时暂停或重新开始倒计时。
摘要由CSDN通过智能技术生成