How can you send variables to unity3d from the url?

Need to send variables in to unity3d from url.

Example: www.domain.com?varname=4

Then have access to varname in unity3d like a global variable.

thanks,

I'm not a network guru, but I think this might work. It would be a two-step process, the first, where your web-server software (for example, Django) breaks down the URL and gets the parameters from the URL.

Second, your Browser can communicate with the Unity executable through the SendMessage() function. This allows the Browser to pass information to Unity (and vice versa). So you would SendMessage(parameter) to pass it to Unity.

The SendMessage() page has a lot more information, including sample code and HTML, that should help you.

Update: just ran across a similar question here in Answers, with a very nice solution: Extract Arguments from URL. Basically, he suggests to use the Application.absoluteURL, which allows your Unity application to see the entire path string it was called from. Then you would just have to parse that string to get the variable.

I'd like to add my 5 cent to this, as I couldn't find any relevant example code to do exactly what I wanted to do.

Using Unity3D v.3.3 and building for webplayer, unity will make a ready WebPlayer.html file for you that embed your .unity file and everything works off the bat. To get url parameters from this setup using PHP do this:

  • copy WebPlayer.html to WebPlayer.php and open in your favorite editor
  • add this line to the very top of the php file:
<? $arg = $_GET["arg"] ? "?" . $_GET["arg"] : null; ?>
  • about line 15/16 find this code:
unityObject.embedUnity("unityPlayer", "WebPlayer.unity3d", 680, 600);
  • replace "WebPlayer.unity3d" with this:
"WebPlayer.unity3d<? echo $arg ?>"
string raw = Application.srcValue;
string[] tmp = raw.Split('?');
string myArguments = tmp[1]; // will give you "myArg1,myArg2,myArg3..."
// ..further parse myArgument as you like

This can probably be done in different ways, don't hesitate to comment some improvements..

thank it helped me a lot :slight_smile:

when passing two arguments , follow this

<? $arg = $_GET["gamename"] ? "?" . $_GET["gamename"] : null; ?> <? $arg = $_GET["id"] ? $arg."?" . $_GET["id"] : null; ?>

and follow the same steps !!

Application.srcValue and Application.absoluteURL doesn’t work (at least in Unity 4.3), as these values do not contain anything past the “.unity” bit in the URL.

I needed to read URL parameters myself, so I did some more work to find the solution and created a RequestParameters.cs class that does it for you (just add it to a gameObject and call the string GetValue(string key) function:

This uses Application.ExternalEval to run browser-side JavaScript, and pass the document.location.search value to Unity code through SendMessage().

It’s explained a bit more in detail in the article containing the RequestParameters.cs class.

Importing assets is simply done as dragging and dropping them onto the Unity window.

EDIT:
Sorry, I didn’t realize you meant receiving variables from websites. Disregard my post.

Re-post, sorry about that.