Unity 2020.1 sendMessage no longer works from browers JS
The new version does not have instantiate in the .js file. There is no this line
var unityInstance = UnityLoader.instantiate("unityContainer", "Build/YourBuild.json", {onProgress: UnityProgress});
If I try unityInstance.SendMessage("ObjectName","methodname",value);
The console in brower reports unityInstance not defined.
How do I send message from browser to webgl now?
Answer by josepaternina · Nov 02, 2020 at 10:04 AM
Hi, If you want to create your own web template: https://docs.unity3d.com/2020.1/Documentation/Manual/webgl-templates.html][1]
Go to Unity WebGL templates folder (the path folder is in the link above) and I copied default template folder to my project in Assets/WebGLTemplates/MyFirstTemplate folder.
Unity WebGL has this code by default:
var unityGame; //This mine
var script = document.createElement("script");
script.src = loaderUrl;
script.onload = () => {
createUnityInstance(canvas, config, (progress) => {
progressBarFull.style.width = 100 * progress + "%";
}).then((unityInstance) => {
unityGame = unityInstance; //This mine
loadingBar.style.display = "none";
fullscreenButton.onclick = () => {
unityInstance.SetFullscreen(1);
};
}).catch((message) => {
alert(message);
});
};
The var unityGame;
and the unityGame = unityInstance;
lines are mine, you can use the variable name you want, with this I ensure the var is instantiated properly wihen the Unity Engine be ready.
And finally use this to send messages to my function MyHelloPublicFunction in my c# file "whatever.cs":
unityGame.SendMessage( "MyObjectCreatedInUnityEngine", "MyHelloPublicFunction", "Hello world" );
[1]: https://docs.unity3d.com/2020.1/Documentation/Manual/webgl-templates.html,Hi, If you want to create your own web template: https://docs.unity3d.com/2020.1/Documentation/Manual/webgl-templates.html][1]
Go to Unity WebGL templates folder (the path folder is in the link above) and I copied default template folder to my project in Assets/WebGLTemplates/MyFirstTemplate folder.
Unity WebGL has this code by default:
var unityGame; //This mine
var script = document.createElement("script");
script.src = loaderUrl;
script.onload = () => {
createUnityInstance(canvas, config, (progress) => {
progressBarFull.style.width = 100 * progress + "%";
}).then((unityInstance) => {
unityGame = unityInstance; //This mine
loadingBar.style.display = "none";
fullscreenButton.onclick = () => {
unityInstance.SetFullscreen(1);
};
}).catch((message) => {
alert(message);
});
};
The var unityGame;
and the unityGame = unityInstance;
lines are mine, you can use the variable name you want, with this I ensure the var is instantiated properly wihen the Unity Engine be ready.
And finally use this to send messages to my function MyHelloPublicFunction in my c# file "whatever.cs":
unityGame.SendMessage( "MyObjectCreatedInUnityEngine", "MyHelloPublicFunction", "Hello world" );
[1]: https://docs.unity3d.com/2020.1/Documentation/Manual/webgl-templates.html