- Home /
PHP pass info to webplayer
Hello, I was just wondering if it was possible to generate a game ID in PHP.
I will try to explain it best I can.
Basically I was wondering if it was possible to set it up so that if the user visits: www.mydomain.com/MyGame/Game.php?id=1
Then it would set a variable in my webplayer stating that the user wants to join game with ID 1
Is this possible?
Answer by IgnoranceIsBliss · Apr 19, 2012 at 03:57 AM
There was a method of getting the URL from the Application object, but when I tried it I found it didn't work very well.
My preferred method is to communicate between my Unity Web Player and the Javascript in my web page.
My Unity Application
void Start()
{
Application.ExternalCall("GetID");
}
public void GiveID(string ID)
{
IDFromWebPage = ID;
}
My Web Page
function GetUnity() {
if (typeof unityObject != "undefined") {
return unityObject.getObjectById("unityPlayer");
}
return null;
}
function GetID()
{
//Set the ID...
var unity = GetUnity();
unity.SendMessage("Main Camera","GiveID","<?php echo $_REQUEST['id']; ?>");
}
So in this example, my Unity application uses Application.ExternalCall which calls a Javascript function named GetID in my web page.
That function then gets a handle to my Unity object and calls 'SendMessage' to call the GiveID function within my Unity project with the ID value I was interested in.
NOTE: In the example below, the Unity script is on the object named "Main Camera". You would need to alter your call to SendMessage to send the information to the appropriate Unity object.
I initiate the call from Unity rather than the web player because the Unity player takes some time to start up. Since Unity knows when you are going to need the data, it's easiest to start the exchange of data from the Unity side.