如何从jslib插件Unity webgl调用外部javascript函数

dav*_*803 5 javascript unity-game-engine unity-webgl

我现在正在开发一个 webgl 项目,我正在尝试从plugin.jslib调用index.html中的javascript函数

我用谷歌搜索了一些方法,似乎它们不起作用。
有没有正确且简单的方法来做到这一点?

下面的代码是我尝试过的。

索引.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>
Run Code Online (Sandbox Code Playgroud)

插件.jslib

mergeInto(LibraryManager.library {
    Loaded: function()
    {
        window.CheckLoad();
    },
}); 
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

dav*_*803 5

嗯..我很傻。
事实证明这是我的错误,而且做这些事情的方法很简单。

对于那些可能有同样问题的人,请检查以下代码。

索引.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>
Run Code Online (Sandbox Code Playgroud)

插件.jslib

var plugin = {
    Loaded: function()
    {
        CheckLoad();
    }
};

mergeInto(LibraryManager.library, plugin);
Run Code Online (Sandbox Code Playgroud)