I’m trying to make my browser send a message to the unity game, I already got this to work when the user clicks a button on the web page, but my goal is for the function to execute as soon as the unity game has finished loading in the browser. I tried using the “callback” parameter of the embedUnity function, but it never executes. I have been following the example found here:
http://unity3d.com/support/documentation/Manual/Working%20with%20UnityObject.html
my web page (abridged) javascript looks similar to this:
if (typeof unityObject != "undefined") {
unityObject.embedUnity("unityPlayer", "Example.unity3d", 600, 450, null, null, onUnityLoaded);
}
function onUnityLoaded(result) {
if (result.success) {
setVariables();
alert("Successfully reached callback and set variables.");
}
else {
alert("Unity failed to load game.");
}
}
function setVariables() {
getUnityObject().SendMessage("Main Camera","setVariable","myArgument");
}
In my web page there is a button which calls the setVariables function when it is clicked, and that works. I also tried using UnityObject’s “AddLoadEvent” and “AddDomLoadEvent”, neither of those worked. I tried calling my “setVariables” function from the body “onLoad” event handler, but that does not work because the page finishes loading before the unity game is loaded. So the function “setVariables” was executed before the unity game was ready. So my question is:
Why is the callback function not working? How can I make it work?