来源http://wiki.ceeger.com/start?do=search&id=Invoke
延时是个很常用的功能
n 秒后执行 一个函数,每隔 n 秒重复执行 一个函数。
反悔了,想取消它?
可以统统取消,也可以只取消具体的某一个延时执行的设定。
JavaScript ⇒ Invoke(methodName: string, time: float): void;
C# ⇒ void Invoke(string methodName, float time);
Description 描述
Invokes the method methodName in time seconds.
在time秒后,延迟调用方法methodName。
// Launches a projectile in 2 seconds
var projectile : Rigidbody;
Invoke("LaunchProjectile", 2);
function LaunchProjectile ()
{
var instance : Rigidbody = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5;
}
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Rigidbody projectile;
void LaunchProjectile() {
Rigidbody instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5;
}
void Example() {
Invoke("LaunchProjectile", 2);
}
}
JavaScript ⇒ InvokeRepeating(methodName: string, time: float, repeatRate: float): void;
C# ⇒ void InvokeRepeating(string methodName, float time, float repeatRate);
Description 描述
Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.
在time秒调用methodName方法,然后每repeatRate秒重复调用。
// Starting in 2 seconds.
// a projectile will be launched every 0.3 seconds
var projectile : Rigidbody;
InvokeRepeating("LaunchProjectile", 2, 0.3);
function LaunchProjectile ()
{
var instance : Rigidbody = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5;
}
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Rigidbody projectile;
void LaunchProjectile() {
Rigidbody instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5;
}
void Example() {
InvokeRepeating("LaunchProjectile", 2, 0.3F);
}
}
JavaScript ⇒ CancelInvoke(): void;
C# ⇒ void CancelInvoke();
Description 描述
Cancels all Invoke calls on this MonoBehaviour.
在当前MonoBehaviour,取消所有Invoke调用
// Starting in 2 seconds.
// a projectile will be launched every 0.3 seconds
var projectile : Rigidbody;
InvokeRepeating("LaunchProjectile", 2, 0.3);
// Cancels the repeating invoke call,
// when the user pressed the ctrl button
function Update()
{
if (Input.GetButton ("Fire1"))
CancelInvoke();
}
function LaunchProjectile ()
{
instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5;
}
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Rigidbody projectile;
void Update() {
if (Input.GetButton("Fire1"))
CancelInvoke();
}
void LaunchProjectile() {
instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5;
}
void Example() {
InvokeRepeating("LaunchProjectile", 2, 0.3F);
}
}
JavaScript ⇒ CancelInvoke(methodName: string): void;
C# ⇒ void CancelInvoke(string methodName);
Description 描述
Cancels all Invoke calls with name methodName on this behaviour.
在当前behaviour,取消所有方法名为methodName的Invoke调用。
// Starting in 2 seconds.
// a projectile will be launched every 0.3 seconds
var projectile : Rigidbody;
InvokeRepeating("LaunchProjectile", 2, 0.3);
// Cancels the repeating invoke call, // when the user pressed the ctrl button
function Update()
{
if (Input.GetButton ("Fire1"))
CancelInvoke("LaunchProjectile");
}
function LaunchProjectile ()
{
instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5;
}
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Rigidbody projectile;
void Update() {
if (Input.GetButton("Fire1"))
CancelInvoke("LaunchProjectile");
}
void LaunchProjectile() {
instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5;
}
void Example() {
InvokeRepeating("LaunchProjectile", 2, 0.3F);
}
}