Unity中三种计时器实现⽅式
public int timerSec;学生剪什么发型好看
private Text txtTimer;
void Start()
{
txtTimer = GetComponent<Text>();
蘑菇的英文
}
private void SecDownTimer()
{
timerSec--;
酒店工程< = string.Format("{0:d2}:{1:d2}", timerSec / 60, timerSec % 60);
新世界 日语
}
1.在Update中(){}利⽤Time.time(游戏时间)标记初始时间点,在update中判断标记时间+1;后不断标记新的时间点.
private float nextTime = 1;
onmondayprivate void SecDownTimer1()
{
uji//再逐帧中单运⾏某个语句
if (Time.time >= nextTime)//利⽤Time.time,利⽤这个思路可以隔帧运⾏
{
timerSec--;
< = string.Format("{0:d2}:{1:d2}", timerSec / 60, timerSec % 60);
nextTime = Time.time + 1;
}
}
2.在Update中利⽤Time.delta(帧间隔)不断叠加间隔,⽐1⼤就计时.
private float totalTime;
private void SecDownTimer2()
{
totalTime += Time.deltaTime;
if(totalTime>=1)
notuntil的用法{
timerSec--;
< = string.Format("{0:d2}:{1:d2}", timerSec / 60, timerSec % 60); totalTime = 0;
}the kings speech
}
3.InvokeRepeating(//重复调⽤(被执⾏的⽅法名称,第⼀次执⾏时间,每次执⾏间隔))
void Start()
{
//重复调⽤(被执⾏的⽅法名称,第⼀次执⾏时间,每次执⾏间隔)
InvokeRepeating("SecDownTimer", 1, 1);
旁其
}
void Update()
{
if(timerSec<=0)
{
CancelInvoke("SecDownTimer");
学府考研
}
}