1.Invoke(string methodName,float time)
- 在一定時間調用methodName函數
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Rigidbody projectile;
void LaunchProjectile() {
Rigidbody instance =
Instantiate(projectile);
instance.velocity = Random.insideUnitSphere
* 5;
}
public void Awake() {
Invoke("LaunchProjectile", 2);
}
}
2.InvokeRepeating(string methodName,float time,float repeatRate)
- 每隔一定時間調用一次methodName函數
在time秒調用methodName方法;簡單說,根據時間調用指定方法名的方法
從第一次調用開始,每隔repeatRate時間調用一次.
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Rigidbody projectile;
void LaunchProjectile() {
Rigidbody instance =
Instantiate(projectile);
instance.velocity = Random.insideUnitSphere
* 5;
}
public void Awake() {
InvokeRepeating("LaunchProjectile", 2, 0.3F); //2秒后,每0.3f調用一次
}
}
3.CanceInvoke("methodName")
- 取消所有名為methodName的調用.
