zoukankan      html  css  js  c++  java
  • Unity中yield return null和yield return WaitForEndOfFrame的区别

    2017/07/04修改 - 对WaitForEndOfFrame的LateUpdate时序进行说明。

    测试结论:

    1.如果只是等待下一帧执行,用yield return null即可。调用顺序在Update后,LateUpdate前

    2.如果有截屏需要,用WaitForEndOfFrame。具体参考官方例子。否则直接用Texture2D.ReadPixel抓取屏幕信息则会报错。

    3.此外,用WaitForEndOfFrame还可以让代码在LateUpdate的时序后调用。

    测试1:

    using UnityEngine;
    using System.Collections;
    using System.IO;
    
    public class Test1 : MonoBehaviour
    {
        void OnEnable()
        {
            StartCoroutine(ReturnNullTest());
            StartCoroutine(ReturnWaitForEndOfFrame());
        }
    
        IEnumerator ReturnNullTest()
        {
            Debug.Log("[1] ReturnNull Frame Count: " + Time.frameCount + "Render Frame Count: " + Time.renderedFrameCount);
            yield return null;
            Debug.Log("[2] ReturnNull Frame Count: " + Time.frameCount + "Render Frame Count: " + Time.renderedFrameCount);
        }
    
        IEnumerator ReturnWaitForEndOfFrame()
        {
            Debug.Log("[1] WaitForEndOfFrame Frame Count: " + Time.frameCount + "Render Frame Count: " + Time.renderedFrameCount);
            yield return new WaitForEndOfFrame();
            Debug.Log("[2] WaitForEndOfFrame Frame Count: " + Time.frameCount + "Render Frame Count: " + Time.renderedFrameCount);
        }
    }

    从测试顺序来看,两者都可以达到下一帧执行的目的

    但WaitForEndOfFrame的渲染帧会多跳一帧

    测试2:

    先看一下官方的执行顺序表

    WaitForEndOfFrame会在一帧结束后调用,且在LateUpdate之后调用。

    而正常yield return null的调用是在update之后,也就是说可以分别做到不同时序的调用。

    测试脚本如下:

    public class Test : MonoBehaviour
    {
        void Awake()
        {
            Debug.Log("0");
            StartCoroutine(TestCoroutine());
            Debug.Log("2");
        }
    
        void Update()
        {
            Debug.Log("3 - Update");
        }
    
        void LateUpdate()
        {
            Debug.Log("4 - LateUpdate");
        }
    
        IEnumerator TestCoroutine()
        {
            Debug.Log("1");
            yield return new WaitForEndOfFrame();
            Debug.Log("5");
        }
    }

    结果:

    不过对于静态置于场景中调用时,会多经过一次Update和LateUpdate

    起初以为是第0帧问题,但后来放在Start中执行依旧会多经过一次。

    但动态加载的prefab则没有该问题。

  • 相关阅读:
    jQuery on注册事件
    前端表格(Table)多条数据可以增加行删除行json封装后Post数据到后台处理
    导出Excel数据
    C#在一个实体类上新加字段并把另外一个实体类的字段值赋给它
    函数(五)-内置函数
    函数(四)-命名空间与作用域
    函数(三)-return与函数的调用
    函数(二)- 参数
    函数(一)-基本格式
    字符串格式化
  • 原文地址:https://www.cnblogs.com/hont/p/6477384.html
Copyright © 2011-2022 走看看