- Home /
UnityObject callback function never runs
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?
just for clarification, the "onUnityLoaded" function is never reached. Neither of the alert messages that function contains appear when I go to the page.
Answer by Bunny83 · Oct 05, 2011 at 07:54 PM
Javascript or JScript is an immediate dynamic language, it isn't compiled. So when the browser reaches the embed line, onUnityLoaded is undefined because you create that function after that line. I guess that's your problem ;)
Just move the function in front of your embed line or use the just-in-place-method like in the example ;)
I have tried both of your suggestions, neither worked. I found out that the callback function is being executed, but it runs immediately ins$$anonymous$$d of waiting for the unity player to finish loading. I think this is a bug in UnityObject.
Finish loading? If you expect it should run when the player has finished loading your scene / game then you're wrong ;)
It's an automatic callback of the player itself which fires when the player is loaded / started. Your game / content is streamed and during the strea$$anonymous$$g the player is already loaded / started.
If you want an event in JScript that fires when your scene is loaded, do it yourself from Unity via browser communication
Answer by msknapp · Oct 06, 2011 at 02:51 PM
I think the workaround for this problem is to have the unity application notify the web page when it is done loading. The callback function always runs before the unity player has finished loading.
In my unity application I added this line to a start function:
Application.ExternalCall("onUnityLoaded","");
and in my web page I wrote a function for "onUnityLoaded" that will configure the unity game, similar to the one in my question.
This has fixed my problem.
Your answer
Follow this Question
Related Questions
communication between unity and browser using webRTC 0 Answers
WebPlayer embedding question 1 Answer
ExternalCall in WebGL not working 0 Answers
Browser to unity communication 2 Answers
Embed Web Browser into unity 4 Answers