首先得深入了解协程的原理。如果还没有完全理解,建议看这篇:

http://wiki.unity3d.com/index.php/CoroutineScheduler

另外还要对 JavaScript 的 yield 有所了解,可以看 Mozilla 这篇文档:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield

---------------------------------------------------------------------------

以下这一段过时,新版本已经没有这个问题,请看其他文章介绍

先说结论吧:用C#写的协程转换成 JavaScript 后,无法正常工作,必须要手动修改一点点代码。

源代码中的协程例子工程:Assets/JSBinding/Samples/Coroutine/TestCoroutine.unity

以下是 TestCoroutine.cs 代码:

 [JsType(JsMode.Clr,"../../../StreamingAssets/JavaScript/SharpKitGenerated/JSBinding/Samples/Coroutine/TestCoroutine.javascript")]
public class TestCoroutine : MonoBehaviour { // Use this for initialization
void Start ()
{
StartCoroutine(DoTest());
} // Update is called once per frame
void Update ()
{ }
void LateUpdate()
{
jsimp.Coroutine.UpdateMonoBehaviourCoroutine(this);
}
IEnumerator WaitForCangJingKong()
{
yield return new WaitForSeconds(2f);
}
IEnumerator DoTest()
{
// test null
Debug.Log();
yield return null; // test WaitForSeconds
Debug.Log();
yield return new WaitForSeconds(1f); // test WWW
WWW www = new WWW("file://" + Application.dataPath + "/JSBinding/Samples/Coroutine/CoroutineReadme.txt");
yield return www;
Debug.Log("Text from WWW: " + www.text); // test another coroutine
yield return StartCoroutine(WaitForCangJingKong());
Debug.Log("Wait for CangJingKong finished!");
}
}

这是 SharpKit 编译后的代码:

 if (typeof(JsTypes) == "undefined")
var JsTypes = [];
var TestCoroutine = {
fullname: "TestCoroutine",
baseTypeName: "UnityEngine.MonoBehaviour",
assemblyName: "SharpKitProj",
Kind: "Class",
definition: {
ctor: function (){
UnityEngine.MonoBehaviour.ctor.call(this);
},
Start: function (){
this.StartCoroutine$$IEnumerator(this.DoTest());
},
Update: function (){
},
LateUpdate: function (){
jsimp.Coroutine.UpdateMonoBehaviourCoroutine(this);
},
WaitForCangJingKong: function (){
var $yield = [];
$yield.push(new UnityEngine.WaitForSeconds.ctor());
return $yield;
},
DoTest: function (){
var $yield = [];
UnityEngine.Debug.Log$$Object();
$yield.push(null);
UnityEngine.Debug.Log$$Object();
$yield.push(new UnityEngine.WaitForSeconds.ctor());
var www = new UnityEngine.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());
$yield.push(this.StartCoroutine$$IEnumerator(this.WaitForCangJingKong()));
UnityEngine.Debug.Log$$Object("Wait for CangJingKong finished!");
return $yield;
}
}
};
JsTypes.push(TestCoroutine);

注意看 DoTest 函数和 WaitForCangJingKong 函数,他们都是协程函数。SharpKit 对其中的 yield 代码翻译成一个 $yield 数组,每一个 yield 指令都加到 $yield 数组中。

这样使得我们无法与 JavaScript 的 yield 对接。这就是为什么协程编译成 JavaScript 代码后无法直接使用的原因。

目前,需要做点小修改就可以运行了,以下是修改过的 JavaScript 文件:

 if (typeof(JsTypes) == "undefined")
var JsTypes = [];
var TestCoroutine = {
fullname: "TestCoroutine",
baseTypeName: "UnityEngine.MonoBehaviour",
assemblyName: "SharpKitProj",
Kind: "Class",
definition: {
ctor: function (){
UnityEngine.MonoBehaviour.ctor.call(this);
},
Start: function (){
this.StartCoroutine$$IEnumerator(this.DoTest());
},
Update: function (){
},
LateUpdate: function (){
jsimp.Coroutine.UpdateMonoBehaviourCoroutine(this);
},
WaitForCangJingKong: function* (){ yield (new UnityEngine.WaitForSeconds.ctor()); },
DoTest: function* (){ UnityEngine.Debug.Log$$Object();
yield (null);
UnityEngine.Debug.Log$$Object();
yield (new UnityEngine.WaitForSeconds.ctor());
var www = new UnityEngine.WWW.ctor$$String("file://" + UnityEngine.Application.get_dataPath() + "/JSBinding/Samples/Coroutine/CoroutineReadme.txt");
yield (www);
UnityEngine.Debug.Log$$Object("Text from WWW: " + www.get_text());
yield (this.StartCoroutine$$IEnumerator(this.WaitForCangJingKong()));
UnityEngine.Debug.Log$$Object("Wait for CangJingKong finished!"); }
}
};
JsTypes.push(TestCoroutine);

