• 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则没有该问题。

  • 相关阅读:
    WordPress fonts.useso.com加载慢解决办法
    ecshop 需要修改权限的文件夹及文件
    手机前端框架UI库(Frozen UI、WeUI、SUI Mobile)
    apache2配置rewrite_module
    wordpress htaccess伪静态设置
    linux whereis 快速查找命令
    Linux下的PS和grep的使用
    [转]group by 后使用 rollup 子句总结
    Maven配置国内镜像仓库
    清除电脑垃圾.bat
  • 原文地址:https://www.cnblogs.com/hont/p/6477384.html
Copyright © 2020-2023  润新知