刚开始用C#的定时器功能;
后来发现Unity3d自己也有定时器功能,而且还简单。
例子如下:
using UnityEngine;
using System.Collections;
using System.Timers;
public class Timer : MonoBehaviour {
int iCount=0;
private System.Timers.Timer m_tmr=new System.Timers.Timer();
// Use this for initialization
void Start () {
InvokeRepeating("Timer_1",1,3);
m_tmr.Elapsed += Timer_2;
m_tmr.Interval = 2000;
m_tmr.Enabled = true;
}
void Timer_1()
{
iCount++;
print ("wyz------timer1------"+iCount);
if(iCount >= 5)
{
CancelInvoke();
}
}
void Timer_2(object send,ElapsedEventArgs e)
{
iCount++;
print ("wyz------timer2------"+iCount);
}
// Update is called once per frame
void Update () {
if(iCount >=10)
{
m_tmr.Elapsed -= Timer_2;
InvokeRepeating("Timer_1",1,3);
}
}
}
效果: