Yield return in C # vs. Coroutine in Unity (next)

Source: Internet
Author: User

Coroutine in Unity (co-process)

Estimated that people familiar with unity have seen or used Startcoroutine ()

Suppose we have a Ugui component in the scene, Image:

Bind the following code to the image

1 usingUnityengine;2 usingSystem.Collections;3 usingSystem.Threading;4 usingUnityengine.ui;5 6  Public classCoroutinedemo:monobehaviour {7 8     //Use this for initialization9     voidStart () {Ten      OneDebug.Log (Thread.CurrentThread.Name +": Start begin. fcount="+  +time.renderedframecount); A  -  -          This. Startcoroutine (Exeutecoroutine ()); the     } -      -     //Update is called once per frame -     voidUpdate () { +          -Debug.Log (Thread.CurrentThread.Name +": Update (): fcount="+time.renderedframecount); +     } A  at       -      PublicIEnumerator Exeutecoroutine () { -Debug.Log (Thread.CurrentThread.Name +": Before yield return A, fcount="+time.renderedframecount); -         //yield return new waitforseconds (1); -         yield return "A"; - //yield return "C"; in //yield return "D"; -Debug.logerror (Thread.CurrentThread.Name +": After yield return A, fcount="+time.renderedframecount); to     } +  - IEnumerator LoadImage () the     { *WWW www=NewWWW ("http://pic89.nipic.com/file/20160211/22571617_214730734684_2.jpg"); $Image img = This.gameobject.getcomponent<image> ();Panax Notoginseng  -Debug.Log ("before yield return:"+ Www.url +"Is it done ?"+ Www.isDone +", rf="+time.renderedframecount); the  +         yield returnwww; A  theDebug.Log ("After yield return,"+ Www.url +"Is it done ?"+ Www.isDone +", rf="+time.renderedframecount); +Rect Spriterect =NewRect (0,0, Www.texture.width, www.texture.height); -Sprite imagesprite = sprite.create (Www.texture, Spriterect,NewVector2 (0.5f,0.5f)); $Img.sprite =Imagesprite; $  -     } -}
View Code

Post-run log output:

Yield return in C # vs. Coroutine in Unity (next)

Related Article

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.