Search Unity

WebPlayer won't execute parts of code.

Discussion in 'Scripting' started by TehGM, Jan 4, 2014.

  1. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    So, first of all, I am rather experienced (not saying pro, but I'm quite good) C# programmer. I am learning Unity to get a hold on game development, so I can later program own game without any engine (this will allow me use ways of programming I got used to but Unity API won't allow me). It goes slowly (cause of my lazyness) but quite good imo, and then there I experienced this problem today:

    Basically I am upgrading displaying recent changes on main menu. I made it check for changelog file and parse data from it, and if it doesn't exist - download data from text file on a server. When running in editor, it works exactly how I made it work, but when I upload to ftp and open the build, it acts like it's skipping a whole method:

    Code (csharp):
    1.     void Start()
    2.     {
    3.         _cache = Camera.main.transform.rotation;
    4.         GetChangelog();
    5.     }
    6.  
    7.     private void GetChangelog()
    8.     {
    9.         if (System.IO.File.Exists("changelog.txt"))
    10.         {
    11.             blahh.text = "found file";
    12.             System.IO.StreamReader rd =
    13.                 new System.IO.StreamReader("changelog.txt");
    14.             string line = rd.ReadLine();
    15.             while (line != null  line.Trim().Length > 0)
    16.             {
    17.                 linesCount++;
    18.                 text += System.Environment.NewLine + line;
    19.                 line = rd.ReadLine();
    20.             }
    21.             rd.Close();
    22.         }
    23.         else
    24.         {
    25.             blahh.text = "didn't find file";
    26.             //downloading data from the server goes here
    27.                              ...
    (Note: I know it might not be the best way to do it, but it's how I got used to it while programming anything else in past few years, and it works well)

    blahh is reference to GUIText I added temporarily to check where the code stops executing. And the problem is, text on screen stays the same (therefore it skips the if and else statements).

    Maybe am tired and doing some damn stupid mistake, but well, I tried many things, even calling the method once in the begining of update (cause everything else in update works) and this still won't work. Any ideas?
     
    Last edited: Jan 4, 2014
  2. Deleted User

    Deleted User

    Guest

    for securty reasons you dont have access to your local filesystem from webplayer build! So System.IO is not available here.
    Have a look at the WWW class
     
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    The WebPlayer, because of its nature, is a fairly strict, sandbox-like environment.

    I'm not sure, but I doubt a Web Browser will let you access System.IO.

    If you're downloading a log from a server, use WWW class.

    (EDIT: lol, beaten by a few seconds)
     
  4. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    Yes, it's using the WWW class after. The point is, it won't execute anything in else {}
    (checking using GUIText, as I said)

    Probably should've either posted more code or cut confusing parts lol.
     
    Last edited: Jan 4, 2014
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    I don't know what happen at compile time to assembly that are not acceptable for deployment. Maybe the if and the else was just not compiled at all, but most likely some exception is being thrown when you reach it and the code stop executing.
     
  6. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    Ok, So I've wrapped the method call in try. Then compiled Windows executable, runs fine just like in the editor.
    Web player however catches an exception:
    Code (csharp):
    1. System.MethodAccessException: Attempt to access a private/protected method has failed.
    2. at System.Security.SecurityManager.ThrowException(System.Exception ex) [0x00000] in <filename unknown>:0
    3. at Menu.GetChangelog() [0x00000] in <filename unknown>:0
    4. at Menu.Start() [0x00000] in <filename unknown>:0
    I have changed GetChangelog flag to public, though I dunno what for since it should work if in exec it works just fine (and so, ofc it didn't fix anything).
     
    Last edited: Jan 4, 2014
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    I don't think the issue is with GetChangelog, but inside GetChangelog. Most likely all System.IO method are flagged private and are unreachable in the scope of a web browser.

    Get rid of System.IO if you want to deploy in a WebPlayer.
     
  8. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    Well, seems like it was easier than I thought. Aaaand also seems like I gotta get used to the API a bit more too, lol.
    Commented out for reuse in standalone once it gets too heavy for webplaying (y).
    Thanks, mate.