unity学习——使用协程实现延时效果

来源:互联网 发布:淘宝文具海报1950px 编辑:程序博客网 时间:2024/06/16 02:46

上一篇中我们提到了使用WaitForSeconds方法来实现延时效果。现在我们来介绍使用WaitForFixedUpdate方法实现。
WaitForFixedUpdate方法也必须配合yield语句使用。从它的类名上就可以知道它的功能,暂停协程知道下一次FixedUpdate时才会继续执行协程,因此它的构造函数也十分简单,它不需要额外的参数。

using System.Collections;using System.Collections.Generic;using UnityEngine;public class WaitForFixedUpdateText : MonoBehaviour {    // Use this for initialization    void Start () {        StartCoroutine("Example");    }    // Update is called once per frame    void Update () {    }    IEnumerator Example()    {        while(true){            Debug.Log(Time.frameCount);//帧数            Debug.Log(Time.time);            yield return new WaitForFixedUpdate();        }    }}

使用WaitForFixedUpdate类暂停的时候取决于unity3D的编辑器中的TimeManager的FixedTimestep中的值。
这里写图片描述

还有一个处理延时的类——WaitForEndOfFrame类,我们常用它来处理等待帧结束再恢复执行的逻辑。它的作用是等到所有的摄像机和GUI被渲染完成后,再恢复协程的执行。

阅读全文
0 0