问题描述
我现在正在开发一个 webgl 项目,我正在尝试从 plugin.jslib 调用 index.html 中的 javascript 函数
I'm working on a webgl project now and I'm trying to call javascript function in index.html from plugin.jslib
我在谷歌上搜索了一些方法,但似乎它们不起作用.
有没有合适且简单的方法来做到这一点?
I did google some methods and seems they're not working.
Is there a proper and simple way to do this?
下面的代码是我试过的.
below codes are the ones that i tried.
index.html
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
plugin.jslib
mergeInto(LibraryManager.library {
Loaded: function()
{
window.CheckLoad();
},
});
Unity C# 脚本
Unity C# script
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();
}
}
推荐答案
好吧..我太笨了.
原来这是我的错误,做这些事情的方法很简单.
Well.. I was stupid.
turned out it was my mistake and the way to do these stuffs are quite easy.
对于那些可能有相同问题的人,请检查以下代码.
For those who might have same question, check below codes.
index.html
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>
var gameInstance = UnityLoader.instantiate("gameContainer", "%UNITY_WEBGL_BUILD_URL%", {onProgress: UnityProgress});
function CheckLoad(){
window.alert("WORKING~!");
}
</script>
</head>
<body>
...
</body>
</html>
plugin.jslib
plugin.jslib
var plugin = {
Loaded: function()
{
CheckLoad();
}
};
mergeInto(LibraryManager.library, plugin);
这篇关于如何从 jslib 插件 Unity webgl 调用外部 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!