我有一个 WebGL unity 项目,它尝试在浏览器上执行 javascript 代码并返回一个值。
我有以下内容.jslib
我的 Assets/Plugins/WebGL 文件夹中的文件:
var BrowserPlugin = {
GetEndpointURL: function()
{
var endpoint = window.itd.getEndpointUrl();
console.log("endpoint: " + endpoint);
return endpoint;
}
};
mergeInto(LibraryManager.library, BrowserPlugin);
在 Unity 的 C# 代码中,我导入 dll 并调用我的 javascript 方法,如下所示:
[DllImport("__Internal")]
private static extern string GetEndpointURL();
string endpointURL = GetEndpointURL();
问题是,在我的 c# 代码中endpointUrl
变量始终为空。但是,在我的浏览器控制台中,我可以清楚地看到在返回之前正确的值已记录在浏览器 JavaScript 中。是什么导致该值恢复为空?
这是你的代码:
GetEndpointURL: function()
{
var endpoint = window.itd.getEndpointUrl();
console.log("endpoint: " + endpoint);
return endpoint;
}
You cannot返回字符串(endpoint
) 直接地。您必须创建一个缓冲区来保存该字符串,此过程包括分配内存_malloc
并将旧字符串复制到新的内存位置writeStringToMemory
.
GetEndpointURL: function()
{
var endpoint = window.itd.getEndpointUrl();
console.log("endpoint: " + endpoint);
var buffer = _malloc(lengthBytesUTF8(endpoint) + 1);
writeStringToMemory(endpoint, buffer);
return buffer;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)