Co-process
Understanding: The process is not a thread, nor does it execute asynchronously (knowing the line).
1. The monobehaviour and the update function are also executed in mainthread (it is important to understand the meaning of this statement).
void Start () { startcoroutine (Hellocoroutine ());} void Update () { Debug.Log ("Update ..."); void Lateupdate () { Debug.Log ("Lateupdate ..."); IEnumerator Hellocoroutine () { while (true) { Debug.Log ("Coroutine ..."); yield return null; } }
Compare the above code with two sheets. This way of writing is like an advanced update. At least it should be seen that this process of writing can complete the function of update.
2. Not the same place as the update.
IEnumerator Count () { int seconds = 0; while (true) {for (float timer = 0; timer < 2; timer + = Time.deltatime ) yield return 0; seconds++; Debug.Log (seconds + "seconds have passed since the coroutine started."); } }
3.yield
yiled return null equals yield return 0
My understanding here is to stop the executing method and start execution from the next frame (typically 0.02 seconds, as is the case with each frame of the update, depending on the timer set by unity).
4. The co-process is capable of passing parameters.
5. The co-process can also be nested.
IEnumerator hellocoroutinue () { Debug.Log ("Start----"); Yield return Startcoroutine (Wait (0.2f)); Yield return new waitforseconds (0.2f); The result is the same. Debug.Log ("End----");} IEnumerator Wait (float s) {for (float timer =0;timer< s;timer+=time.deltatime) { Debug.Log (" Current Timer "+ timer. ToString ()); Yield return 0; yield return null; } Debug.Log ("Wait ...");
Fancy draw line time difference, again verified with update like. The time to pause is the same.
You can see the method that paused the current method to execute yield return.
Additional notes:
A. Multiple processes can be run concurrently, and they will be updated according to their respective boot order;
B. If you want multiple scripts to access a co-process, you can define it as a static co-process;
(this blog is a reference to a blog on the web, simplifying itself, adding a little bit of their own understanding)
Talking about the Unity3d process