Unity3d 中定时器的使用

10 篇文章 0 订阅
9 篇文章 0 订阅

来源http://wiki.ceeger.com/start?do=search&id=Invoke

  • 延时是个很常用的功能
    n 秒后执行 一个函数,每隔 n 秒重复执行 一个函数。

  • 反悔了,想取消它?
    可以统统取消,也可以只取消具体的某一个延时执行的设定。

MonoBehaviour.Invoke 延迟调用

JavaScript ⇒ Invoke(methodName: string, time: float): void;
C# ⇒ void Invoke(string methodName, float time);

Description 描述

Invokes the method methodName in time seconds.

在time秒后,延迟调用方法methodName。

JavaScript:

// Launches a projectile in 2 seconds

var projectile : Rigidbody;

Invoke("LaunchProjectile", 2);

function LaunchProjectile () 
{ 
    var instance : Rigidbody = Instantiate(projectile); 
    instance.velocity = Random.insideUnitSphere * 5; 
} 

C#:

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);
    }
}

MonoBehaviour.InvokeRepeating 重复延迟调用

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秒重复调用。

JavaScript:

// 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; 
} 

C#:

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);
    }
}

MonoBehaviour.CancelInvoke 取消延迟调用

JavaScript ⇒ CancelInvoke(): void;
C# ⇒ void CancelInvoke();

Description 描述

Cancels all Invoke calls on this MonoBehaviour.

在当前MonoBehaviour,取消所有Invoke调用

JavaScript:

// 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; 
} 

C#:

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调用。

JavaScript:

// 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; 
} 

C#:

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);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值