Unity 5 WebGL:与C#中的外部JavaScript通信 [英] Unity 5 WebGL: Communicating with external javascript from C#

查看:1498
本文介绍了Unity 5 WebGL:与C#中的外部JavaScript通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用与SCORM 1.2集成的Unity 5 WebGL项目。这涉及Unity WebGL构建与一些外部JavaScript进行通信。

I'm trying to get a Unity 5 WebGL project integrated with SCORM 1.2. This involves the Unity WebGL build communicating with some external javascript.

我找到了这个页面:

https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

...建议将外部js放在Assets / Plugins / WebGL中文件夹并将文件另存为.jslib文件。以下是我的临时SCORM.jslib文件的外观:

I found this page:
https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
...which suggests placing the external js in an Assets/Plugins/WebGL folder and saving the file as a .jslib file. Here's how my temporary SCORM.jslib file looks:

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

然后,在附加到我的gameObject触发器的C#脚本中(立方体):

Then, in a C# script attached to my gameObject trigger (a cube):

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'错误。

The problem is I'm getting an 'EntryPointNotFoundException' error at the line where 'Hello()' is called.

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

I've googled and searched here, but the answers seem to relate to developing C++ dlls or RoR.

有谁知道导致此错误的原因是什么?
谢谢。

Does anyone know what's causing this error? Thanks.

编辑

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

EDIT
I saw a comment on another page that said the error appeared only in the editor so I thought I'd run a WebGL build again. I get the following error:

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

错误消息会持续一段时间,但我在此处包含的最后一行似乎建议编译器不知道如何处理'窗口',说它没有定义。还有一个提到健全检查,这似乎很合适。

The error message continues for a while, but the last line I've included here seems to suggest the compiler doesn't know what to do with 'window', saying it's not defined. There's also mention of a 'sanity check' in there, which seems quite apt.

再一次,如果有人有任何指针,我将非常感激。

Once again, if anyone has any pointers, it would be much appreciated.

推荐答案

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

In case anyone has the same problem, I've managed to dodge this issue:

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

I used the first option on that Unity Manual page instead, so now my SCORM.js file is loaded into the index.html WebGL template.

(要制作自定义WebGL模板,请在Assets文件夹中创建'WebGLTemplates'文件夹。包含index.html文件的资产/模板中的任何文件夹都将作为模板提供在编辑>项目设置>播放器检查器面板中。

(To make a custom WebGL template, create a 'WebGLTemplates' folder in your Assets folder. Any folders in Assets/Templates containing index.html files will be available as templates in the Edit > Project Settings > Player inspector panel).

所以,添加了......

So, having added...

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

...到我的index.html并将SCORM.js放在名为'js'的文件夹中 - 在最终构建index.html文件旁边,我现在可以从C#调用SCORM.js函数,如下所示:

...to my index.html and put SCORM.js in a folder called 'js' - adjacent to the final build index.html file, I can now call SCORM.js functions from C#, like this:

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

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

The next step will be getting SCORM information from SCORM.js, but as that page suggested, I should be able to use:

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

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

...in SCORM.js to pass information into the Unity game.

这篇关于Unity 5 WebGL:与C#中的外部JavaScript通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