I recommend you read our docs on this, we provide plenty of examples and explanations, and if the docs don’t help, please reply to this thread again with what you tried that didn’t work!
Thanks for the quick reply @unityruba
I have tested this link and its working fine in case I wanted to invoke any message from js to unity : Unity - Manual: Call Unity C# script functions from JavaScript
But there is no example which shows how to call a function of .jslib from .jspre file
Also I tried something like this
function callMergeStrings() {
const str1 = 'Hello';
const str2 = 'World';
// Allocate memory for the strings
const str1Ptr = Module._malloc(lengthBytesUTF8(str1) + 1);
const str2Ptr = Module._malloc(lengthBytesUTF8(str2) + 1);
// Copy the strings to the allocated memory
stringToUTF8(str1, str1Ptr, lengthBytesUTF8(str1) + 1);
stringToUTF8(str2, str2Ptr, lengthBytesUTF8(str2) + 1);
// Call the mergeStrings function from MyLibrary.jslib
const mergedStringPtr = Module.ccall(
'SendMessageToUnity', // Name of the function in the .jslib file
'number', // Return type (pointer to a string)
['number', 'number'], // Parameter types (pointers to char arrays)
[str1Ptr, str2Ptr] // Arguments passed to the function
);
// Read the merged string from the pointer
const mergedString = Pointer_stringify(mergedStringPtr);
console.log('Received merged string:', mergedString);
// Free the allocated memory
Module._free(str1Ptr);
Module._free(str2Ptr);
Module._free(mergedStringPtr);
}
this is a function in a .jspre file which merge 2 strings and call a method “SendMessageToUnity” which exist in .jslib
But for some reason its not working.
I will gather some more info, but meanwhile can you provide me some hint or a solution for fixing the above problem.
Sorry for misunderstanding your original question! I’ll see if we can add even more examples to that page that cover this case.
Generally, the names merged into the library generates a function name prefixed with a _ (see this Emscripten note)
If they’re defined starting with a $ will have that character stripped and no underscore added.
With that being said, how did you define CoolFunction? can you share that code? did you do it exactly as the other thread?