Read the current URL

Hello,

Is it possible to know the current url, when an Unity game is loaded in a browser ? I’d like to read parameters added to the url.

For example: http://example.com/test.html?id=321654

There isn’t a Unity API call to get the URL of the host web page, but you can do it by adding a bit of JavaScript to the page. If you put this function into the element:-

function GetURL() {
  var unity = GetUnity();
  unity.SendMessage("URLObj", "SetMyURL", document.URL);
}

…then you can call it from Unity with a couple of functions:-

var pageURL: String;

function GetURL() {
  Application.ExternalCall("GetURL");
}

function SetMyURL(url: String) {
  pageURL = url;
}

Hopefully, you can see what is going on here. You call the function in the host page from Unity, then that function gets the page’s URL and calls Unity back with the result.

Ok, thanks for the tip !

Note that the current 3.0 version of the webplugin has a broken Application.ExternalCall. This is due to be fixed today (that’s 5th Oct 2010).

Its returning Null…

May be i was wrong with what i have done…

But i have done exactly as you have said but its returning NULL…