素材巴巴 > 程序开发 >

Unity笔记(14):Timer [2D]

程序开发 2023-09-17 07:41:57

参考:Unity实现倒计时功能 - 知乎

 1、在侧边栏中新建Text

 2、创建脚本

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;public class TimeDelate : MonoBehaviour
 {public GameObject text;public int TotalTime = 60;void Start(){StartCoroutine(Time());}IEnumerator Time(){while (TotalTime >= 0){text.GetComponent().text = TotalTime.ToString();yield return new WaitForSeconds(1);TotalTime--;}}void update(){if (TotalTime == 0){Debug.Log("game over");}}}

3、将脚本挂载到Text上

设置栏如下,60为初始时间。

4、运行如下

​​​​​​​

 


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。