- Home /
Question by
PizzaGuy213 · Dec 02, 2018 at 09:27 PM ·
javascriptwebgl
Can't get WebGL to Interact with browser scripting
Hi all.
I keep getting errors when trying to interact with browser scripting.
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:
mergeInto(LibraryManager.library, {
Hello: function () {
window.alert("Hello, world!");
},
HelloString: function (str) {
window.alert(Pointer_stringify(str));
},
PrintFloatArray: function (array, size) {
for(var i = 0; i < size; i++)
console.log(HEAPF32[(array >> 2) + i]);
},
AddNumbers: function (x, y) {
return x + y;
},
StringReturnValueFunction: function () {
var returnStr = "bla";
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
return buffer;
},
BindWebGLTexture: function (texture) {
GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
},
});
I got a gameobject containt a script called NewBehaviourScript:
using UnityEngine;
using System.Runtime.InteropServices;
public class NewBehaviourScript : MonoBehaviour {
[DllImport("__Internal")]
private static extern void Hello();
[DllImport("__Internal")]
private static extern void HelloString(string str);
[DllImport("__Internal")]
private static extern void PrintFloatArray(float[] array, int size);
[DllImport("__Internal")]
private static extern int AddNumbers(int x, int y);
[DllImport("__Internal")]
private static extern string StringReturnValueFunction();
[DllImport("__Internal")]
private static extern void BindWebGLTexture(int texture);
void Start() {
Hello();
HelloString("This is a string.");
float[] myArray = new float[10];
PrintFloatArray(myArray, myArray.Length);
int result = AddNumbers(5, 7);
Debug.Log(result);
Debug.Log(StringReturnValueFunction());
var texture = new Texture2D(0, 0, TextureFormat.ARGB32, false);
BindWebGLTexture(texture.GetNativeTextureID());
}
}
But when building and running the WebGL player I get this error:
An error occurred running the Unity content on this page. See your browser JavaScript console for more info. The error was:
uncaught exception: abort(-1) at jsStackTrace (TemplateTest.wasm.framework.unityweb:2:22127)
stackTrace (TemplateTest.wasm.framework.unityweb:2:22298)
onAbort@file:///D:/Thom/Unity/Arti/Projects/Builds/TemplateTest/Build/UnityLoader.js:4:10645
abort (TemplateTest.wasm.framework.unityweb:2:481204)
_Hello (TemplateTest.wasm.framework.unityweb:2:46724)
@blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[27384]:0x8ad47d (blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[26720]:0x8a0acf)
@blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 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/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[24271]:0x84f271 (blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[24625]:0x85c523)
@blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[1925]:0x9a32d (blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[1924]:0x9a296)
@blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[3415]:0x100cf7 (blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[3410]:0x100a05)
@blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[6314]:0x267100 (blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[4101]:0x16ac7f)
@blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[7056]:0x2b9599 (blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[6409]:0x270d9f)
@blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[6409]:0x270db6 (blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[6404]:0x26ffcc)
@blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 line 2 > WebAssembly.instantiate:wasm-function[6398]:0x26e852 (blob:null/6a898df7-bfc2-4be5-b2d9-be16afff3d52 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!
Comment