版权声明:本文为博主原创文章,未经博主允许不得转载,如需转载请先得到博主的同意,如需帮助,联系[email protected],谢谢。 https://blog.csdn.net/HW140701/article/details/84824064
1 使用协程WaitForSeconds()或者WaitForSecondsRealtime()做唤醒
利用协程等待时间的机制以此达到定时的目的
void Start()
{
StartCoroutine(DoSomething());
}
IEnumerator DoSomething()
{
while (true)
{
yield return new WaitForSeconds(5.0f); // 等待5秒再执行
Debug.Log(string.Format("CurrentTime={0}", Time.time));
}
}
2 人为定义
人为定义一个定时变量控制时间,在Update()方法中对所定义的定时变量进行操作以达到定时的目的。
public float m_CurrentTime= 5.0f;
void Update()
{
m_CurrentTime-= Time.deltaTime;
if (m_CurrentTime<= 0)
{
Debug.Log(string.Format("CurrentTime={0}", Time.time)); //do something ...
timer = 5.0f; //时间复原
}
}