Search Unity

WebGL builds and streamingAssetsPath

Discussion in 'Web' 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,654
    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,666
    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:
    42
    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,654
    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
    Feargus99 likes this.
  12. xujinm

    xujinm

    Joined:
    Dec 11, 2018
    Posts:
    13
    I have same issue like you,you can rebuild localhost path at parent folder of unity project
     
  13. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    These errors look like something that cannot be helped only by reading the error message. If you can make a test case available, maybe someone on the forums will be able to give a helpful tip?
     
  14. rc31

    rc31

    Joined:
    Mar 12, 2016
    Posts:
    11
    Hello,
    You can access StreamingAssset folder using UnityWebRequest.

    Code (CSharp):
    1. //Inside a couroutine
    2. string path = "StreamingAssets/myFile.txt"; //This works because index.html is in the same folder as StreamingAssets ?
    3. UnityWebRequest uwr = UnityWebRequest.Get(path);
    4. yield return uwr.SendWebRequest();
    Code (CSharp):
    1. uwr.downloadHandler.data
    gives you an array of bytes (usefull for loading image for example)
    Code (CSharp):
    1. uwr.downloadHandler.text
    gives you the content of the file

    Hope it helps.
     
    renem and SimsUnited like this.
  15. SimsUnited

    SimsUnited

    Joined:
    Nov 29, 2016
    Posts:
    1
    Hi rc31, I'm having problems getting AssetBundles from the StreamingAssets folder into my WebGL build, I get response code 200 but no data is downloaded...
    here's my code:

    Code (CSharp):
    1.  IEnumerator DownLoadAsset(assetName)
    2.     {
    3.         UnityWebRequest unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(Application.streamingAssetsPath);
    4.         yield return unityWebRequest.Send();
    5.         Debug.Log("responseCode:" + unityWebRequest.responseCode);
    6.         if (unityWebRequest.isDone)
    7.         {
    8.             ab = (unityWebRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
    9.             Instantiate(ab.LoadAsset(assetName) as GameObject, Vector3.zero, new Quaternion(0, 0, 0, 0));
    10.         }
    11.     }
    any help would be greatly appreciated!!!
     
    Last edited: Jul 9, 2021
  16. JoJoJoX

    JoJoJoX

    Joined:
    Jun 23, 2022
    Posts:
    52
    I think I know what happened.
    That's not like android or iOS on which files in streamingAssets folder is downloaded with main app bundle.
    On WebGL, they are not downloaded automatically and `Application.streamingAssetsPath` is local path when they are downloaded. And we need to download them first by ourselves then we can use `Application.streamingAssetsPath` & File IO to access them. Is it right?
     
  17. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    did you get around this? I am having the same issue.
     
  18. SubbuWipro

    SubbuWipro

    Joined:
    Feb 17, 2017
    Posts:
    3
    Code (CSharp):
    1. string path = Path.Combine(Application.streamingAssetsPath, name.ToLower());
    2.      
    3.         UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(path);
    4.         yield return request.SendWebRequest();
    5.  
    6.         if (request.isNetworkError || request.isHttpError)
    7.         {
    8.             Debug.LogError(request.error);
    9.         }
    10.         else
    11.         {
    12.                 // Get downloaded asset bundle
    13.                 AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
    14.  
    15.                 if (bundle == null)
    16.                 {
    17.                     Debug.Log("Failed to load AssetBundle!");
    18.                     yield break;
    19.                 }
    20.  
    21.                 GameObject prefab = bundle.LoadAsset<GameObject>(name);
    22.                 callback(prefab);
    23.  
    24.                 // Use the loaded AssetBundle here...
    25.                 bundle.Unload(false);
    26.          
    27.         }
    Its working for localhost webgl