当前位置: 首页 > news >正文

网站建设各模块功能简述/网站站内关键词优化

网站建设各模块功能简述,网站站内关键词优化,如何做好一个外贸网站的编辑,电子邮箱怎么注册必读好文推荐: Unity协程(Coroutine)原理深入剖析 Unity协程(Coroutine)原理深入剖析再续 上面的文章说得太透彻,所以这里就记一下自己的学习笔记了。 首先要说明的是,协程并不是线程&#xff0…

必读好文推荐:

Unity协程(Coroutine)原理深入剖析

Unity协程(Coroutine)原理深入剖析再续

 

上面的文章说得太透彻,所以这里就记一下自己的学习笔记了。

首先要说明的是,协程并不是线程,协程是运行在主线程中的,是和主线程同步执行的代码,不同的地方是运行的方法可以被yield return在当前帧进行打断,到下一帧后可以继续从被打断的地方继续运行。

下面我们看一个示例,场景中有一个空的GameObject对象,其绑定了下面的脚本:

复制代码
 1 using UnityEngine;2 using System.Collections;3 4 public class Test : MonoBehaviour5 {6     int frame = 0;7 8     void Start ()9     {
10         this.StartCoroutine(CountDown());
11     }
12     
13     void Update ()
14     {
15         Debug.Log("Now is frame: " + (++frame));
16     }
17 
18     IEnumerator CountDown()
19     {
20         Debug.Log("step - 1");
21         yield return null;
22         Debug.Log("step - 2");
23         yield return null;
24         Debug.Log("step - 3");
25         yield return null;
26         Debug.Log("step - 4");
27     }
28 }
复制代码

下面是执行的结果:

下面我们看看运行的逻辑是如何的:

当进入Start方法时开始启动协程,这时候协程开始运行,输出“step1”后遇到第一个yield return后暂停本帧的运行,接下来进入Update方法输出“frame1”,由于协程调用是在Update之后,所以第二帧开始后,先执行了第二个Update输出“frame2”,然后从协程的上次暂停处继续执行,输出“step2”后遇到第二个yield return后暂停本帧的运行,如此反复,当输出“step4”后发现方法已经执行完毕,协程结束。

 

下面看看yield break的效果,这个语句会立即中断协程的运行,代码如下:

复制代码
 1 using UnityEngine;2 using System.Collections;3 4 public class Test : MonoBehaviour5 {6     int frame = 0;7 8     void Start ()9     {
10         this.StartCoroutine(CountDown());
11     }
12     
13     void Update ()
14     {
15         Debug.Log("Now is frame: " + (++frame));
16     }
17 
18     IEnumerator CountDown()
19     {
20         Debug.Log("step - 1");
21         yield return null;
22         Debug.Log("step - 2");
23         yield return null;
24         Debug.Log("step - 3");
25         yield break;
26         Debug.Log("step - 4");
27     }
28 }
复制代码

下面是运行的结果:

我们可以发现“step4”已经运行不到了。

 

yield的返回值,我们可以返回null或者数字0,效果是一致的,同时还可以返回3个对象,分别如下:

yield return new WaitForFixedUpdate();

·等待直到下一个固定帧速率更新函数。

yield return new WaitForEndOfFrame();

·等待直到所有的摄像机和GUI被渲染完成后,在该帧显示在屏幕之前。

yield return new WaitForSeconds(1);

·在给定的秒数内,暂停协同程序的执行。

 

下面我们来看一个例子,修改第一个例子的Test.cs:

