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)