This document explains the detail how jsbinding supports Unity coroutine in JavaScript.
First, I suggest you read the page to understand Coroutine scheduling:
Http://wiki.unity3d.com/index.php/CoroutineScheduler
and also Yield instructions in Mozilla SpiderMonkey engine:
Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield
OK, let ' s get started. Open This scene:
Assets/jsbinding/samples/coroutine/testcoroutine.unity
Part 1. Usage
This is source code of TestCoroutine.cs:
1[Jstype (JSMODE.CLR,".. /.. /.. /streamingassets/javascript/sharpkitgenerated/jsbinding/samples/coroutine/testcoroutine.javascript")]2 Public classTestcoroutine:monobehaviour {3 4 //Use this for initialization5 voidStart ()6 {7 Startcoroutine (Dotest ());8 }9 Ten //Update is called once per frame One voidUpdate () A { - - } the voidlateupdate () - { -Jsimp. Coroutine.updatemonobehaviourcoroutine ( This); - } + IEnumerator Waitforcangjingkong () - { + yield return Newwaitforseconds (2f); A } at IEnumerator dotest () - { - //test NULL -Debug.Log (1); - yield return NULL; - in //Test Waitforseconds -Debug.Log (2); to yield return NewWaitforseconds (1f); + - //Test WWW thewww www =NewWWW ("file://"+ Application.datapath +"/jsbinding/samples/coroutine/coroutinereadme.txt"); * yield returnwww; $Debug.Log ("Text from WWW:"+www.text);Panax Notoginseng - //Test another coroutine the yield returnStartcoroutine (Waitforcangjingkong ()); +Debug.Log ("Wait for Cangjingkong finished!"); A } the}
This is a JavaScript code compiled by Sharpkit:
1 if(typeof(jstypes) = ="undefined")2 varJstypes = [];3 varTestcoroutine = {4FullName"Testcoroutine",5Basetypename:"Unityengine.monobehaviour",6AssemblyName:"sharpkitproj",7Kind:"Class",8 definition: {9 ctor:function () {TenUnityEngine.MonoBehaviour.ctor.call ( This); One }, A start:function () { - This. startcoroutine$ $IEnumerator ( This. Dotest ()); - }, the update:function () { - }, - lateupdate:function () { -Jsimp. Coroutine.updatemonobehaviourcoroutine ( This); + }, - waitforcangjingkong:function () { + var$yield= []; A$yield. Push (NewUnityEngine.WaitForSeconds.ctor (2)); at return$yield; - }, - dotest:function () { - var$yield= []; -unityengine.debug.log$ $Object (1); -$yield. Push (NULL); inunityengine.debug.log$ $Object (2); -$yield. Push (NewUnityEngine.WaitForSeconds.ctor (1)); to varwww =Newunityengine.www.ctor$ $String ("file://"+ UnityEngine.Application.get_dataPath () +"/jsbinding/samples/coroutine/coroutinereadme.txt"); +$yield. push (WWW); -unityengine.debug.log$ $Object ("Text from WWW:"+Www.get_text ()); the$yield. Push ( This. startcoroutine$ $IEnumerator ( This. Waitforcangjingkong ())); *unityengine.debug.log$ $Object ("Wait for Cangjingkong finished!"); $ return$yield;Panax Notoginseng } - } the }; +Jstypes.push (Testcoroutine);
See "Dotest" and "Waitforcangjingkong" method, they is coroutine methods. Sharpkit translates C # yield to a $yield array, and every yield instruction are pushed into the that array.
But that is not JavaScript yield syntax. So, I made a menu to correct this: JSB | Correct JavaScript Yield Code
After excuting this menu, the 2 methods look like:
1waitforcangjingkong:function* (){2 yield(NewUnityEngine.WaitForSeconds.ctor (2));3 },4 5dotest:function* () {6unityengine.debug.log$ $Object (1);7 yield(NULL);8unityengine.debug.log$ $Object (2);9 yield(NewUnityEngine.WaitForSeconds.ctor (1));Ten varwww =Newunityengine.www.ctor$ $String ("file://"+ UnityEngine.Application.get_dataPath () +"/jsbinding/samples/coroutine/coroutinereadme.txt"); One yield(WWW); Aunityengine.debug.log$ $Object ("Text from WWW:"+Www.get_text ()); - yield( This. startcoroutine$ $IEnumerator ( This. Waitforcangjingkong ())); -unityengine.debug.log$ $Object ("Wait for Cangjingkong finished!"); the}
Notice what have been changed?
- Use "function*" instead of "function" to declare a coroutine function
- "$yield" Array has been deleted
- "$yield. Push" have been replaced with simple "yield"
Now the code matchs exactly JavaScript yield syntax, and are ready to run.
Part 2. Things inside
A C # Coroutine method is compiled by C # Comipler, it returns a IEnumerator when you call it.
This IEnumerator was then passed to "Startcoroutine" method. After that, it's Unity Coroutine scheduler who manages them and decides when to call "MoveNext".
In the case of JavaScript, it also have compiler to compile coroutine function, but C # Coroutine Scheduler is not
Jsbinding + sharpkit/supporting Coroutine