需要修改的有:

  1. 协程函数改用 function* 定义
  2. 删除 $yield 数组的定义以及协程函数最后的返回
  3. 将 $yield.push 替换为 yield 。

===================================================

2015/07/13 22:18 更新,目前已经把这个替换工作做到菜单了,菜单是 JSB | Correct JavaScript Yield code

这个菜单会尝试替换所有在 JSBindingSetting.jsDir 目录下的所有 JavaScript 文件。你只需要在编译 SharpKit 工程后运行一下这个菜单即可,如果有错误会给出提示,如果没错,代码应该可以正常使用了!

目前这个方案算是比较完美了。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgMAAAE3CAIAAAC4utUmAAAgAElEQVR4nO2d+7ccRbn3609w4VpcDIHcJPdk9s7eHRIQcxGFADHktrPhII2X5eV41BeFk0RQ9JW0eF495oBEFhAP4OLWhIhcDhAhgCIBQhOQiyGEhICu41mvZtBX13teduT9oXu6n6p6qrp7ZnqmZ/r7WbWyZnq6q556qvr5VlXvdIndvx/rcHq2mPTM78ee/t3Yrt+NPfX22K/fHvvVW2O/PDT2xKGxx94c23lw7JEDYzveGHto/7sP7n/3P15/9/7X371v37v3vvbuz197957X3r1n77s/KzhtR0JCQiprErt+d6QU6e1W01NvH/n1W0eefOvIrw4d+dWhI0+8eeTxN4/sPHjk0QNHGjIw9uD+sQdeH7t/39h9+8bufW3s56+N/fy1sXv2jt2zd+xnSEhISFVN4u0/H+mn9Nafj7z1zpG33jny5jtH3nxn7OA7YwfrYwfqYwfqY28cHnvj8Nj+w2P7D4+9fnjs9T+Ovf7HsX1ISEhIlU9i9B83FpM2jP7jhtEvSGldD6f1tvR5JCQkpB5O4u9/fw8JCQkJqcpJjB35u5Luve++L3/5K+vWjbYxffnLX7n3vvv0spCQkJCQup7E3/7vGE3b7r7nM5/57OZ/u+Yn/35zG9Pmf7vm/H+4YNvd9yjFISEhISF1PYk///X/0fS5L3xx89U/uu2Oux54cEcb00M7Hv3XH179uS98USkOCQkJCanrSRz+83/TtGr1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5tmCCNKfC7FXhQYoK0sm+9mDUAIAQPvpASWgy+Ps98ZyDF2hJ0+Mk1+ZZ8iG9XrDZ6YgCfI7OUE1QHvGwGbLVwpKAABoP72gBAAAAIoESgAAAFUHSgAAAFUHSgAAAFUHSgAAAFUHSgAAAFUHSgAAAFWnJSUAAADQN0AJAACg6uRWAgAAAP1HDiVQGB4ertVq06ZNmzRp0oQJE04EAFSACRMmHHXUUcccc8wxxxxz1FFHjZ/xoXEzlyL1ejph7rLJzpoZSz4798yvDuRSAsdxBgcHZ82aNW3atJMAANVg6tSpRx999Lhx48aNG3f00UdPHlw2cehcpF5Pk501J536iVmnf3Husq/lU4L58+cPDw8PDg7WarW5AIBqUKvVxo0bN2nSpEmTJo0bN27maedPX/QZpF5PM5Z8dtbpX5xz5sW1s/5ZkYEUJQjFwHGcYQBAZZg/f/6JJ544derUqVOnnnjiiQMf+9ycMy9G6vU098yvzl12Se2sf9YnBOlKQCUBAFAFFixYMHHixOnTp0+fPn3ixInzlv1T7ez1SL2eBsKkaUA+JQAAVISFCxdSJRg660um8IHUNwlKAACQgBJUMEEJAAASUIIKpv8PrOBKlqwqt4IAAAAASUVORK5CYII=" alt="" />