复制代码
 1 using UnityEngine;2 using System.Collections;3 4 public class Test : MonoBehaviour5 {6     int frame1 = 0;7     int frame2 = 0;8     int frame3 = 0;9 
10     void Start ()
11     {
12         this.StartCoroutine(CountDown());
13         this.StartCoroutine(CountDown_WaitForFixedUpdate());
14         this.StartCoroutine(CountDown_WaitForEndOfFrame());
15         this.StartCoroutine(CountDown_WaitForSeconds());
16     }
17     
18     void Update ()
19     {
20         Debug.Log("Update is frame: " + (++frame1));
21     }
22 
23     void FixedUpdate ()
24     {
25         Debug.Log("FixedUpdate is frame: " + (++frame2));
26     }
27 
28     void LateUpdate ()
29     {
30         Debug.Log("LateUpdate is frame: " + (++frame3));
31     }
32 
33     IEnumerator CountDown()
34     {
35         Debug.Log("yield - step - 1");
36         yield return null;
37         Debug.Log("yield - step - 2");
38         yield return null;
39         Debug.Log("yield - step - 3");
40     }
41 
42     IEnumerator CountDown_WaitForFixedUpdate()
43     {
44         Debug.Log("yield WaitForFixedUpdate - step - 1");
45         yield return new WaitForFixedUpdate();
46         Debug.Log("yield WaitForFixedUpdate - step - 2");
47         yield return new WaitForFixedUpdate();
48         Debug.Log("yield WaitForFixedUpdate - step - 3");
49     }
50 
51     IEnumerator CountDown_WaitForEndOfFrame()
52     {
53         Debug.Log("yield WaitForEndOfFrame - step - 1");
54         yield return new WaitForEndOfFrame();
55         Debug.Log("yield WaitForEndOfFrame - step - 2");
56         yield return new WaitForEndOfFrame();
57         Debug.Log("yield WaitForEndOfFrame - step - 3");
58     }
59 
60     IEnumerator CountDown_WaitForSeconds()
61     {
62         Debug.Log("yield WaitForSeconds - step - 1");
63         yield return new WaitForSeconds(1 / 60 * 3);//大概是三帧的时间
64         Debug.Log("yield WaitForSeconds - step - 2");
65         yield return new WaitForSeconds(1 / 60 * 3);
66         Debug.Log("yield WaitForSeconds - step - 3");
67     }
68 }
复制代码

运行的结果如下,有点长,我就弄成两张图了:

 

通过输出我们可以得出下面的结果:

  1. 当帧数波动时,FixedUpdate会进行多次补帧处理,我们可以发现两张图之间FixedUpdate从3一直补帧到15;
  2. WaitForFixedUpdate表示协程是跟在FixedUpdate之后执行的;
  3. WaitForEndOfFrame表示协程是跟在LateUpdate之后执行的;
  4. WaitForSeconds额。。。不用多说了,你指定多久后执行就多久后执行,当然由于是基于帧运算的,所以可能会不准确;

 

最后补一张开头博客的运行顺序图:

转载于:https://www.cnblogs.com/lancidie/p/7346653.html

http://www.lbrq.cn/news/1295767.html

相关文章:

  • 非主营电子商务企业网站有哪些/东营百度推广公司
  • 网站建设合同标的/什么网站可以免费发广告
  • 30岁女人学网站开发可以吗/推荐友情链接
  • 树莓派做博客网站/最近10条重大新闻
  • wordpress备份和还原/seo是什么级别
  • 基于MVC模式的Web网站开发的优点/搜狗seo刷排名软件
  • 网站首页包括哪些内容/济南专业seo推广公司
  • 怎么验证网站备案密码是否正确/百度竞价的优势和劣势
  • 怎样做网站api接口/站长工具seo综合查询columbu cat
  • 如何制作一个单页网站/seo常规优化
  • 做网站都需要买什么/电商推广方案
  • 网站运营怎么自学/关键词seo排名优化
  • 党务网站建设依据公开/品牌宣传推广方案
  • 电商网站首页模板/合肥seo公司
  • asp做的网站频繁报错 参数错误/百度关键词推广2元一天
  • 项目负责人质量建设厅官方网站/网站推广哪家好
  • 专业建站商/源云推广
  • 本地拖拽网站建设/crm系统网站
  • 网站改版301设置/谷歌商店下载安装
  • 梅州网站优化/什么叫软文
  • 这么给网站做关键字/搜索引擎优化的技巧
  • 部队网站建设多少钱/短视频seo搜索优化
  • 什么网站代做毕业设计比较好/公司的网站
  • 一个空间可以做多个网站吗/武汉seo招聘信息
  • 个人做网站排版/在线建站模板
  • 做二手手机的网站有哪些/慧聪网seo页面优化
  • 制作网站的专业公司/网站seo源码
  • 网络营销策划有限公司/站长工具seo综合查询怎么关闭
  • 连云港权威网站建设价格/百度推广在哪里能看到
  • wp做图网站/杭州seo排名收费
  • 智慧场景:定制开发开源AI智能名片S2B2C商城小程序赋能零售新体验
  • 创建一个触发csrf的恶意html
  • MES系列 - MES是提升制造执行效率与透明度的关键系统
  • JavaScript平滑滚动与锚点偏移控制的完整指南
  • Spring AI 项目实战(十八):Spring Boot + AI + Vue3 + OSS + DashScope 实现高效语音识别系统(附完整源码)
  • 机械材料计算软件,快速核算重量