[1] initial knowledge of coroutine & quot; Unity 3d & quot;

Source: Internet
Author: User

Hey, U3D coroutine

During Game Development, we often encounter this situation. when specific conditions are met, some behaviors are triggered and a series of code is executed. You can use coroutine when you encounter such problems that require wake-up operations.

Features:

Conditional callback code.

Example:

Private void BeginCoroutine ()
{
Debug. Log ("coroutine before ");
StartCoroutine (DoSomethings ());
Debug. Log ("coroutine after ");
}


Private IEnumerator DoSomethings ()
{
Debug. Log ("doing ");
Yield return 0;
Debug. Log ("done ");
}

Running result:

Coroutine before

Doing

Coroutine after

Done

The example is very simple. From the running result, we can see that the DoSomethings function immediately executes the coroutine after code segment after running doing.

Brief Analysis of implementation process

1. Execute the function through StartCoroutine to start the coroutine

2. embed the yield block in the function. The block must be of the IEnumerator iterator return type.

Below, we often see several obfuscation points:

1. coroutine is executed on the main thread and is not blocked.

2. Once again, yield can only appear in the iterator Block


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.