以上这一段过时,新版本已经没有这个问题,请看其他文章介绍

---------------------------------------------------------------------------

下面讲一讲原理。

当我们在 C# 中使用 MonoBehaviour.StartCoroutine 函数时,传递给他代表协程函数的 IEnumerator(后面简称 IE)。之后是由 Unity 内部决定何时调用 IE.MoveNext()。而这部分的源代码我们是无法得到的。

我是学习了本文开始处第一个链接的内容,在 JavaScript 端写了一个模拟 Unity 功能的协程管理器。文件是:

StreamingAssets/JavaScript/Manual/UnityEngine_MonoBehaviour.javascript

(后面简称 B)。

这里顺便提一下,当你导出 MonoBehaviour 类时,会产生

StreamingAssets/JavaScript/Generated/UnityEngine_MonoBehaviour.javascript

文件,简称A。B 和 A 的关系是,在includes.javascript 中,包含顺序是先 A 后 B,B重写了一些 A 的函数,并增加了一些内部函数。目前重写的函数只有 StartCoroutine$$IEnumerator 和 StartCoroutine$$String。增加的函数有 $UpdateAllCoroutines,$updateCoroutine等等,这些就是协程管理器的内容。以下贴出代码(可能不是最新的):

 _jstype = undefined;
for (var i = ; i < JsTypes.length; i++) {
if (JsTypes[i].fullname == "UnityEngine.MonoBehaviour") {
_jstype = JsTypes[i];
break;
}
} if (_jstype) {
_jstype.definition.StartCoroutine$$String = function(a0/*String*/) {
if (this[a0])
{
var fiber = this[a0].call(this);
return this.$AddCoroutine(fiber);
}
}
_jstype.definition.StartCoroutine$$IEnumerator = function(a0/*IEnumerator*/) {
return this.$AddCoroutine(a0);
} //
// Coroutine Scheduler
//
// REFERENCE FROM
//
// Coroutine Scheduler:
// http://wiki.unity3d.com/index.php/CoroutineScheduler
//
// JavaScript yield documents:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield
// // fiber 类似于 C# 的 IEnumerator
_jstype.definition.$AddCoroutine = function (fiber) {
var coroutineNode = {
$__CN: true, // mark this is a coroutine node
prev: undefined,
next: undefined,
fiber: fiber,
finished: false, waitForFrames: , // yield null
waitForSeconds: undefined, // WaitForSeconds
www: undefined, // WWW
waitForCoroutine: undefined, // Coroutine
}; if (this.$first) {
coroutineNode.next = this.$first;
this.$first.prev = coroutineNode;
}; this.$first = coroutineNode;
// NOTE
// return coroutine node itself!
return coroutineNode;
} // this method is called from LateUpdate
_jstype.definition.$UpdateAllCoroutines = function (elapsed) {
// cn is short for Coroutine Node
var cn = this.$first;
while (cn != undefined) {
// store next coroutineNode before it is removed from the list
var next = cn.next;
var update = false; if (cn.waitForFrames > ) {
cn.waitForFrames--;
if (cn.waitForFrames <= ) {
waitForFrames = ;
this.$UpdateCoroutine(cn);
}
}
else if (cn.waitForSeconds) {
if (cn.waitForSeconds.get_finished(elapsed)) {
cn.waitForSeconds = undefined;
this.$UpdateCoroutine(cn);
}
}
else if (cn.www) {
if (cn.www.get_isDone()) {
cn.www = undefined;
this.$UpdateCoroutine(cn);
}
}
else if (cn.waitForCoroutine) {
if (cn.waitForCoroutine.finished == true) {
cn.waitForCoroutine = undefined;
this.$UpdateCoroutine(cn);
}
}
else {
this.$UpdateCoroutine(cn);
}
cn = next;
}
} _jstype.definition.$UpdateCoroutine = function (cn) { // cn is short for Coroutine Node
var fiber = cn.fiber;
var obj = fiber.next();
if (!obj.done) {
var yieldCommand = obj.value;
// UnityEngine.Debug.Log$$Object(JSON.stringify(yieldCommand));
if (yieldCommand == null) {
cn.waitForFrames = ;
}
else {
if (yieldCommand instanceof UnityEngine.WaitForSeconds.ctor) {
cn.waitForSeconds = yieldCommand;
}
else if (yieldCommand instanceof UnityEngine.WWW.ctor) {
cn.www = yieldCommand;
}
else if (yieldCommand.$__CN === true/*yieldCommand.toString() == "[object Generator]"*/) {
cn.waitForCoroutine = yieldCommand;
}
else {
throw "Unexpected coroutine yield type: " + yieldCommand.GetType();
}
}
}
else {
// UnityEngine.Debug.Log$$Object("cn.finished = true;");
cn.finished = true;
this.$RemoveCoroutine(cn);
}
} _jstype.definition.$RemoveCoroutine = function (cn) { // cn is short for Coroutine Node
if (this.$first == cn) {
this.$first = cn.next;
}
else {
if (cn.next != undefined) {
cn.prev.next = cn.next;
cn.next.prev = cn.prev;
}
else if (cn.prev) {
cn.prev.next = undefined;
}
}
cn.prev = undefined;
cn.next = undefined;
}
}

