- Home /
WebGL + JSLIB Plugin, using functions
I've written a .jslib file in my /Assets/Plugins/WebGL folder that I am successfully calling back and forth with my C# code (using [DllImport ("__Internal")] in C# and SendMessage in JS). The only problem I'm running into is I have not found a way that I can add a function to my Javascript object that I can call from another JS function.
Ex.
var WebInteraction = {
func1: function()
{
console.log("do func1 stuff");
func3();
},
func2: function(str)
{
console.log("do func2 stuff str=" + str);
func3();
},
func3: function()
{
console.log("do func3 stuff");
}
};
mergeInto(LibraryManager.library, WebInteraction);
Code similar to the above example is giving me a func3 doesn't exist error. I also looked in the .js file that Unity outputs and confirmed func3 was not exported. I do see func1 and func2 as _func1() and _func2().
Is there a way to add functions into the jslib that are not intended to be called from the C# code?
I've also tried calling the function like WebInteraction.func3(), as well as moving the function out of the WebInteraction object, but both did not get the compiler to export the func3().
Answer by Roy Sharon · Sep 23, 2015 at 11:54 AM
You need to add dependencies declaration so emscripten does not strip out func3:
func1__deps: ['func3'],
func2__deps: ['func3']
There's a tool that can do this for you automatically: https://www.npmjs.com/package/emscripten-library-generator
@ Roy Sharon i m making an openGl project , in which user have to open file dialog and manipulate some work on it , but unity does not provide any builtin way to do it ,
found this link webgl-interactingwithbrowserscripting could not found any way to declare veritable in jslib , is there any document to study this ...?
mergeInto(Library$$anonymous$$anager.library, {
foo: 'some arbitrary value',
printFoo__deps: ['foo'],
printFoo: function() {
console.log(_foo); // notice that foo is preceded with an underscore
}
});
Answer by sonxoans2 · Sep 14, 2016 at 12:34 PM
When i use js plugin run by webgl with https. I got error: "Uncaught ReferenceError: SocketSend is not defined"
Jslib: var LibraryWebSockets = { $webSocketInstances: [],
SocketCreate: function(url)
{
var str = Pointer_stringify(url);
var socket = {
socket: new WebSocket(str),
buffer: new Uint8Array(0),
error: null,
messages: []
}
socket.socket.binaryType = 'arraybuffer';
socket.socket.onmessage = function (e) {
// Todo: handle other data types?
if (e.data instanceof Blob)
{
var reader = new FileReader();
reader.addEventListener("loadend", function() {
var array = new Uint8Array(reader.result);
socket.messages.push(array);
});
reader.readAsArrayBuffer(e.data);
}
else if (e.data instanceof ArrayBuffer)
{
var array = new Uint8Array(e.data);
socket.messages.push(array);
}
};
socket.socket.onclose = function (e) {
if (e.code != 1000)
{
if (e.reason != null && e.reason.length > 0)
socket.error = e.reason;
else
{
switch (e.code)
{
case 1001:
socket.error = "Endpoint going away.";
break;
case 1002:
socket.error = "Protocol error.";
break;
case 1003:
socket.error = "Unsupported message.";
break;
case 1005:
socket.error = "No status.";
break;
case 1006:
socket.error = "Abnormal disconnection.";
break;
case 1009:
socket.error = "Data frame too large.";
break;
default:
socket.error = "Error "+e.code;
}
}
}
}
var instance = webSocketInstances.push(socket) - 1;
return instance;
},
SocketState: function (socketInstance)
{
var socket = webSocketInstances[socketInstance];
return socket.socket.readyState;
},
SocketError: function (socketInstance, ptr, bufsize)
{
var socket = webSocketInstances[socketInstance];
if (socket.error == null)
return 0;
var str = socket.error.slice(0, Math.max(0, bufsize - 1));
writeStringToMemory(str, ptr, false);
return 1;
},
SocketSend: function (socketInstance, ptr, length)
{
//alert('start SocketSend');
//var socket = webSocketInstances[socketInstance];
//socket.socket.send (HEAPU8.buffer.slice(ptr, ptr+length));
var socket = webSocketInstances[socketInstance];
if (socket.socket.readyState === 1) {
socket.socket.send (HEAPU8.buffer.slice(ptr, ptr+length));
}else{
//var m_that = this;
// optional: implement backoff for interval here
setTimeout(function () {SocketSend(socketInstance, ptr, length);}, 1000);
}
},
SocketRecvLength: function(socketInstance)
{
var socket = webSocketInstances[socketInstance];
if (socket.messages.length == 0)
return 0;
return socket.messages[0].length;
},
SocketRecv: function (socketInstance, ptr, length)
{
var socket = webSocketInstances[socketInstance];
if (socket.messages.length == 0)
return 0;
if (socket.messages[0].length > length)
return 0;
HEAPU8.set(socket.messages[0], ptr);
socket.messages = socket.messages.slice(1);
},
SocketClose: function (socketInstance)
{
var socket = webSocketInstances[socketInstance];
socket.socket.close();
},
SocketAlert: function(msg){
alert(msg);
}
};
autoAddDeps(LibraryWebSockets, '$webSocketInstances');
mergeInto(LibraryManager.library, LibraryWebSockets);
somebody tell me how to fix this, please...
sonxoans2, just change memory size to 256 (default). If you increase value -> you see this bug
Answer by DanielSafs · Jan 17, 2019 at 02:47 PM
" I also looked in the .js file that Unity outputs "
What is this file name? Although my functions are working fine. I tried to find my functions in UnityLoader.js but had no success.
If you are using chrome, in the dev tools. in the source tab, you could ctrl+shift+F to search in all sources, and try to find the function name you are looking for. It worked for me.
In my case the function was defined in a blob:, where url was the address from where i loaded my webgl game.
Your answer

Follow this Question
Related Questions
Can't connect to Web socket, connection refused Unity WebGL 0 Answers
WebGL builds: decipher jsStackTrace errors with symbol map? 1 Answer
Browser interaction to Unity 0 Answers
Unity WebGL Problem 0 Answers
ReferenceError: Call is not defined 1 Answer