当前位置:网站首页>Unity update process_ Principle of unity synergy
Unity update process_ Principle of unity synergy
2022-07-04 15:07:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Unity The principle of CO process
Release time :2019-06-13 18:45,
A coroutine is not multithreaded , The coroutine is still in the main thread ( notes : stay Unity The Central African main thread is not accessible Unity Resources )
1、 Threads 、 The difference between process and coroutine
The process has its own independent heap and stack , Neither heap nor stack is shared , Processes are scheduled by the operating system
Threads have their own independent stacks and shared heaps , Shared heap does not share stack , Threads also have operating system scheduling ( Standard threads are like this )
Coroutines share heaps and not stacks like threads , The programmer displays the schedule in the code of the coroutine
An application generally corresponds to a process , A process usually has a main thread , There are also several worker threads , Threads are parallel , You can start a coroutine in a thread , Let the program run at a specific time .
The difference between a coroutine and a thread is : The process avoids meaningless scheduling , This can improve performance , But it is also so , Programmers have to take responsibility for scheduling themselves , meanwhile , Coroutines also lose the standard thread usage CPU The ability of .
2、Unity The principle of intermediate process execution
Put one on first unity Frame operation diagram of the main line :
stay Unity Runtime , Calling a coroutine is just starting a IEnumerator( iterator ), The process begins to run , In carrying out the yield
return There was no difference from other normal procedures , But when it comes to yield
return And I'll be back immediately , And suspend the function temporarily . In the next frame FixedUpdate perhaps Update After judgment yield return
Whether the following conditions are satisfied , If satisfied, execute downward .
according to unity We know the main frame diagram , Collaborative programs are mainly in update() After method ,lateUpdate() Call before method .
Unity The impact of the life cycle on the process :
By setting MonoBehaviour The script enabled There's no impact on Xiecheng , But if gameObject.SetActive(false)
Then the started coroutine stops completely , Even in Inspector hold gameObject
Activation is still not continuing . In other words, although Xiecheng is MonoBehvaviour Starting up (StartCoroutine) But the position of coprogram function is completely related to MonoBehaviour It's a hierarchical , Not subject to MonoBehaviour The state of influence , But follow MonoBehaviour Scripts are just as popular as gameObject
control , It should also be with MonoBehaviour Script like every frame “ polling ” yield Whether the conditions of .
notes :WaitForSends() suffer Time.timeScale influence , When Time.timeScale = 0f when ,yieldreturn new
WaitForSecond(X) Will not be satisfied .
3、 The main applications of coprocessing are
The coroutine can't just do some simple delays , If you simply pause for a few seconds and then execute, there is no need to start a thread .
The real function of coprocessing is to do some time-consuming things step by step , For example, loading resources in the game
using System.Collections; using System.Collections.Generic; using
System.Threading; using UnityEngine; public class Test : MonoBehaviour {
private int num = 0; void Start() { StartCoroutine(Test1()); } IEnumerator
Test1() { while (num < 30) { num++; Debug.Log(num); yield return null;
Debug.Log(“————-” + num); } } void Update() { Debug.Log(“update” +
num); } void LateUpdate() { Debug.Log(“lateUpdate!” + num); } }
The above example is very simple: loading 30 Objects , If it is loaded at one time, it will get stuck , Influence the game experience . This is how we can use the synergy , The co process is every frame LateUpdate Before execution yield
return Previous code ,LateUpdate After performing yield return
Later code ( You can see from the above small example ). In this way, we can load one per frame until the loading is completed , There will be no Caton .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/149432.html Link to the original text :https://javaforall.cn
边栏推荐
- LVGL 8.2 Line
- Flutter reports an error no mediaquery widget ancestor found
- Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
- LVGL 8.2 Draw label with gradient color
- I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
- 宽度精度
- go-zero微服务实战系列(九、极致优化秒杀性能)
- How to match chords
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- 开源人张亮的 17 年成长路线,热爱才能坚持
猜你喜欢
微博、虎牙挺进兴趣社区:同行不同路
leecode学习笔记-约瑟夫问题
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
Quick introduction to automatic control principle + understanding
TechSmith Camtasia studio 2022.0.2 screen recording software
Redis 發布和訂閱
How to build a technical team that will bring down the company?
从0到1建设智能灰度数据体系:以vivo游戏中心为例
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model
随机推荐
数据库函数的用法「建议收藏」
Optimization method of deep learning neural network
2022 financial products that can be invested
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
夜天之书 #53 Apache 开源社群的“石头汤”
为什么国产手机用户换下一部手机时,都选择了iPhone?
Redis 發布和訂閱
左右对齐!
Dialogue with ye Yanxiu, senior consultant of Longzhi and atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
找数字
开源人张亮的 17 年成长路线,热爱才能坚持
Preliminary exploration of flask: WSGI
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model
音视频技术开发周刊 | 252
小数,指数
Ffmpeg Visual Studio development (IV): audio decoding
Is BigDecimal safe to calculate the amount? Look at these five pits~~
宽度精度
PLC Analog input analog conversion FC s_ ITR (CoDeSys platform)