- Home /
Can't get SendMessage in webpage to send info to the web player
Can't get SendMessage in webpage to send info to the web player
I am trying to get the containing webpage to send information to the unity web player with no luck.
I followed what they have here: http://docs.unity3d.com/Manual/UnityWebPlayerandbrowsercommunication.html
But I read other comments on this and found that people advise making a call to a function on the containing webpage from the web player through the start function, to make sure it's loaded. Then I have that function execute SendMessage to send a text string back to the web player.
In the function (MyFunction) in Unity I just have it set a String variable to the text that is sent by SendMessage, then in OnGUI, I want it to display that value in a box, but I'm not able to get the value from SendMessage and assign it to the variable in my unity script.
Here is what I have:
In Unity:
myObjectScript.js ( this is attached to an object named MyObject)
#pragma strict
var myMessage : String = "testing..";
function Start ()
{
Application.ExternalCall("DoSomething");
}
function OnGUI ()
{
GUILayout.Box("myMessage: " + myMessage);
}
function MyFunction(param : String)
{
myMessage = param;
}
In the html file: (See the function DoSomething at the bottom. The rest is copied from the code generated by unity for the html file.)
var config = {
width: 960,
height: 600,
params: { enableDebugging:"1" }
};
var u = new UnityObject2(config);
jQuery(function() {
var $missingScreen = jQuery("#unityPlayer").find(".missing");
var $brokenScreen = jQuery("#unityPlayer").find(".broken");
$missingScreen.hide();
$brokenScreen.hide();
u.observeProgress(function (progress) {
switch(progress.pluginStatus) {
case "broken":
$brokenScreen.find("a").click(function (e) {
e.stopPropagation();
e.preventDefault();
u.installPlugin();
return false;
});
$brokenScreen.show();
break;
case "missing":
$missingScreen.find("a").click(function (e) {
e.stopPropagation();
e.preventDefault();
u.installPlugin();
return false;
});
$missingScreen.show();
break;
case "installed":
$missingScreen.remove();
break;
case "first":
break;
}
});
u.initPlugin(jQuery("#unityPlayer")[0], "webcomm.unity3d");
function DoSomething()
{
u.getUnity().SendMessage("MyObject", "MyFunction", "Hello!");
}
});
I tried adding the (), but that didn't work. Also, in the manual they just have it in there without the (). I looked in my web console and it is saying "ReferenceError: DoSomething is not defined". Is there somewhere else where this needs to be defined?
Answer by Ciix · Feb 18, 2015 at 09:33 PM
Ok I got it now. I had to move DoSomething() outside of jQuery(function() {
....
});
Your answer
Follow this Question
Related Questions
Change Web Player Size? 1 Answer
Web Player Auto Update Failed 8 Answers
unity web player has stopped responding 0 Answers
Failed to Update Unity Web Player on multiple browsers 0 Answers
Regarding PlayerPrefs vrs. (WebPlayer && Standalone) Builds 1 Answer