当前位置:网站首页>[Solved] Unity Coroutine coroutine is not executed effectively
[Solved] Unity Coroutine coroutine is not executed effectively
2022-08-05 03:06:00 【cat running wild outside the mountain】
Development platform: Unity
Programming platform: Visual Studio 2020 or above
Language used: C#
Problem description
When calling an object that is frequently activated/deactivated, the coroutine (Coroutine) mounted by the object will only be executed when it enters Runtime mode for the first time.Subsequent deactivation and reactivation operations cause the content of the coroutine to not run correctly.
public RectMask2D Mask2D;public Vector4 Forward;public void Start(){StartCoroutine(DOLineMove);}public IEnumerator DOLineMove(){yield return Mask2D.padding -= Forward;StartCoroutinue(DOLineMove());}
Problem Analysis
The premise of running the coroutine is that the mounted GameObject object remains in the Active state.Note: The mounted GameObject object of the coroutine must remain in the Active state during execution or self-iteration).In the Unity life cycle, the initialization phase is Awake , OnEnable , and Start three cycle phases.Roughly as follows:
- The Awake stage is used to capture Component information on the script mount object.
- OnEnable phase is used for event listener registration behavior.
- The Start phase executes the entry procedure.
When Editor first enters Runtime mode, Initialization phase methods will be executed sequentially.So no matter at which stage the StartCoroutine(string methodName)
is called, the IEnumerator
type is effectively called.The problem is evident at the following levels:
- The internal call
StopCoroutine(string methodName)
orStopAllCoroutine()
is not standardized.
These two methods disable coroutine objects enabled within the script.One disables only specific objects, the other disables all objects within the script.Irregular use logic is easy to cause this problem. - The script object or the script object itself is automatically disabled and restarted by programs of unknown origin, causing the coroutine to run abnormally.
For example: Awake phase should execute coroutine methods correctly.But in the same Awake cycle stage, another script modified (·SetActive()
orenable
) state caused.In this case, the coroutine will not function properly.Combined with the execution cycle of the Unity program in Runtime mode, the next time it is enabled, it will be performed directly from theOnEnable
stage.Of course, frequent disabling and re-enabling can also be problematic.(This must be a bug written by someone)
Solution: Troubleshoot Who is tampering with (or frequently) the state of the script?
Troubleshooting is often a troublesome thing, which is closely related to the amount of code of the project.It can be resolved quickly, or it can take a few hours, or even a week.Perhaps it was the framework that was not considered problematic at first, and the hidden problems were gradually exposed in the subsequent development.The best way is to follow the design of Loading into the scene on demand, rather than keeping it in the scene, activating, deactivating.If it is too late to save, then the next is the only possible effective method.
1) Use the Quick Find function to check one by one
Visual Studio provides editing for Quick Find .Usually this function is used to query keywords.Of course, it also plays a role in the investigation.Because it is related to disabling and enabling, search for the content associated in SetAtive()
/ enable
in the code, and confirm the troubleshooting one by one.(Time-consuming is inevitable)
2) Use Browse Definitions to quickly locate references
Add anywhere in the program executablethis.gameObject.SetActive(false)
or this.enable(true)
, right-click the keyword SetActive or enable and select View AllQuote.Visual Studio will give all quoted statements that use this keyword.It is convenient and quick to check.Notice!
- Be sure to open all assemblies for this project when browsing for definitions.rather than individuals.
- In some cases, developers use self-encapsulated
GameObjectUtil.Disable(GameObject obj)
and other methods.In this case, it is also necessary to check the object and location information on which the method is called and check one by one.
边栏推荐
- PostgreSQL数据库 用navicat 打开表结构的时候报错 cannot update secondarysnapshot during a parallel operation 怎么解决?
- QStyle platform style
- Distributed systems revisited: there will never be a perfect consistency scheme...
- 软链接引发的物理备份问题
- Syntax basics (variables, input and output, expressions and sequential statements)
- 思考(八十八):使用 protobuf 自定义选项,做数据多版本管理
- OpenGL 工作原理
- 【滤波跟踪】基于matlab无迹卡尔曼滤波惯性导航+DVL组合导航【含Matlab源码 2019期】
- leetcode - symmetric binary tree
- Why did they choose to fall in love with AI?
猜你喜欢
.NET应用程序--Helloworld(C#)
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
用CH341A烧录外挂Flash (W25Q16JV)
2022-08-04 The sixth group, hidden from spring, study notes
你要的七夕文案,已为您整理好!
Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
shell statement to modify txt file or sh file
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
Regular expression to match a certain string in the middle
毕设-基于SSM房屋租赁管理系统
随机推荐
The linear table lookup
The design idea of DMicro, the Go microservice development framework
In 2022, you still can't "low code"?Data science can also play with Low-Code!
PostgreSQL数据库 用navicat 打开表结构的时候报错 cannot update secondarysnapshot during a parallel operation 怎么解决?
[Fortune-telling-60]: "The Soldier, the Tricky Way"-2-Interpretation of Sun Tzu's Art of War
Principle and Technology of Virtual Memory
[In-depth study of 4G/5G/6G topic-51]: URLLC-16-"3GPP URLLC related protocols, specifications, and technical principles in-depth interpretation"-11-High reliability technology-2-Link adaptive enhancem
Matlab drawing 3
Ant Sword Advanced Module Development
QT MV\MVC structure
Cybersecurity and the Metaverse: Identifying Weak Links
2022-08-04: Input: deduplicated array arr, the numbers in it only contain 0~9.limit, a number.Return: The maximum number that can be spelled out with arr if the requirement is smaller than limit.from
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
Details such as compiling pretreatment
[Storage] Dawning Storage DS800-G35 ISCSI maps each LUN to the server
Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
The 22-07-31 weeks summary
(十一)元类
Review 51 MCU
Native js realizes the effect of selecting and canceling all the multi-select boxes