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

  • 相关阅读:
    关于课内外读物的建议
    c# Aes加解密
    web api 如何通过接收文件流的方式,接收客户端及前端上传的文件
    c# 文件夹权限
    mysql 8创建远程访问用户以及连接mysql速度慢的解决方法
    为什么读书?读书让我们明心见性!
    大部分教程不会告诉你的 12 个 JS 技巧
    nuget包管理nuget服务器发布包时出现请求报错 406 (Not Acceptable)
    Python 实现毫秒级淘宝、京东、天猫等秒杀抢购脚本
    eos的资源和工具列表
  • 原文地址:https://www.cnblogs.com/hont/p/6477384.html
Copyright © 2011-2022 走看看