accessing functions from javascript in Unity3d webgl

340 views
Skip to first unread message

Dan Savage

unread,
Mar 24, 2016, 1:28:12 PM3/24/16
to emscripten-discuss
Unity3d uses emscripten to run unity in javascript for webgl.  I'm trying to access functions that I wrote in unity from javascript but I get errors that it cannot find the functions.

I'm using the documentation examples of communicating found here
https://kripken.github.io/emscripte...l#interacting-with-code-direct-function-calls

I've tried the example of putting int_sqrt  into the code and then using _ underscore for direct call to the function. I also tried the ccall and cwrap.  The functions are not found using either of these methods.

Assertion failed: Cannot call unknown function int_sqrt (perhaps LLVM optimizations or closure removed it?)

The function is there because I can call it using a unity specific SendMessage command that unity included.
Why can't I see it using the underscore or cwrap commands?

Alon Zakai

unread,
Mar 24, 2016, 1:30:55 PM3/24/16
to emscripten-discuss
The int_sqrt function also needs to be exported, using the EXPORTED_FUNCTIONS option on the commandline, or the EMSCRIPTEN_KEEPALIVE macro in the source. Otherwise, the compiler removes code it thinks is not needed.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dan Savage

unread,
Mar 24, 2016, 1:56:23 PM3/24/16
to emscripten-discuss
So in this case unity3d is doing the compilations so I have no control over that. Unity has their own method for calling functions which works so the function must exist somewhere but maybe it's renamed or hidden.  I was hoping there might be a way to interact directly with the function.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

Alon Zakai

unread,
Mar 24, 2016, 2:22:44 PM3/24/16
to emscripten-discuss
Can't you add EMSCRIPTEN_KEEPALIVE in the C++ source where you wrote that int_sqrt function? See examples in the link from before.

To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

juj j

unread,
Mar 24, 2016, 6:05:35 PM3/24/16
to emscripte...@googlegroups.com
Unity wraps on top of the Emscripten mechanisms to interact with C# scripts and JavaScript. See here for instructions: http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html. Do the methods outlined there work for you?

If you are writing a Unity C/C++ plugin and want to call that from JS side, then you will likely need to use the EMSCRIPTEN_KEEPALIVE method that Alon mentions, if the function is not otherwise pinned down by any other function calls.
Reply all
Reply to author
Forward
0 new messages