Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of coroutine problem in Unity

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of coroutine problems in Unity, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

1. On the surface, the execution order of the cooperative program

One of the simplest co-programming functions:

IEnumerator Test () {yield return new WaitForSeconds (1); Debug.Log ("End");} / / StartCoroutine elsewhere (Test ())

What it does: when you execute StartCoroutine (Test ()) elsewhere; after that, print "End" 1 second later.

When will Test be called after the collaboration process is started? The answer is actually to call every frame. (for iterator functions, it means to continue execution once per frame).

If every frame continues, why does Test finally continue to execute after a second? The answer is in WaitForSeconds.

The process goes like this:

1. In StartCoroutine (Test ()), Test is executed for the first time, and yield aborts and returns a "WaitForSeconds (1)" object.

2. When StartCoroutine is executed, the Test function is registered internally by Unity. Because it has to be called every frame after that, write it down internally in Unity.

3. Here, the returned WaitForSeconds (1) is actually a gimmick, and its design idea is:

If (Time.time is less than the previously agreed time) {return: not over;} return: over, continue. / / the pseudo code is used here. If you want to know the principle, please see the Unity official documentation below.

Remember: the co-program function is called once every frame. So in this second, the Unity engine layer calls the Test function every frame, repeating dozens of times, each time blocked by WaitForSeconds, can not be executed further.

5. Until a call, 1 second later, WaitForSeconds is finally released, Test has a chance to continue to execute, print "End".

This is to take WaitForSeconds as an example to explain the execution process of the collaboration program, and the principle of waiting for 1 second.

2. Explain yield return www

Now we can go back to the main question. In fact, yield return www and yield return new WaitForSeconds (1) are exactly the same. The design of WWW is:

If (network request is still in progress) {return: not finished;} return: over

In the same way, if the network request is not completed, then your co-program function will be blocked by www. This cleverly implements an asynchronous mechanism so that the whole game will not get stuck because of www.

3. How does Unity do this?

Eldereal's answer is very detailed, in fact, C # does not support real collaborators at all, but only iterators.

Because of the particularity of the game engine (updated every frame), Unity cleverly adapted the iterator into something that seemed to execute in parallel, and it really worked well.

Don't forget that WWW and WaitForSeconds are both classes provided by Unity and are not originally supported by C #. If you are curious and want to extend your own WWW-like class, refer to the official documentation to customize the Yield object:

CustomYieldInstruction

Https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html

Because Unity's "pseudo" collaboration belongs to its own scheme, and the design will even be modified when the engine is updated in the future, it is more important to understand the call order and general principles, and not to stick to the code itself.

4. Other instructions

Unity's Coroutine mechanism is well designed, but the name is so problematic that it is not suitable for Coroutine in English or in Chinese.

People who have some experience in golang, python gevent, and skynet development (mainly on the server side) have a good understanding of the collaborative process, but the collaborative process of Unity is too different from the real one. Giving the same name is very misleading and it is difficult to communicate.

In a word, explain the Unity protocol: in fact, Unity's Coroutine is more like "registering a function called every frame", except that this function supports yield abort, that's all.

Thank you for reading this article carefully. I hope the article "sample Analysis of coroutine problems in Unity" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report