I’ve run into a problem with calling Native JavaScript functions from my app code - I’ve marked it as a Bug but maybe I’m missing something and hoping someone can shine a light on the problem.
I need to make a call out to a JavaScript function on the page my Unity WebGL app runs on. I added a jslib file and dllimport the JS function in C# and call it from a function on a game object. I followed the instructions here: Unity - Manual: Interaction with browser scripting
After adding the call to my JS function I get errors. I am using the Unity FaceBook plugin in my app for authentication with FaceBook and I get an exception thrown in the FB Async Init call. This worked fine before adding the JavaScript function import but when I add the import for my JavaScript function then FB fails to initialize with an exception.
The Facebook plugin also does dllImports of JavaScript functions so my theory is there is something I need to do to import my function and allow the FaceBook SDK to import it’s functions. Or there’s some sort of collision between the two that I can’t quite figure out.
If I just remove the dllimport of my JavaScript function and rebuild and deploy my app it works. Adding just the dllimport causes the failure.
I’m having a hard time debugging this because when I try to switch to a developmental build (so I get full symbol table in the stack trace in the browser console) then I don’t get the exception and everything works. (Release-only bugs suck).
If I put the call to my JavaScript function just before we initialize the FB SDK then my function executes properly but then we get this exception and the app hangs. If I move the call to my JavaScript function to later in the app initialization so it happens after the FB SDK initialization then we still get this same error.
That’s what led to my theory and makes me think it has to do with how Unity/WebGL is importing the JavaScript function calls and I need to do something non-standard.
Here’s the error message I get in browser console when we hit the exception:
Uncaught TypeError: Cannot read properties of undefined (reading 'apply')
at Module._SendMessageString (webgl92.framework.js:3:323243)
at Object.ccall (webgl92.framework.js:3:10850)
at SendMessage (webgl92.framework.js:3:471)
at Object.sendMessage (webgl92.framework.js:3:20326)
at window.fbAsyncInit (webgl92.framework.js:3:19869)
at a.__wrapper.a.__wrapper (sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:63:825)
at sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:227:805
Module._SendMessageString @ webgl92.framework.js:3
ccall @ webgl92.framework.js:3
SendMessage @ webgl92.framework.js:3
sendMessage @ webgl92.framework.js:3
window.fbAsyncInit @ webgl92.framework.js:3
a.__wrapper.a.__wrapper @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:63
(anonymous) @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:227
setTimeout (async)
a.__wrapper.a.__wrapper @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:63
(anonymous) @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:227
setTimeout (async)
a @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:227
(anonymous) @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:230
j @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:24
k @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:24
n @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:24
(anonymous) @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:230
(anonymous) @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:231
(anonymous) @ sdk.js?hash=8b2a82a6642a0917259e8f50d6843caa:231
Line 6 has the call to window.fbAsyncInit which is the FB SDK initialization - which then fails and without authenticating the app is hung.
A few more details that might be relevant:
- Unity Version is 2021.3
- We are using AssetBundles for the build - I’ve tried doing the dllimport for the JS functions from objects loaded from the assetbundles and that didn’t change the behavior.
- FB SDK version is 14.1
Not sure what else might be relevant - but if anyone has any ideas on what to look into I’ll take any ideas at this point.