Passing variables from webpage to game

This will be a little hard to explain correctly, but i’ll try. What i’m trying to do is simply pass a variable from the webpage to the Unity application.

For example: i would call the Unity web player with an additional PARAM statement.

<param name="variable" value="variable value" />

Then, in-game, i would be able to use this “variable”. A real example would be:

User goes to the webpage. Before going to the webpage that holds the Unity webplayer, he’s asked for he’s Name. The form will submit a POST and assign his Name to a POST variable. The webpage that holds the webplayer will call the POST variable inside the PARAM and assign the player name to the PARAM variable. Then, in-game, i would display the player name by using the assigned PARAM variable.

I suggest passing the variable to mysql database from php. Then access the data in the mysql database from Unity3d via the method from the high score script on the wiki.

Try Javascript communication to Unity.

http://unity3d.com/support/documentation/Manual/Unity%20Web%20Player%20and%20browser%20communication.html

You cannot use custom parameters like that as the Unity web player isn’t ‘aware’ of them in any way just yet. Your options are:

  1. Append your data to the end of the src value:

src=yourFile.unity3d?variable=somevalue

Then on content load you can read in the src value in particular and parse out your data. Note that in doing this you essentially kill any browser cache gains for your web player if the data changes often (if somevalue is different than before the content is re-downloaded).

  1. Follow DrHotbunz and have your content load and then ping your server for the same data (PHP, ASP, XML file, whatever) using the WWW class.

  2. Follow hgbimonti’s advice and use browser-content communication. When doing this make sure that you’re careful about timing and ensuring that the content is fully loaded first. If you blindly call from the page to the content immediately on page load the game might not even be up and running to receive it (best to call from within the content first).

For using SendMessage - as described in link above - I presume that if the message is sent before the unity content’s loaded, it’d be a silent fail? If so, how do you know when the Unity content has finished loading?

A good way to do this is to have the Unity player call a function in the webpage as soon as it is up and running (in an object’s Awake function, say). This function simply calls SendMessage to pass the desired value back from the page to Unity.

Incidentally, that old wiki page you linked above might be updated. The auto-gen code doesn’t use

getObjectById(“UnityContent”)

but instead uses

getObjectById(“unityPlayer”)