Search Unity

How to load streamingAssets json file in Android build?

Discussion in 'Scripting' started by lakequeen, Jul 5, 2019.

  1. lakequeen

    lakequeen

    Joined:
    Aug 11, 2015
    Posts:
    1
    I'm trying to load Json file from streamingAssetsPath in Android build, and my code is blow:

    Code (CSharp):
    1. IEnumerator loadPlayerData()
    2.     {
    3. dataPath = Path.Combine(Application.persistentDataPath, "PlayerDatabase.txt");
    4.         filePath = Path.Combine(Application.streamingAssetsPath, "PlayerDatabase.json");
    5.  
    6.         if (!File.Exists(dataPath))
    7.         {
    8. #if UNITY_ANDROID
    9.  
    10.             UnityWebRequest www = UnityWebRequest.Get(filePath);
    11.             yield return www.SendWebRequest();
    12.             string jsonString = www.downloadHandler.text;
    13.             File.WriteAllText(dataPath, jsonString);
    14.  
    15.             if (www.isNetworkError)
    16.             {
    17.                 Debug.Log("Error: " + www.error);
    18.             }
    19.             else
    20.             {
    21.                 Debug.Log("Received: " + www.downloadHandler.text);
    22.             }
    23. #endif
    24.         }
    and it logs "Error: Cannot connect to destination host", I can't load anything from it. I don't know where is wrong.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You forgot to add protocol specifier to the url, for local file system it is file:// and on android to get into jar you want somethig like jar://, check unity manual for scriptable assets for correct url prefixes for platforms.