From inside the RequestParameters.cs class, Application.ExternalEval() is used to execute JavaScript in the browser, from the context of the html page.

Through the undocumentedUnityObject2.instances[0], we get access to the Unity Web Player object, allowing us to send data from the JavaScript context back to the player (discovered on this question on Stackoverflow).

10 thoughts on “Reading URL Parmeters in Unity”

I wanted to know if there anything else to setup (maybe Player Setting or the web server) to have this working on the web player. I tried out the script and it works fine on the Editor, but on the web player it doesn’t even recognize the key passed through the url.

One thing: I’m calling your script (C#) through a JavaScript. Can this be an issue?

The reason why it isn’t possible in earlier browsers is that other attempts to change the URL also force a page reload, which I assume you don’t want.

You’ll need to update the string in the Application.ExternalEval() call to make this possible.
Basically you’ll need to use the window.history.pushState function there to make it work (rather than modifying the value of window.location.href – which does force a page reload).

Your RequestParameters solution looks like it could be perfect for a need that I have. Unfortunately, I use UnityScript and have little experience with C# – and I have not been able to successfully call it within my project.

Would you possibly have an example that you could share that shows the RequestParameters.cs being called in another script to get the data from the URL?

For people writing javascript, put RequestParameters.cs into the Plugins folder so it gets compiled first, and then call it with this. (this assumes your javascript and RequestParameters.cs are on the same game object.

I had the same problem as Sandro. In my case I could access the parameters after the second frame. So I added an ‘isInitialized’ flag, that gets ‘true’ after the parameters got extracted. I check this parameter before accessing the values.