大家好,我是北京菜鳥在線的unity3d高級講師范老師
廢話不多說燕侠,直接上代碼
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class xiao : MonoBehaviour {
private int shijian;//聲明時(shí)間
public Slider bar; //聲明一個(gè)拖條
public static xiao qu; //聲明一個(gè)靜態(tài)棒掠,可以在其他類里面調(diào)用
private bool dui = true;// 開關(guān)
void Start () {
qu = this;
init(100); //開始時(shí)間
}
public void init(int a){// 觸發(fā)時(shí)間的方法
if (a == 100)
{
shijian += a; //初始化時(shí)間控制
StartCoroutine(aa()); // 調(diào)用下面時(shí)間每秒-1 的方法 孵构。StartCoroutine 協(xié)同 IEnumerator
}
else // 給時(shí)間設(shè)一個(gè)最大值為100
{
shijian += a;
shijian = (shijian > 100) ? 100 : shijian;
bar.value = shijian;
}
}
IEnumerator aa() // 時(shí)間每秒 -1 的方法
{
while (shijian > 0)
{
yield return new WaitForSeconds(1f);
shijian -= 1;
bar.value = shijian;
if (shijian <= 0) // 如果時(shí)間倒計(jì)時(shí)結(jié)束,打印一條語句烟很。
{
Debug.Log("ddddddddd");
}
}
}
}