【问题标题】:Unity 5 WebGL: Communicating with external javascript from C#Unity 5 WebGL:从 C# 与外部 javascript 通信
【发布时间】:2016-09-20 19:00:46
【问题描述】:

我正在尝试将 Unity 5 WebGL 项目与 SCORM 1.2 集成。这涉及到 Unity WebGL 构建与一些外部 javascript 通信。

我找到了这个页面:
https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
...这建议将外部 js 放在 Assets/Plugins/WebGL 文件夹中,并将文件保存为 .jslib 文件。这是我的临时 SCORM.jslib 文件的外观:

var SCORM = {
    Hello: function()
    {
        window.alert("Hello, world!");
        console.log("Hello function in SCORM.jslib has been called.");
    }
}

然后,在附加到我的游戏对象触发器(一个立方体)的 C# 脚本中:

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class CompletionTrigger : MonoBehaviour {    

    [DllImport("__Internal")]
    private static extern void Hello();

    void Start () {

        Hello();

    }    

    void Update () {

    }
}

问题是我在调用 'Hello()' 的那一行遇到了 'EntryPointNotFoundException' 错误。

我在这里用谷歌搜索过,但答案似乎与开发 C++ dll 或 RoR 有关。

有谁知道是什么导致了这个错误? 谢谢。

编辑
我在另一个页面上看到一条评论说错误只出现在编辑器中,所以我想我会再次运行 WebGL 构建。我收到以下错误:

Failed running "C:\Program Files\Unity\Editor\Data\PlaybackEngines\WebGLSupport/BuildTools/Emscripten_Win/python/2.7.5.3_64bit/python.exe" "C:\Program Files\Unity\Editor\Data\PlaybackEngines\WebGLSupport/BuildTools/Emscripten/emcc" @"D:/Projects/Unity 3D/Packaging Test/Assets/../Temp/emcc_arguments.resp"
stdout:
WARNING: sanity check failed to run [Errno 13] Permission denied: 'C:\\Program Files\\Unity\\Editor\\Data\\PlaybackEngines\\WebGLSupport/BuildTools/emscripten.config_sanity'
stderr:
WARNING  root: did not see a source tree above or next to the LLVM root directory (guessing based on directory of C:\Program Files\Unity\Editor\Data\PlaybackEngines\WebGLSupport/BuildTools/Emscripten_FastComp_Win\llc), could not verify version numbers match
INFO     root: (Emscripten: Running sanity checks)
WARNING  root: java does not seem to exist, required for closure compiler, which is optional (define JAVA in ~/.emscripten if you want it)
error: failure to execute js library "D:\Projects\Unity 3D\Packaging Test\Assets\Plugins\WebGL\SCORM.jslib": ReferenceError: window is not defined,,ReferenceError: window is not defined

错误消息持续了一段时间,但我在这里包含的最后一行似乎表明编译器不知道如何处理“窗口”,说它没有定义。那里还提到了“健全性检查”,这似乎很贴切。

再次,如果有人有任何指示,将不胜感激。

【问题讨论】:

    标签: javascript c# unity3d scorm


    【解决方案1】:

    如果有人遇到同样的问题,我已经设法避开了这个问题:

    我使用了 Unity 手册页面上的第一个选项,所以现在我的 SCORM.js 文件已加载到 index.html WebGL 模板中。

    (要制作自定义 WebGL 模板,请在 Assets 文件夹中创建一个“WebGLTemplates”文件夹。Assets/Templates 中包含 index.html 文件的任何文件夹都将在 Edit > Project Settings > Player 检查面板中作为模板提供)。

    所以,添加...

    <script src="js/SCORM.js"></script>
    

    ...到我的 index.html 并将 SCORM.js 放在一个名为“js”的文件夹中 - 与最终构建 index.html 文件相邻,我现在可以从 C# 调用 SCORM.js 函数,如下所示:

    Application.ExternalCall("initialiseSCORM", "This could specify an id");
    

    下一步将从 SCORM.js 获取 SCORM 信息,但正如该页面所建议的,我应该能够使用:

    SendMessage ('MyGameObject', 'MyFunction', 'foobar');
    

    ...在 SCORM.js 中将信息传递到 Unity 游戏中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-31
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多