当前位置:网站首页>协程
协程
2022-06-23 12:02:00 【大老婆灰原哀】
老师定义:在主线程运行时同时开启另一段逻辑处理,来协助当前程序的执行。
官方定义:协程是一个分部执行,遇到条件(yield return 语句)会挂起,直到条件满足才会被唤醒继续执行后面的代码。
注意的点:
1.Unity在每一帧(Frame)都会去处理对象上的协程。Unity主要是在Update后去处理协程(检查协程的条件是否满足),但也有写特例:
2.协程跟Update()其实一样的,都是Unity每帧对会去处理的函数(如果有的话)。如果MonoBehaviour 是处于激活(active)状态的而且yield的条件满足,
就会执行协程方法的后面代码。还可以发现:如果在一个对象的前期调用协程,协程会立即运行到第一个 yield return 语句处,如果是 yield return null ,就会在同一帧再次被唤醒。
3.Unity的C#代码中一个协程的返回值通常是IEnumerator(迭代器)类型,IEnumerator接口有两个方法,分别是Current和MoveNext。我们可以简单的认为:
协程就是一个返回迭代器的函数,一开始迭代器的Current指向函数的开头,每执行一次MoveNext,Current就指向下一个yield return返回的值。迭代器方法运行到 yield return
语句时,会返回一个expression表达式并保留当前在代码中的位置。 当下次调用迭代器函数时执行从该位置重新启动。
Unity在每帧做的工作就是:调用 协程(迭代器)MoveNext() 方法,如果返回 true ,就从当前位置继续往下执行。
边栏推荐
- Oracle数据库的主导地位被云竞争对手逐渐侵蚀
- 华为云GaussDB重磅发布HTAP商用,定义云原生数据库2.0新范式
- 想学习eTS开发?教你开发一款IQ-EQ测试应用
- 09 -- 回文对
- Halcon principle: correlation matching
- Learning notes sweep crawler framework
- Easy to understand soft route brushing tutorial
- Common fault analysis and Countermeasures of MySQL in go language
- 理财产品的赎回时间有规定吗?
- 国产化信息 | 爱可生与中科方德完成产品兼容互认证
猜你喜欢

"Dream of children's travel" in 2022, GAC Honda children's road safety charity travel entered the Northeast

Getting started with redis - Chapter 2 - data structures and objects - linked lists

Halcon原理:auto_threshold算子

【零基础微信小程序】基于百度大脑人像分割的证件照换底色小程序实战开发

Huawei cloud gaussdb heavily released HTAP for commercial use, defining a new paradigm of cloud native database 2.0

蓝桥杯单片机(一)——关闭外设及熄灭LED

『忘了再学』Shell流程控制 — 39、特殊流程控制语句

年薪中位数超30万,南大AI专业首届毕业生薪资曝光

Blue Bridge Cup single chip microcomputer (I) -- turn off peripherals and turn off led

QT5知识:信号和槽的一些要点
随机推荐
How to uninstall and reinstall gazebo
Where is the safest and most formal way to open an account at present?
电脑坏了,换了台电脑,装node环境的时候出了问题,报错URL not defined
ROS知识:librviz库的调用实践
Huawei cloud gaussdb heavily released HTAP for commercial use, defining a new paradigm of cloud native database 2.0
机器学习系列5:距离空间(1)
Easy to understand soft route brushing tutorial
10-- construct binary tree according to middle order traversal and post order traversal
【UVM入门 ===> Episode_7 】~ sequence、sequence item、sequencer、driver
[basic knowledge] - data bit width converter
Introduction to redis - Chapter 3 - data structures and objects - Dictionary
HMS Core 视频编辑服务开放模板能力,助力用户一键Get同款酷炫视频
@黑马粉丝,这份「高温补贴」你还没领?
The computer broke down. I changed the computer. There was a problem installing the node environment. The URL is not defined
Redis 入门-第二篇-数据结构与对象-链表
Qt知识:视图框架QGraphicsWidget详解
DuPont analysis: what is the investment value of Anyang Iron and Steel Co., Ltd?
I am in Foshan. Where can I open an account? Is it safe to open a mobile account?
Analysis of six dimensional chart: analysis of enterprise growth of CSCEC
得物多活架构设计之路由服务设计