简体   繁体   中英

Unity 5 WebGL: Communicating with external javascript from C#

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.

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.");
    }
}

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 () {

    }
}

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

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.

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:

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.

(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>

...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");

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');

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM