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

Unity 2021.3导入WebGL jslib插件出现undefined symbol错误求助

Unity 2021.3 WebGL jslib 「undefined symbol: Hello」错误解决方案

在Unity 2021.3版本中按照官方文档实现WebGL jslib插件的HelloWorld示例时,出现「undefined symbol: Hello」错误,已排查函数名拼写、Plugins文件夹路径等基础问题,代码均直接复制自官方文档,项目无其他错误。

涉及代码

.jslib文件内容

mergeInto(LibraryManager.library,
{
    Hello: function()
    {
        window.alert("Hello, world!");
    }
});

C#文件内容

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

public class importtest : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern void Hello();
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

错误信息

Library\Bee\artifacts\WebGL\build\debug_WebGL_wasm\build.js: undefined symbol: Hello (referenced by top-level compiled C/C++ code)
UnityEngine.GUIUtility.ProcessEvent(int,intptr,bool&)

解决步骤

  • 检查jslib文件的平台编译设置:选中Plugins文件夹中的jslib文件,在Inspector面板的「Platform Settings」区域,确认WebGL选项已勾选,且「Include Platforms」设置为WebGL。Unity 2021.3可能默认未正确配置该选项,导致jslib未被纳入构建。
  • 确认jslib文件扩展名:必须为.jslib,不能是.js或其他后缀,否则Unity无法识别为WebGL插件资源。
  • 在C#代码中添加函数调用并加平台判断:当前代码未实际调用Hello()函数,添加调用并通过宏判断避免编辑器报错,修改后的Start方法如下:
    void Start()
    {
        #if UNITY_WEBGL && !UNITY_EDITOR
        Hello();
        #endif
    }
    
  • 清理项目后重新构建:删除项目根目录下的Library、Temp、obj文件夹,重启Unity等待资源重新导入,再尝试WebGL构建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 14:25:13