【 unity3d 】使用协同程序、yield写计时器

需要使用协同程序及yield

通过开启协同程序,
1. 写一个while死循环,里面加上累加一帧时间间隔
2. 里面写一个if条件来跳出循环
3. 和一个等待一帧的停止协程,来确保是一帧的时间间隔

using UnityEngine;
using System.Collections;

public class CoroutineTest : MonoBehaviour {

    void Start(){
        StartCoroutine (Func ());
    }

    IEnumerator Func(){
        float f = 0.0f;
        while (true) {
            f += Time.deltaTime;//一帧的时间间隔
            if (f > 3.0f) {
                Debug.Log ("计时时间到");
                yield break;//跳出协程程序 类似于Continue
            }
            Debug.Log (f);
            yield return new WaitForEndOfFrame ();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/liaoshengg/article/details/80987812