My WebGL build using Unity 2021.2
I’m trying to get my jslib plugin to talk back to Unity but can’t even get it to return to Unity no matter what I try.
According to the documentation Unity - Manual: Interaction with browser scripting (unity3d.com)
I’ve edited the build index.html to include the myGameObject (myUnityInstance)
I’ve setup a button click to trigger the js code and then throws the error
ReferenceError: MyUnityInstance is not defined as _GetString
the js code
mergeInto(
LibraryManager.library,
{
GetString: function(){
var str = "This is a string";
var buffersize = lengthBytesUTF8(str) +1;
var buffer = _malloc(buffersize);
stringToUTF8(str, buffer, buffersize);
MyUnityInstance.SendMessage('JSUrl', 'JSReturnString', 'buffer');
return buffer;
}
}
);
the C# function
public void JSReturnString(string returnStr)
{
print("Returned String: " + returnStr);
}
index.html
var myUnityInstance = null;
script.src = loaderUrl;
script.onload = () => {
createUnityInstance(canvas, config, (progress) => {
progressBarFull.style.width = 100 * progress + "%";
}).then((unityInstance) => {
myUnityInstance = unityInstance;
loadingBar.style.display = "none";
fullscreenButton.onclick = () => {
unityInstance.SetFullscreen(1);
};
}).catch((message) => {
alert(message);
});
};