Reading URL Parmeters in Unity

This is how you use it:

  • Download the RequestParameters class.
  • Add it to your project and attach it to a GameObject in your scene.
  • Use PreviewLabs.RequestParameters.HasKey(string key) and PreviewLabs.RequestParameters.GetValue(string key) anywhere in your code to access URL parameters.
  • Test it in the Editor by setting the test parameters string in the inspector window.

Some examples of what it could be used for:

  • Creating a Unity Web Player e-card where you set the name of the recipient in the URL (this is the reason why I wrote the script; see holidaysCard.html?name=RequestParameters.cs%20User).
  • Passing variables from one web player build to an other when using Application.OpenURL()
  • Recognizing a user already logged in on your website.

This is the line of code doing the trick without requiring to modify the Web Player build’s HTML file each time a build is made:

    + name + "',"
    + "'SetRequestParameters'"
    + ",;");

How it works:

  • 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 undocumented UnityObject2.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).
  • SendMessage is used to pass the value of (browser-side JavaScript code to get the part of the URL from the question mark).
  • The string is parsed and turned into a Dictionary, allowing easy and efficient retrieval of the parameters.
  • WWW.UnEscapeURL() is used to convert from percent encoding used in URLs to normal strings (so space characters for instance are transformed from “%20” to a regular space).

The PreviewLabs.RequestParameters class is public domain, so you can use it any way you want to, modify it, use it in a commercial product, …

10 thoughts on “Reading URL Parmeters in Unity”

  1. First of all, thanks for the new years present Bernard!

    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?

    Well, any help is welcome (:

    Thanks again!

  2. Problem solved!

    Calling the RequestParameters.cs from JavaScript doesn’t work on Start() (I’m curious why). But if you put it on Update() (with a bool to run it just once) it works perfectly!

    Bernard, once again, thanks for your help!

  3. Chris says:

    Is there a way to do the opposite (sort of)? From the Unity Web Player change the current URL as a very simple way to save a game (the user would only have to bookmark the URL).

    One could easily show the URL in-game, but it would be a lot smoother if the user wouldn’t need to copy+paste, then bookmark.

  4. Yes, this would be possible in more recent browsers (Chrome, Safari, FF4+, and IE10pp4+), according to this answer on Stack Overflow.

    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).

  5. Chris says:

    Thanks Bernard. It’s working!

    I use

            " UnityObject2.instances[0].getUnity().SendMessage('" + 
            name + 
            "', 'SetBaseURL', document.location.href);"

    to get current URL (I split any parameters and discard, then save base URL to *url*).


            "window.history.replaceState", "-", "-", 

    to change the browser URL.

  6. Jim Kiggens says:

    Greetings Bernard,

    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?

    Warm regards,
    Jim Kiggens

  7. Bill says:

    Works well – thanks.

    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.

    var rp = GetComponent(typeof(PreviewLabs.RequestParameters)) as PreviewLabs.RequestParameters;
    var whatever=PreviewLabs.RequestParameters.GetValue(“whatever”);
    Debug.Log(“whatever is “+whatever);

  8. Fictional says:

    Is it possible to know the current url and print as string, when an Unity game is loaded in a browser? I want something like this:
    string pageUrl; //Current web page url

    void OnGUI(){
    GUI.Label (new Rect(10,10,200,50),”You play on: ” + pageUrl + “now.”);

    Is it possible? Thanks a lot.

  9. saftschachtel says:

    Thank you for sharing!

    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.

  10. cesar says:

    Thanks for this script!

    can you upload or send by email any full sample of use. i have some problems in implementation of it.

    Many thanks!!

Leave a comment

Your email address will not be published. Required fields are marked *