湖边的小屋遗址

那唐僧内眼凡胎不识妖怪,反护着它……

Unity3d 中定时器的使用

来源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);
    }
}
阅读更多
文章标签: unity3d C# JavaScript
个人分类: Unity 3D C# Javascript
想对作者说点什么? 我来说一句

Unity定时器

Unity定时器

u011611902 u011611902

2016-02-27 21:51:50

阅读数:643

没有更多推荐了,返回首页

不良信息举报

Unity3d 中定时器的使用

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