【问题标题】:How to call external javascript function from jslib plugin Unity webgl如何从jslib插件Unity webgl调用外部javascript函数
【发布时间】: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


【解决方案1】:

嗯..我很愚蠢。
原来这是我的错误,做这些事情的方法很容易。

对于可能有相同问题的人,请查看以下代码。

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

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

mergeInto(LibraryManager.library, plugin);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2021-12-23
    • 2021-05-07
    • 2013-09-15
    • 1970-01-01
    • 2016-07-31
    • 2014-10-01
    相关资源
    最近更新 更多