Search Unity

Launch a local webpage on a Windows machine?

Discussion in 'Scripting' started by bigkahuna, Dec 14, 2007.

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I'm using Application.OpenURL() to launch a webpage that exists on the user's hard drive. I can get this to work under OS X:

    Code (csharp):
    1. var newURL ="file://localhost" + Application.dataPath +"/../../MyFolder/" + "webpage/mypage.html";
    2. newURL = Regex.Replace(newURL, " ", "%20");
    3. Application.OpenURL(newURL);
    But why won't this work under Windows?

    Code (csharp):
    1. var newURL ="file://localhost" + Application.dataPath +"\\..\\MyFolder\\" + "webpage/mypage.html";
    2. newURL = Regex.Replace(newURL, " ", "%20");
    3. Application.OpenURL(newURL);
    I also tried "file:\\localhost" but that didn't work either. Can anyone help?
     
  2. JFo

    JFo

    Joined:
    Dec 9, 2007
    Posts:
    217
    Try "file:///C:/" (or whatever is your drive letter) instead of "file://localhost".

    BR,
    Juha
     
  3. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    The problem with that is that I won't know what the drive is, that's why I'm using Application.dataPath. The Windows code works fine when I use it for writing a screen shot or reading a data file, but launching a web page it doesn't work.
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    "localhost" tries to connect to a web server running on the machine. Most Windows users don't have webservers running on their machines though.

    What you should try is using Application.dataPath, convert backward slashes to forward slashes, and append "file:///" in front.
     
  5. JFo

    JFo

    Joined:
    Dec 9, 2007
    Posts:
    217
    OK. That was just a thought..

    Just curious (as I don't have currently access to Windows with Unity apps, but will be trying something like you soon), what is the value of string "Application.dataPath"? Does it contain the full path including drive letter in Windows?

    BR,
    Juha
     
  6. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
  7. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I've got everything but converting backwards slashes to forward slashes working. I tried this:

    Code (csharp):
    1. NewURL = Regex.Replace(newURL, "\", "%47");
    but it results in errors. I tried it without converting the backwards slashes and it seems to work with Firefox on Windows XP. Do you know if having mixed forward + backward slashes will be a problem for other browsers? If so, any hints on how to convert this?
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    "\" is an escape character, so I think that should be "\\".

    Edit: OK, that didn't work either...some problem with the regular expression syntax, apparently, which I don't know about....

    --Eric
     
  9. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Use the verbatim string literal @ to ignore escape characters.
    Code (csharp):
    1. myString.Replace(@"\", @"/"));
     
  10. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Also, for a bit more flexibility, you can make use of shell commands, like 'open', using System.Diagnostics.
    Code (csharp):
    1. switch (Application.platform)
    2.      {
    3.       case RuntimePlatform.WindowsPlayer:
    4.             System.Diagnostics.Process.Start(PathAndFileName);
    5.             break;
    6.       default:
    7.              System.Diagnostics.Process.Start(string.Format("open \"{0}\"", PathAndFileName));
    8.               break;
    9.      }
     
  11. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Thanks Shaun. Unfortunately, I'm getting errors when I try to use "@", is this only usable with C#? (I'm writing in Javascript).
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    OK, I figured it out...since \ is an escape character, you need \\ to actually represent a \, but since \ is also an escape character in regular expressions, you need \\ to match a \ there too. Got that? ;)

    Code (csharp):
    1. NewURL = Regex.Replace(NewURL, "\\\\", "/");
    --Eric
     
  13. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Eric to the rescue again! Thanks, works perfect!
     
  14. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Sorry about that - I should've mentioned it was C# only.
    Thanks Eric :)