Use of startcoroutine

Source: Internet
Author: User

Startcoroutine is called coroutine in the help of unity3d, which means to start an auxiliary thread.

There is a thread in C #, but some elements in unity cannot be operated. The coroutine can be used in this case.

The advantage of using a thread is that there will be no interface failure. If there is a large number of operations, the thread will be suspended if it is useless.

Here is a simple example to illustrate the benefits of using coroutine:

 

 

[CSHARP]View plaincopy
  1. Void ongui ()
  2. {
  3. Gui. Label (New rect (0, 0,200, 50), "Test 1:" + result );
  4. If (GUI. Button (New rect (0,100,100, 50), "enable coroutine "))
  5. {
  6. Startcoroutine (getresult ());
  7. }
  8. Gui. Label (New rect (200, 0,200, 50), "Test 2:" + result1 );
  9. If (GUI. Button (New rect (200,100,100, 50), "No coroutine test "))
  10. {
  11. Getresult1 ();
  12. }
  13. }

The above Code defines two labels and buttons in the GUI. One button starts the coroutine calculation and the other directly calculates the result. Because the two methods both calculate the same result and the calculation amount is large, a temporary card death occurs during direct calculation.

 

[CSHARP]View plaincopy
  1. Float result;
  2. Ienumerator getresult ()
  3. {
  4. For (INT I = 0; I <1000; I ++)
  5. {
  6. For (Int J = 0; j <100000; j ++)
  7. {
  8. Result + = (I + J );
  9. }
  10. If (I % 100 = 0)
  11. Yield return 1;
  12. }
  13. }

This method is used to write coroutine. in C #, The coroutine must be defined as ienumerator, which is not required in JavaScript.

Yield return 1; this statement indicates that the result of one frame is returned. When I is an integer of 100, a result is returned once. This prevents a large number of computations from getting stuck.

 

 

[CSHARP]View plaincopy
  1. Float result1;
  2. Void getresult1 ()
  3. {
  4. For (INT I = 0; I <1000; I ++)
  5. {
  6. For (Int J = 0; j <100000; j ++)
  7. {
  8. Result1 + = (I + J );
  9. }
  10. }
  11. }

 

 

This method directly calculates the result. Because of the large amount of computing, the interface will be stuck, which will reflect the benefits of using coroutine.
When you use ienumerator, you must use yield return to return results. If the parameter is a number, it indicates the number of frames.
For example, yield return 1 indicates that each frame returns a result.

Use of startcoroutine

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.