You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity WebGL构建报错‘ReferenceError: _GetBrowserQueryString is not defined’如何解决?

问题:Unity WebGL调用JS函数出现ReferenceError: _GetBrowserQueryString is not defined错误

我在Unity中尝试调用JavaScript函数,已按照官方文档创建Plugins文件夹,添加了后缀为.jslib的JS文件,代码如下:

mergeInto(LibraryManager.library, {
    GetBrowserQueryString: () => {
        const qs = window.location.search;
        const bufferSize = lengthBytesUTF8(qs) + 1;
        const buffer = _malloc(bufferSize);

        stringToUTF8(qs, buffer, bufferSize);

        return buffer;
    },
    GetVkUserId: () => {
        const qs = window.location.search;
        const match = qs.match(/vk_user_id=(\d+)/);

        const vkUserIdString = match[1];
        const bufferSize = lengthBytesUTF8(vkUserIdString) + 1;
        const buffer = _malloc(bufferSize);

        stringToUTF8(vkUserIdString, buffer, bufferSize);

        return buffer;
    }
});

同时已在该jslib文件的Inspector面板中勾选WebGL选项。C#调用代码如下:

public class SocketManager : MonoBehaviour {
    ... 
    [DllImport("__Internal")]
    private static extern string GetBrowserQueryString();
    [DllImport("__Internal")]
    private static extern string GetVkUserId();

    private void Awake() {
        string vkQuery = GetBrowserQueryString();
        string vkUserId = GetVkUserId();
        ... 
    }
}

但在浏览器中运行构建后的程序时,出现错误ReferenceError: _GetBrowserQueryString is not defined,请问哪里操作有误?该如何修复?


解决方案

1. 修正jslib文件的存放路径

Unity WebGL要求jslib文件必须放在Assets/Plugins/WebGL目录下(需手动创建WebGL子文件夹),而非直接放在Plugins根目录。如果路径错误,Unity构建时不会打包该jslib文件,导致函数无法被找到。

2. 匹配C#与JS函数的命名绑定规则

Unity WebGL默认会给C#声明的外部函数自动添加下划线前缀,再去JS中查找对应的函数。你有两种修复方式:

  • 方式一:修改JS函数名,添加下划线前缀
    调整jslib中的函数名,与Unity自动生成的调用名匹配:
    mergeInto(LibraryManager.library, {
        _GetBrowserQueryString: () => {
            const qs = window.location.search;
            const bufferSize = lengthBytesUTF8(qs) + 1;
            const buffer = _malloc(bufferSize);
    
            stringToUTF8(qs, buffer, bufferSize);
    
            return buffer;
        },
        _GetVkUserId: () => {
            const qs = window.location.search;
            const match = qs.match(/vk_user_id=(\d+)/);
            const vkUserIdString = match ? match[1] : ""; // 新增空值判断
            const bufferSize = lengthBytesUTF8(vkUserIdString) + 1;
            const buffer = _malloc(bufferSize);
    
            stringToUTF8(vkUserIdString, buffer, bufferSize);
    
            return buffer;
        }
    });
    
  • 方式二:在C#中显式指定EntryPoint
    保持JS函数名不变,在DllImport属性中指定要调用的JS函数名:
    [DllImport("__Internal", EntryPoint = "GetBrowserQueryString")]
    private static extern string GetBrowserQueryString();
    [DllImport("__Internal", EntryPoint = "GetVkUserId")]
    private static extern string GetVkUserId();
    

3. 修复JS函数的空值风险

原GetVkUserId函数中,如果URL没有vk_user_id参数,match会返回null,调用match[1]会直接报错。需要添加空值判断,避免程序崩溃。

4. 重新构建并清理缓存

修改jslib文件后,必须完全重新构建WebGL项目,并且在浏览器中清除缓存(或使用无痕模式测试),防止旧的JS代码被浏览器缓存加载。


内容的提问来源于stack exchange,提问作者oleegarch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 15:20:36