Unity's own implementation of the co-process

Source: Internet
Author: User

There are several benefits to implementing your own process:

    1. Independence from unity and can be used anywhere else.
    2. The non-main thread can also start the co-process and execute it on the main thread, such as asynchronous network messages.
    3. You can give each process an ID, start or close a particular association at any time through an ID, or a non-Monobehavior object can manage its own process.

In Unity, Coroutine is executed in Lateupdate, and each update executes a portion of the code, IEnumerator, which is MoveNext every time.

The IEnumerator has three interfaces:

    • Current: Returns an object that can be set to a state.
    • MoveNext: Returns true to indicate that there is no end, and returns false to indicate that the enumeration has completed.
    • Reset: Restores the state and enumerates from the beginning.

If you write a return IEnumerator method, each time will be MoveNext to a yield, encounter yield Return,movenext will return the True,current value is the return of the object (yield return NULL when current=null, yield return obj current=obj), encountering yield Break,movenext returns false, indicating that the execution is complete. But if call Ienumerator.movenext directly, will not go to the inside of the other MoveNext, next jump.

Based on the above, you can write a method for IEnumerator to MoveNext to achieve the core code of the self-control process:

 Public Static BOOLMoveNext (IEnumerator subTask) {varChild =subtask.current; //yield return another process: recursive MoveNext    if(Child! =NULL&& Child isIEnumerator && MoveNext (child asIEnumerator)) return true; #ifUNITY//yield return www: wait for www to complete    if(Child isUnityengine.www &&! (Child asunityengine.www). IsDone)return true; #endif    if(Subtask.movenext ())return true; return false;}

The management process is also very simple, with a linked list to manage:

linkedlistnode<ienumerator> node = M_tasklist.first; LinkedListNode<IEnumerator> Tempnode = node;  while NULL ) {                  if (!  MoveNext (node. Value)) {
= node; = node. Next; // Post -Processing of deleted nodes can be written here M_tasklist.remove (tempnode); Else { = node. Next; }
}

When you write the IEnumerator method, return to the other process does not write yield return Startcoroutine (func ()), directly write yield return func () can be, such as:

IEnumerator A () {...} IEnumerator B () {... yield return  A (); ...}

You can also write some useful IEnumerator classes, such as the waitforevent used in our project, as well as combination classes such as Waitforall,waitforany.

Unity's own implementation of the co-process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.