在 Unity WebGL 中从 JavaScript 返回复数值
Returning complex values from JavaScript in Unity WebGL
我正在使用 Unity3D 的 WebGL 支持,并且我正在尝试从我的代码中调用 JavaScript 库以执行一些计算。我要调用的函数的 return 类型是一个 JavaScript 对象。 The Unity documentation 提供有关如何从 JavaScript 和 return 原始值调用函数的信息:
[DllImport("__Internal")]
private static extern string StringReturnValueFunction();
所以我目前使用的方式是将对象转换成JSON,传入Unity,然后反序列化。正如您想象的那样,这种方法存在一些性能问题。
我真正想做的是直接 return 一个 JavaScript 对象并从 C# 访问它:
[DllImport("__Internal")]
private static extern MyStruct StructReturnValueFunction();
是否有一些有效的方法来映射 JavaScript 对象,以便可以从 emscripten 端的托管 C# 访问它?
据我所知,这是不可能的。
看看this thread。
这两个帖子几乎完全符合您的要求:
dansav
Is there any way to share user created classes between unity c# and webgl javascript? I was reading about Emscripten Embind which seems to offer a solution for sharing classes between c++ and javascript. Is it possible to make a jslib class?
https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html
以及UnityTechnologies的回答
No, you can not use this mechanism directly for C#. As has been noted before, C# scripts are first transformed into C++ code using il2cpp. There is no direct correspondence between C# classes and their generated C++ representations, and even if there was, it would be Unity version specific, and therefore you would not be able to rely on it. It should be however possible to achieve something like this using C plugin.
Note that mentioned C++ binding does not involve sharing of the actual class objects or memory. When you use instance.x=20 in JavaScript from the above example, you do not modify the x value on the heap directly, but instead implicitly execute the JavaScript setter function of the instance, which then transfers your value 20 to the binded &MyClass::setX C++ function. So this only looks like sharing while in fact this is copying. This mechanism has been implemented for programming convenience and you should not expect any performance boost when using it. Moreover, you can implement the same accessor functions for your C# classes yourself (in which case you will also have to add the binding code to the JavaScript manually).
但是,从 2016 年开始......事情可能已经发生了变化,但 afaik 仍然 string
并且数字类型和它们的数组是唯一直接可能的 return 类型。
我正在使用 Unity3D 的 WebGL 支持,并且我正在尝试从我的代码中调用 JavaScript 库以执行一些计算。我要调用的函数的 return 类型是一个 JavaScript 对象。 The Unity documentation 提供有关如何从 JavaScript 和 return 原始值调用函数的信息:
[DllImport("__Internal")]
private static extern string StringReturnValueFunction();
所以我目前使用的方式是将对象转换成JSON,传入Unity,然后反序列化。正如您想象的那样,这种方法存在一些性能问题。
我真正想做的是直接 return 一个 JavaScript 对象并从 C# 访问它:
[DllImport("__Internal")]
private static extern MyStruct StructReturnValueFunction();
是否有一些有效的方法来映射 JavaScript 对象,以便可以从 emscripten 端的托管 C# 访问它?
据我所知,这是不可能的。
看看this thread。
这两个帖子几乎完全符合您的要求:
dansav
Is there any way to share user created classes between unity c# and webgl javascript? I was reading about Emscripten Embind which seems to offer a solution for sharing classes between c++ and javascript. Is it possible to make a jslib class?
https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html
以及UnityTechnologies的回答
No, you can not use this mechanism directly for C#. As has been noted before, C# scripts are first transformed into C++ code using il2cpp. There is no direct correspondence between C# classes and their generated C++ representations, and even if there was, it would be Unity version specific, and therefore you would not be able to rely on it. It should be however possible to achieve something like this using C plugin.
Note that mentioned C++ binding does not involve sharing of the actual class objects or memory. When you use instance.x=20 in JavaScript from the above example, you do not modify the x value on the heap directly, but instead implicitly execute the JavaScript setter function of the instance, which then transfers your value 20 to the binded &MyClass::setX C++ function. So this only looks like sharing while in fact this is copying. This mechanism has been implemented for programming convenience and you should not expect any performance boost when using it. Moreover, you can implement the same accessor functions for your C# classes yourself (in which case you will also have to add the binding code to the JavaScript manually).
但是,从 2016 年开始......事情可能已经发生了变化,但 afaik 仍然 string
并且数字类型和它们的数组是唯一直接可能的 return 类型。