Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

XmlReader for WebPlayer: MethodAccessException

Discussion in 'Editor & General Support' started by korsaat84, Apr 10, 2012.

  1. korsaat84

    korsaat84

    Joined:
    Apr 10, 2012
    Posts:
    2
    Hello community,

    I am reading some dialogues from an XML file using an XmlReader.
    Everything works fine in the editor and in the standalone build, but it just doesn't work for the web build. I know I need a WWW object for the web player to be able to access a file, so this is what I'm using for creating the reader.
    Code (csharp):
    1.  
    2. filePath = Application.dataPath + "/Dialogues/" + fileName;
    3. if (Application.isWebPlayer){
    4.    www = new WWW(filePath);
    5.    reader = XmlReader.Create(www.url, settings);
    6. }
    7.  
    Sadly, this is what I get :\

    I've tried to remove the "Dialogue" folder and place the XML file in the same folder as the .unity3d file, but nope.
    According to this I don't need a crossdomain.xml, since the file is on the same domain hosting the .unity3d file.

    I'm really clueless. Any hints?
     
  2. YorickVanVliet

    YorickVanVliet

    Joined:
    Nov 14, 2009
    Posts:
    22
    You shouldnt use XmlReader.Read, since it does networking or i/o access outside of Unity. You'll just have to do your WWW request, and then parse that to a XMLReader.

    Something like this (not tested):

    Code (csharp):
    1. IEnumerator loadXML()
    2. {
    3.   WWW webReq = new WWW(Application.dataPath + "/Dialogues/" + fileName);
    4.   yield return webReq;
    5.   XmlReader reader = XmlReader.Create(new StringReader(webReq.text));
    6. }
    7.  
     
    Last edited: Apr 10, 2012
  3. korsaat84

    korsaat84

    Joined:
    Apr 10, 2012
    Posts:
    2
    Thanks a lot, that was the right direction! :D
     
  4. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    That's correct; keep in mind that due to security reasons the webplayer runs in a sandbox, and as such has restricted access to the underlying filesystem. If you want to keep the file local and bundled with the rest of your game, convert the file(s) you wish to access into unity assets, make sure they are in the Resources folder (or create one), and load them using Resources.Load. On the other hand, if you want the file to reside on a web server, you should use the WWW class as has already been described.

    El Diablo
     
  5. zerocorp

    zerocorp

    Joined:
    Nov 29, 2012
    Posts:
    4
    hello that such I have been following your example and I find it very interesting. I have a question only when I try to compile I get these errors. Can you help me I would greatly appreciate it.
    $error.jpg
     
  6. zerocorp

    zerocorp

    Joined:
    Nov 29, 2012
    Posts:
    4
    hello that such I have been following your example and I find it very interesting. I have a question only when I try to compile I get these errors. Can you help me I would greatly appreciate it.
    $error.jpg
     
  7. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287