Search Unity

WebGL Javascript plugin not working, missing function

Discussion in 'Web' started by JDMulti, Jul 20, 2018.

  1. JDMulti

    JDMulti

    Joined:
    Jan 4, 2012
    Posts:
    384
    I'm exploring some functionalities within WebGL to see how to get things done that way. But I'm having trouble getting a javascript plugin working within a build.

    I used this documentation to get started:
    https://docs.unity3d.com/560/Documentation/Manual/webgl-interactingwithbrowserscripting.html

    The build I made running at:
    https://jdmulti.nl/research/jdmulti_modelviewer/

    Build with: Unity 2018.2.0f2
    Running in: Chrome 67.0.3396.99
    Operation System: Windows 7

    I've created an javascript plugin in: Assets/Plugins/WebGL/ImageUploader.jslib with this code:
    Code (JavaScript):
    1. Assets/Plugins/WebGL/ImageUploader.jslib
    2. mergeInto(LibraryManager.library,
    3. {
    4.   Hello: function ()
    5.   {
    6.     window.alert("Hello, world!");
    7.   },
    8. });
    Then I created an C# script doing this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Runtime.InteropServices;
    3.  
    4. public class ImageUploader : MonoBehaviour
    5. {
    6.     [DllImport("__Internal")]
    7.     private static extern void Hello();
    8.  
    9.     public void OnButtonUpload()
    10.     {
    11.         Hello();
    12.     }
    13. }
    In the view I've got a button which will execute the function: 'OnButtonUpload' when pressed. But when I test it locally or on my server I won't get it successfully run and instead get the error:

    Code (CSharp):
    1. UnityLoader.js:4 missing function: Hello
    2. printErr @ UnityLoader.js:4
    3.  
    4. Invoking error handler due to
    5. Uncaught abort(-1) at Error
    6.     at jsStackTrace (WebGL.wasm.framework.unityweb:2:22127)
    7.     at stackTrace [Object.stackTrace] (WebGL.wasm.framework.unityweb:2:22298)
    8.     at Object.onAbort (https://jdmulti.nl/research/jdmulti_modelviewer/Build/UnityLoader.js:4:10650)
    9.     at abort (WebGL.wasm.framework.unityweb:2:481498)
    10.     at _Hello (WebGL.wasm.framework.unityweb:2:46544)
    11.     at wasm-function[23228]:1
    12.     at wasm-function[14281]:273
    13.     at wasm-function[14474]:22
    14.     at wasm-function[14500]:104
    15.     at wasm-function[12688]:77
    16.     at wasm-function[12689]:15
    17.     at wasm-function[12458]:152
    18.     at wasm-function[12462]:100
    19.     at wasm-function[22190]:114
    20.     at wasm-function[29565]:20
    21.     at dynCall_viiii [Object.UnityLoader.522d0a97dcf11b9129d21cebf76de72a.Module.dynCall_viiii] (WebGL.wasm.framework.unityweb:2:473723)
    22.     at invoke_viiii (WebGL.wasm.framework.unityweb:2:373474)
    23.     at wasm-function[27267]:341
    24.     at wasm-function[12386]:11
    25.     at wasm-function[12633]:1231
    26.     at wasm-function[12632]:251
    27.     at wasm-function[12631]:7
    28.     at wasm-function[12626]:106
    29.     at wasm-function[12390]:46
    30.     at wasm-function[12429]:523
    31.     at wasm-function[27958]:16
    32.     at wasm-function[29539]:20
    33.     at dynCall_iiiii [Object.UnityLoader.522d0a97dcf11b9129d21cebf76de72a.Module.dynCall_iiiii] (WebGL.wasm.framework.unityweb:2:462887)
    34.     at invoke_iiiii (WebGL.wasm.framework.unityweb:2:356649)
    35.     at wasm-function[24148]:170
    36.     at wasm-function[24286]:36
    37.     at wasm-function[2102]:67
    38.     at wasm-function[2101]:109
    39.     at wasm-function[6510]:473
    40.     at wasm-function[6498]:5
    41.     at wasm-function[8865]:721
    42.     at wasm-function[8864]:3
    43.     at wasm-function[7252]:30
    44.     at wasm-function[6600]:147
    45.     at wasm-function[6600]:170
    46.     at wasm-function[6595]:525
    47.     at wasm-function[6589]:200
    48.     at wasm-function[24759]:12
    49.     at dynCall_v [Object.UnityLoader.522d0a97dcf11b9129d21cebf76de72a.Module.dynCall_v] (WebGL.wasm.framework.unityweb:2:468842)
    50.     at browserIterationFunc (WebGL.wasm.framework.unityweb:2:133798)
    51.     at runIter [Object.runIter] (WebGL.wasm.framework.unityweb:2:136871)
    52.     at Browser_mainLoop_runner (WebGL.wasm.framework.unityweb:2:135333)
    53.  
    I've been trying several things which included removing the path within the javascript plugin and the path within Unity is also correct. Is there a place where I need to drag the plugin file into to have it included in my build? I'm clueless at the moment.

    Another question, is there a way to debug javascript plugins within the editor as build times take some time before you can test it, someone has a good workflow for that? :)
     
    Last edited: Jul 20, 2018
  2. kou_yeung

    kou_yeung

    Joined:
    Mar 24, 2016
    Posts:
    28
    the sample is work for me
    1.
    try to replace the line1 at ImageUploader.jslib
    Code (JavaScript):
    1. Assets/Plugins/WebGL/ImageUploader.jslib
    to
    Code (CSharp):
    1. //Assets/Plugins/WebGL/ImageUploader.jslib
    2.
    select "ImageUploader.jslib" in UnityEditor and check the Inspector.
    and check "Select platforms for plugin" -> "WebGL" is already on?