目前支持的 yield return 后面可接的类型有:

  1. yield return null; // 下一帧调用 MoveNext()
  2. yield return new WWW(...); // WWW
  3. yield return new WaitForSeconds(...); // 等待一定时间
  4. yield return new StartCoroutine(...); // 串连另一个协程

C# 协程和 JavaScript 协程有一个区别:C#是协程初始就调用了 MoveNext(),JavaScript 需要初始调用 next() 才能和 C# 匹配(现在没有调用,因为一帧的时间也挺快的,效果差不多一样)。

另外,看前面的 C# 代码有这样的代码:

 void LateUpdate()
{
jsimp.Coroutine.UpdateMonoBehaviourCoroutine(this);
}

现在因为我们自己要管理协程,所以需要有一个 Update 入口。如果想让 JavaScript 协程正常工作,必须在某个地方调用协程管理器的 Update。现在我是把他放在 LateUpdate 函数中,如果你想换地方,也是可以的。

在 C# 中,jsimp.Coroutine.UpdateMonoBehaviourCoroutine 函数并不做任何事情,只有当运行 JavaScript 版本时,才有做事情。JavaScript 的实现是在这个文件中:

StreamingAssets/JavaScript/JSImp/Coroutine.javascript

 if (typeof(JsTypes) == "undefined")
var JsTypes = [];
var jsimp$Coroutine = {
fullname: "jsimp.Coroutine",
baseTypeName: "System.Object",
staticDefinition: {
UpdateMonoBehaviourCoroutine: function (mb){
mb.$UpdateAllCoroutines(UnityEngine.Time.get_deltaTime());
}
},
assemblyName: "SharpKitProj",
Kind: "Class",
definition: {
ctor: function (){
System.Object.ctor.call(this);
}
}
}; // replace old Coroutine
jsb_ReplaceOrPushJsType(jsimp$Coroutine);

这个文件同样在 includes.javascript 中进行了包含。

看第8行,调用了 $UpdateAllCoroutines 函数更新协程管理器。

对于 WaitForSeconds 类,C#中并没有暴露任何接口。我们无法判断一个 WaitForSeconds 是否时间已到。所以我又自定义了这个类,来达到这个目的。

文件是:StreamingAssets/JavaScript/Manual/UnityEngine_WaitForSeconds.javascript

 _jstype = undefined;
for (var i = ; i < JsTypes.length; i++) {
if (JsTypes[i].fullname == "UnityEngine.WaitForSeconds") {
_jstype = JsTypes[i];
break;
}
} if (_jstype) { _jstype.definition.ctor = function(a0) {
this.$totalTime = a0;
this.$elapsedTime = ;
this.$finished = false;
} _jstype.definition.get_finished = function(elapsed) {
if (!this.$finished) {
this.$elapsedTime += elapsed;
if (this.$elapsedTime >= this.$totalTime) {
this.$finished = true;
}
}
return this.$finished;
}
}

这个文件也很简单,只是记录初始时的时间,后面更新时时间进行递增。get_finished() 函数被协程管理器用于判断时间是否已到。

