- Home /
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,
Answer by Cyclops · Mar 05, 2010 at 06:05 PM
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.
Answer by alph · Apr 13, 2011 at 11:11 AM
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 ?>"
in the url, call your game with something like this: http://myServer.com/pathToGame/WebPlayer.php?arg=myArg1,myArg2,myArg3...
now in unity:
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..
Great "workaround" Using u5 and Application.srcValue stops at the unity3d file path. I'm using Application.absolutePath ins$$anonymous$$d.
I had used this same way for my applications, but now I need to build the same app for WebGL. I'm trying to find a way without success until now. The application don't receive the params from URL but from a query in databases using the PHP. You know how to get the parameters in this case? Thanks!
Answer by Minato Namikaze · Mar 06, 2010 at 07:45 AM
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.
You missed the point of the question. The question refers to runtime data acquisition, not editor asset importing.
Answer by ArunRaj · Aug 17, 2011 at 06:25 AM
thank it helped me a lot :)
when passing two arguments , follow this
and follow the same steps !!
Your answer
Follow this Question
Related Questions
Hide public field on base class but show it on derived class 1 Answer
How to properly declare this variable 2 Answers
How do you clear a variable in the inspector? 4 Answers
variable doesn't change 1 Answer
Why the variable updates only twice? 1 Answer