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函數
Invokes the method methodName in time seconds.
在time秒調用methodName方法;簡單說,根據時間調用指定方法名的方法
After the first invocation repeats calling that function every repeatRate seconds.
從第一次調用開始,每隔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.CancelInvoke(string methodName)
取消這個腳本中所有的調用
Cancels all Invoke calls on this MonoBehaviour.
取消這個MonoBehaviour上的所有調用。
public
class
example : MonoBehaviour {
public
Rigidbody projectile;
void
Update() {
if
(Input.GetButton(
"Fire1"
))
CancelInvoke();
}
void
LaunchProjectile() {
instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere *
5
;
}
public
void
Awake() {
InvokeRepeating(
"LaunchProjectile"
,
2
,
0
.3F);
}
}
function LaunchProjectile () {
instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere *
5
;
}
4.(bool) IsInvoking(string methodName)
using UnityEngine;
using System.Collections;
public
class
example : MonoBehaviour {
public
Rigidbody projectile;
void
Update() {
if
(Input.GetKeyDown(KeyCode.Space) && !IsInvoking(
"LaunchProjectile"
))
//如果這個方法不在調用並且等待了2秒
Invoke(
"LaunchProjectile"
,
2
);
}
void
LaunchProjectile() {
Rigidbody instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere *
5
;
}
}