整个过程差不多就是这样,有想到什么再增加吧。

返回:Unity代码热更新方案 JSBinding + SharpKit 首页

JSBinding + SharpKit / Coroutine支持的更多相关文章

  1. JSBinding + SharpKit / 需要注意及不支持的列表

    1) 序列化不支持 public List<T>,其余都支持(JSBinding+Bridge无此功能) 2015年11月5日 补充:序列化只处理 Field.目前发现 Animation ...

  2. JSBinding + SharpKit / 实战:转换 2DPlatformer

    最后修改:2015年07月29日 2016年2月25日 2DPlatformer 是 Unity3D 的一个官方 Demo.本文将介绍使用 JSBinging + SharpKit 转换 2DPlat ...

  3. Unity代码热更新方案 JSBinding + SharpKit 首页

    目前Unity的代码更新方案有很多,主要以lua为主. JSBinding + SharpKit 是一种新的技术,他做了两件事情: JSBinding将C#导出到 JavaScript (引擎是 Mo ...

  4. JSBinding + SharpKit / 编译 Cs 成 Js

    轻轻一点菜单:[JSB | Compile Cs to Js] 主要产出:StreamingAssets/JavaScript/SharpkitGeneratedFiles.javascript,你的 ...

  5. JSBinding + SharpKit / 实战:转换 Survival Shooter

    从 asset store 下载 Survival Shooter (商店里有2个版本,一种是给Unity5用的,一个是给Unity4.6用的,我们这个实验用的是后者,版本是2.2.如果) 1 删除多 ...

  6. JSBinding + SharpKit / JavaScript 加载流程

    首先,现在的方案是游戏启动就加载全部的 JavaScript 代码. 先看下 StreamingAssets/JavaScript/ 文件夹下的目录结构:

  7. JSBinding + SharpKit / 常见问题

    运行时出现: Return a "System.Xml.XmlIteratorNodeList" to JS failed. Did you forget to export th ...

  8. JSBinding + SharpKit / Important Notes

    Serialization of List<T> is not supported. 1 public int v; // SUPPORTED 2 public GameObject go ...

  9. JSBinding+SharpKit / 菜单介绍

随机推荐

  1. Quartz 2D--长方形和线条绘图

    今天原本想模仿2048 游戏的. 但是在设计背景环境时,涉及到绘图的知识!于是就开始对绘图进行了一翻学习. 若想自己绘图必须 写自己的View(继承UICView):然后重写UIView 中的 dra ...

  2. jQuery 元素遍历

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 操作系统:cpu调度 6-25

    1. 进程选择 1小时和1分钟? 进程优先1分钟,再执行1小时. 时间短的进程先执行,执行顺序也有关. 2. 遇到io操作,执行的进程先让出cpu,切换其他进程. 3.进程先来先服务,进程调度策略: ...

  4. Ubuntu 14.10 下安装java反编译工具 jd-gui

    系统环境,Ubuntu 14.10 ,64位 1 下载JD-GUI,网址http://221.3.153.126/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/jd.benow.ca/ ...

  5. maven学习之二M2_HOME简介

    在<maven学习之一>中介绍了M2_HOME指向了maven的安装目录,如下图: weiwan..................

  6. 2016-1-15 抽屉效果实现demo

    // // ViewController.m // 抽屉 // // Created by Mac on 16/1/15. // Copyright © 2016年 Mac. All rights r ...

  7. Why am I getting an error converting a Foo** → const Foo**?

    Because converting Foo** → const Foo** would be invalid and dangerous. C++ allows the (safe) convers ...

  8. iOS开发 coreText

    coreText的demo下载地址:http://download.csdn.net/detail/shaoting19910730/9254143 NSTextView和Attribued Stri ...

  9. CODEVS1001 舒适的路线 (并查集)

    对所有边从大到小排序,枚举最大边,O(m)验证,用并查集维护图是否联通. program CODEVS1001; ; maxn=; INF=; type arr=record u,v,w:int64; ...

  10. Unity3D ShaderLab 使用BlinnPhong高光类型

    Unity3D shaderLab 使用BlinnPhong高光类型 上一篇我们实现了自定义高光类型,这一篇,我们说Blinn高光,它是另一种计算和估算高光更高效的方式,它是通过视线防线和光线方向,所 ...