Search Unity

WebGL builds and streamingAssetsPath

Discussion in 'WebGL' started by ekatzenstein1, Nov 6, 2015.

  1. ekatzenstein1

    ekatzenstein1

    Joined:
    Feb 13, 2015
    Posts:
    5
    I have a Unity build that references a .json file. This file is to be kept in the streamingAssets folder after the build. The build works great on the desktop when I use the line of code below to call the file in my script (using LitJson):

    Code (CSharp):
    1.  jsonString = File.ReadAllText(Application.streamingAssetsPath + "/quatData.json");
    However, when building with WebGL, I get an error in the console that reads:

    The path in the console is almost the correct location for quatData.json, but I believe that the slash in front of the http would cause the link to fail. Any ideas why this isn't working or why there is a slash after the build?
     
  2. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,653
    Which version of Unity ?
     
  3. ekatzenstein1

    ekatzenstein1

    Joined:
    Feb 13, 2015
    Posts:
    5
    Version 5.2.2f1 Personal
     
  4. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,633
    See the docs for Application.streamingAssetsPath: http://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html

    The example code on that page should work for WebGL.
     
    mviranyi-here likes this.
  5. ben06feb

    ben06feb

    Joined:
    Oct 2, 2012
    Posts:
    28
    Hi,

    Its not working for me, I am using Unity 5.3.5 please find the code here, In editor its working fine.

    Code (CSharp):
    1. public string filePath = Application.streamingAssetsPath + "/UserDetails.xml";
    2.     public string result = "";
    3.  
    4.     IEnumerator userDetailsXmlPath()
    5.     {
    6.         print (filePath);
    7.  
    8.         if (filePath.Contains ("://") || filePath.Contains (":///")) {
    9.             WWW www = new WWW (filePath);
    10.             yield return www;
    11.             result = www.text;
    12.  
    13.             print (result);
    14.             FetchUserDetails ();
    15.         } else {
    16.             result = File.ReadAllText (filePath);
    17.  
    18.             print (result);
    19.             FetchUserDetails ();
    20.         }
    21.     }
    22.  
    23.     public void FetchUserDetails()
    24.     {
    25.         XmlDocument userXml1 = new XmlDocument ();
    26.     //    userXml1.Load(Application.streamingAssetsPath + "/UserDetails.xml");
    27.     //    userXml1.Load(Application.dataPath + "js/UserDetails 1.xml");
    28.     //    userXml1.LoadXml(userXml.text);
    29.  
    30.         userXml1.LoadXml(result);
    31.  
    32.  
    33.         XmlNodeList userList = userXml1.GetElementsByTagName ("user");
    34.  
    35.         foreach(XmlNode userValue in userList)
    36.         {
    37.             XmlNodeList userContent = userValue.ChildNodes;
    38.             objUser = new Dictionary<string, string>();
    39.  
    40.             foreach(XmlNode value in userContent)
    41.             {
    42.                 objUser.Add (value.Name, value.InnerText);
    43.             }
    44.  
    45.             userFullDetails.Add (objUser);
    46.  
    47.             userCountInXml = userList.Count;
    48.  
    49.             userId = new string[userList.Count];
    50.             questionSetOfUser = new string[userList.Count];
    51.         }
    52.  
    53.         AssignUserXmlValuesToArray ();
    54.     }
     

    Attached Files:

  6. vinit_ingle

    vinit_ingle

    Joined:
    Jun 20, 2013
    Posts:
    2
    I've got the same issue. Only difference being that in my case i am reading data from a '.txt' file. While searching for a solution for this issue, i read somewhere that this is because WebGL builds do not support System.IO operations due to security reasons. It would be very helpful if one of the UNITY guys could confirm me on this and also help on this issue.
     
  7. anmu90

    anmu90

    Joined:
    May 30, 2017
    Posts:
    1
    I've the same issue!
     
  8. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,653
    correct, there is no access to the file system for security reasons. You need to use WWW class to download the file before being able to parse it.
     
  9. hacaro76

    hacaro76

    Joined:
    Oct 14, 2015
    Posts:
    25
    Hi all, i'm Working on a Multiplatform Project, Including WebGL. The project move around 3 different scenes. Before load other scene i use Easy Save 2 asset for saving some object, materials, etc. Easy save 2, uses File on Desktop Platforms and Playerprefs on WebGL. all works fine on editor and Standalone, but on WebGL, I receive the playerprefs exception. probably because i'm over the limit of 1mb for the Playerprefs.

    At the moment i'm stuck because i've not found any solution to fix this. and so,
    Someone can give me some suggestions, solution to jump this big obstacle ?

    Thanks in advance and Best regards -
     
  10. GilesDMiddleton

    GilesDMiddleton

    Joined:
    Aug 12, 2015
    Posts:
    91
  11. ishwara_bhat

    ishwara_bhat

    Joined:
    Oct 18, 2018
    Posts:
    11
    Could not find a part of the path "/http:/localhost:3333/StreamingAssets/xyv.json".

    one slash comes before http:. The path is fine. Browser is chrome. I think Chrome supports file system?. I suspect some project setting getting corrupt.. If anyone else has any ideas, it would be great!

    I have exact same issue with Unity WebGL while using 2018.3 and also 2019.
     
    Last edited: Jul 29, 2019