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("");
StartCoroutine(TestCoroutine());
Debug.Log("");
} void Update()
{
Debug.Log("3 - Update");
} void LateUpdate()
{
Debug.Log("4 - LateUpdate");
} IEnumerator TestCoroutine()
{
Debug.Log("");
yield return new WaitForEndOfFrame();
Debug.Log("");
}
}
结果:
不过对于静态置于场景中调用时,会多经过一次Update和LateUpdate
起初以为是第0帧问题,但后来放在Start中执行依旧会多经过一次。
但动态加载的prefab则没有该问题。
Unity中yield return null和yield return WaitForEndOfFrame的区别的更多相关文章
- Unity 中的协同程序
今天咱就说说,协同程序coroutine.(这文章是在网吧敲的,没有unity,但是所有结论都被跑过,不管你信得过我还是信不过我,都要自己跑一下看看,同时欢迎纠错)先说说啥是协程:协同程序是一个非常让 ...
- C#中的yield return与Unity中的Coroutine(协程)(下)
Unity中的Coroutine(协程) 估计熟悉Unity的人看过或者用过StartCoroutine() 假设我们在场景中有一个UGUI组件, Image: 将以下代码绑定到Image using ...
- yield学习续:yield return迭代块在Unity3D中的应用——协程
必读好文推荐: Unity协程(Coroutine)原理深入剖析 Unity协程(Coroutine)原理深入剖析再续 上面的文章说得太透彻,所以这里就记一下自己的学习笔记了. 首先要说明的是,协程并 ...
- java中,return和return null有什么区别吗?
java中,return和return null有什么区别吗? 最大的区别:return;方法的返回值必须是void!return null;方法的返回值必须不是 原始数据类型(封装类除过)和void ...
- 在Java中,return null 是否安全, 为什么?
Java代码中return value 为null 是不是在任何情况下都可以,为什么不会throw NullPointerException? Java语言层面:null值自身是不会引起任何问题的.它 ...
- 【Unity】协程Coroutine及Yield常见用法
最近学习协程Coroutine,参考了别人的文章和视频教程,感觉协程用法还是相当灵活巧妙的,在此简单总结,方便自己以后回顾.Yield关键字的语意可以理解为“暂停”. 首先是yield return的 ...
- java多线程中wait/notify/sleep/join/yield方法以及多线程的六种状态
刚开始学线程的时候也是被这几个方法搞的云里雾里的,尤其是一开始看的毕老师的视频,老师一直在强调执行权和执行资格,看的有点懵逼,当然不是说毕老师讲的不好,就是自己有点没听明白,后来复习看了一些其他的博客 ...
- 你写的return null正确吗?
上次一篇“你写的try…catch真的有必要吗”引起了很多朋友的讨论.本次我在code review又发现了一个问题,那就是有人有意无意的写出了return null这样的代码,例如: public ...
- delete attempted to return null from a method with a primitive return type (int)
今天被自己给蠢死了 今天在代码中遇到这个错误, 百度翻译一下:映射方法,从一org.system.mapper.child.chmorganizationexaminationmapper.delet ...
随机推荐
- jquery .live() .delegate() .bind() .click()区别
什么是.live()? 除了让你对Dom元素现在和将来绑定事件之外,.live() 方法和.bind()方法很像.你可以用.live()方法对没有存在的Dom节点绑定事件.考虑下面的情况. 当用户要离 ...
- jenkins权限配置不对导致jenkins无法登陆
提醒:每次更改权限时,要将config.xml备份一下. 在打开jenkins后,没有创建用户前,先不要勾选系统设置中启用安全选项,如果勾选了,就会出现无法进入jenkins的现象. 如果已经勾选可以 ...
- Vacuum tube 真空管/电子管
真空管/电子管的发明 1904, John Ambrose Fleming invented the two-electrode vacuum-tube rectifier, which he cal ...
- windows下卸载mysql5.5,升级为mysql5.7.25
0. 停止mysql的服务 1. 卸载mysql5.5 1.1 使用360或者控制面板卸载mysql,为求干净,我使用的360,结果也需要手动清理 1.2 卸载完成之后到mysql的安装目录删掉该目录 ...
- OnPreRender事件常见用法
protected override void OnPreRender(EventArgs e) 1) 加入脚本 protected override void OnPreRender(EventAr ...
- ios中修改数字键盘
自定义文本框: #import <UIKit/UIKit.h> //自定义键盘的键定义 @interface DIYKey : NSObject { } @property(copy, n ...
- [转]GFS架构分析
Google文件系统(Google File System,GFS)是构建在廉价的服务器之上的大型分布式系统.它将服务器故障视为正常现象,通过软件的方式自动容错,在保证系统可靠性和可用性的同时,大大减 ...
- Alamofire使用报错Extra argument 'method' in call解决办法
使用Alamofire的时候,在用这句的时候报错了: Extra argument 'method' in call Alamofire.request("", method: H ...
- 严重: The web application [] registered the JDBC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDB
idea项目启动报如下错误, 网上的方法都试了都没用, 一直没解决, 干掉项目, 重新从svn检出就好了...坑 啊 Root WebApplicationContext: initializatio ...
- 【DeepLearning】Exercise:Softmax Regression
Exercise:Softmax Regression 习题的链接:Exercise:Softmax Regression softmaxCost.m function [cost, grad] = ...