【发布时间】:2020-05-22 01:51:57
【问题描述】:
我现在正在开发一个 webgl 项目,我正在尝试从 plugin.jslib 调用 index.html 中的 javascript 函数
我用谷歌搜索了一些方法,但似乎它们不起作用。
有没有合适且简单的方法来做到这一点?
下面的代码是我尝试过的。
index.html
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>%UNITY_WEB_NAME%</title>
<link rel="shortcut icon" href="TemplateData/favicon.ico">
<link rel="stylesheet" href="TemplateData/style.css">
<script src="TemplateData/UnityProgress.javascript"></script>
<script src="%UNITY_WEBGL_LOADER_URL%"></script>
<script type="text/javascript">
window.CheckLoad = function(){ window.alert('It is working!!'); };
</script>
<script>
var gameInstance = UnityLoader.instantiate("gameContainer", "%UNITY_WEBGL_BUILD_URL%", {onProgress: UnityProgress});
</script>
</head>
<body>
...
</body>
</html>
plugin.jslib
mergeInto(LibraryManager.library {
Loaded: function()
{
window.CheckLoad();
},
});
Unity C# 脚本
public class blablabla : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void Loaded();
public static void IsLoaded()
{
#if !UNITY_EDITOR && UNITY_WEBGL
Loaded();
#endif
}
void Start()
{
IsLoaded();
}
}
【问题讨论】:
-
您是否在控制台中看到任何错误?
-
@Papa 实际上,存在构建错误。所以无法测试它。
-
@Papa 所以我想通了。问题是我使用jslib的错误。还是谢谢!
标签: javascript unity3d unity-webgl