1、利用Update();适合重复调用
public float timer = 1.0f;
// Update is called once per framevoid Update() {
timer -= Time.deltaTime;
if (timer <= 0) {
timer = 1.0f;
}
}
2、利用协程;
void Start() {
StartCoroutine(Timer());
}
IEnumerator Timer() {
while (true) {
yield return new WaitForSeconds(1.0f);
}
}
3、利用Invoke()
// Use this for initializationvoid Start() {
Invoke("Timer", 1.0f);
}
void Timer() {
}
4、利用InvokeRepeating();
void Timer() {}
public void Awake() {
InvokeRepeating("Timer", 1, 2f); //1秒后,每2f调用一次
}