我正在尝试遵循基本教程来让 jslib 插件在 Unity 中工作。甚至是最基本的“你好,世界!”文档here中给出的示例在 Unity 2021.3 中给出了“未定义符号”错误。我已经四处寻找解决方案,但我看到的大多数问题都与函数名称拼写错误或不在 Plugins 文件夹中有关,我已经验证这两个问题在我的代码中都是正确的。
.jslib 文件是:
mergeInto(LibraryManager.library,
{
Hello: function()
{
window.alert("Hello, world!");
}
});
.cs 文件是:
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&)
到目前为止,我已经验证所有内容都是直接从文档复制/粘贴的,文件路径正确,并且 Unity 项目中没有其他错误/代码。
更新Unity版本时出现此错误。 您需要关闭项目,从项目根目录中删除“Library”文件夹并重建项目。
将项目从
2022 LTS
升级到 2023
时,我遇到了同样的问题。我还尝试验证拼写错误的名称并删除“Library”文件夹。他们都没有帮助。
修复: 通过删除所有
.jslib
文件及其从脚本中的导入/调用,我让它再次工作。然后用不同的名称创建一个新的.jslib
。然后添加一些虚拟函数(例如“PrintHelloWorld”)并进行测试。希望您的“PrintHelloWorld”能够正常工作。然后开始将您的函数添加回这个新文件中,它们将再次像魅力一样工作。
PS:似乎在将项目升级到
Unity 2023
时会弄乱 jslib
文件的导入配置,这就是为什么旧文件不起作用但新添加的文件起作用的原因。虽然我也不确定。