unity3d定时器

来源:互联网 发布:python hdfs 容量 编辑:程序博客网 时间:2024/06/07 21:11

1、利用Update();适合重复调用

public float timer = 1.0f;

// Update is called once per frame
void Update() {
    timer -= Time.deltaTime;
    if (timer <= 0) {
        timer = 1.0f;
    }
}

2、利用协程;
void Start() {
    StartCoroutine(Timer());
}


IEnumerator Timer() {
    while (true) {
        yield return new WaitForSeconds(1.0f);
    }

}


3、利用Invoke()

// Use this for initialization
void Start() {
    Invoke("Timer", 1.0f);
}
void Timer() {

}


4、利用InvokeRepeating();

    void Timer() {  
    }  
    public void Awake() {  
        InvokeRepeating("Timer", 1, 2f);  //1秒后,每2f调用一次  
}
0 0
原创粉丝点击