Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't get WebGL to Interact with browser scripting

Discussion in 'Editor & General Support' started by PizzaGuy213, Dec 2, 2018.

  1. PizzaGuy213

    PizzaGuy213

    Joined:
    Nov 23, 2010
    Posts:
    305
    Hi all.

    For 2 days now I've been trying to interact with browser scripting from Unity WebGL, without success.

    I can send messages from HTML to the Unity WebGL player, but when trying to interact with browser scripting I keep getting errors.

    I've followed a bunch of examples, but let's use the standard example from the Unity manual:
    https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

    Using Unity 2018.2.18f1

    In a folder called Plugins I've created a file called test.jslib:
    Code (CSharp):
    1. mergeInto(LibraryManager.library, {
    2.  
    3.   Hello: function () {
    4.     window.alert("Hello, world!");
    5.   },
    6.  
    7.   HelloString: function (str) {
    8.     window.alert(Pointer_stringify(str));
    9.   },
    10.  
    11.   PrintFloatArray: function (array, size) {
    12.     for(var i = 0; i < size; i++)
    13.     console.log(HEAPF32[(array >> 2) + i]);
    14.   },
    15.  
    16.   AddNumbers: function (x, y) {
    17.     return x + y;
    18.   },
    19.  
    20.   StringReturnValueFunction: function () {
    21.     var returnStr = "bla";
    22.     var bufferSize = lengthBytesUTF8(returnStr) + 1;
    23.     var buffer = _malloc(bufferSize);
    24.     stringToUTF8(returnStr, buffer, bufferSize);
    25.     return buffer;
    26.   },
    27.  
    28.   BindWebGLTexture: function (texture) {
    29.     GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
    30.   },
    31.  
    32. });
    33.  
    I got a gameobject containt a script called NewBehaviourScript:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Runtime.InteropServices;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour {
    5.  
    6.     [DllImport("__Internal")]
    7.     private static extern void Hello();
    8.  
    9.     [DllImport("__Internal")]
    10.     private static extern void HelloString(string str);
    11.  
    12.     [DllImport("__Internal")]
    13.     private static extern void PrintFloatArray(float[] array, int size);
    14.  
    15.     [DllImport("__Internal")]
    16.     private static extern int AddNumbers(int x, int y);
    17.  
    18.     [DllImport("__Internal")]
    19.     private static extern string StringReturnValueFunction();
    20.  
    21.     [DllImport("__Internal")]
    22.     private static extern void BindWebGLTexture(int texture);
    23.  
    24.     void Start() {
    25.         Hello();
    26.      
    27.         HelloString("This is a string.");
    28.      
    29.         float[] myArray = new float[10];
    30.         PrintFloatArray(myArray, myArray.Length);
    31.      
    32.         int result = AddNumbers(5, 7);
    33.         Debug.Log(result);
    34.      
    35.         Debug.Log(StringReturnValueFunction());
    36.      
    37.         var texture = new Texture2D(0, 0, TextureFormat.ARGB32, false);
    38.         BindWebGLTexture(texture.GetNativeTextureID());
    39.     }
    40. }

    But when building and running the WebGL player I get this error:

    missing function: Hello UnityLoader.js:4:10740
    -1 UnityLoader.js:4:10703
    -1 UnityLoader.js:4:10740
    Invoking error handler due to uncaught exception: abort(-1) at jsStackTrace (TemplateTest.wasm.framework.unityweb:2:22127) stackTrace (TemplateTest.wasm.framework.unityweb:2:22298) onAbort@file:///D:/Builds/TemplateTest/Build/UnityLoader.js:4:10645 abort (TemplateTest.wasm.framework.unityweb:2:481204) _Hello (TemplateTest.wasm.framework.unityweb:2:46724) @blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[27384]:0x8ad47d (blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[26720]:0x8a0acf) @blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[27676]:0x8c0345 d38edf9076c603d4878e64a366122da4 [UnityLoader.d38edf9076c603d4878e64a366122da4/Module.dynCall_iiiii] (TemplateTest.wasm.framework.unityweb:2:1) invoke_iiiii (TemplateTest.wasm.framework.unityweb:2:354531) @blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[24271]:0x84f271 (blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[24625]:0x85c523) @blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[1925]:0x9a32d (blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[1924]:0x9a296) @blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[3415]:0x100cf7 (blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[3410]:0x100a05) @blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[6314]:0x267100 (blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[4101]:0x16ac7f) @blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[7056]:0x2b9599 (blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[6409]:0x270d9f) @blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[6409]:0x270db6 (blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[6404]:0x26ffcc) @blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[6398]:0x26e852 (blob:null/fe108dac-08d1-4a2f-8802-2b2d79852168 line 2 > WebAssembly.instantiate:wasm-function[24873]:0x866f5a) d38edf9076c603d4878e64a366122da4 [UnityLoader.d38edf9076c603d4878e64a366122da4/Module.dynCall_v] (TemplateTest.wasm.framework.unityweb:2:468735) browserIterationFunc (TemplateTest.wasm.framework.unityweb:2:131228) runIter (TemplateTest.wasm.framework.unityweb:2:134320) Browser_mainLoop_runner (TemplateTest.wasm.framework.unityweb:2:132765)


    I've followed multiple other examples, as far as I could find them, but I've always seem to be getting these kind of errors.

    Does it have something to do with WebAssembly? is the .jslib file incorrect? How would I get this to work?

    Thanks in advance!
     
    Last edited: Dec 2, 2018