Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Can't use json file from StreamingAssets on Android and iOS

Discussion in 'Scripting' started by vorora, May 22, 2017.

  1. vorora

    vorora

    Joined:
    Mar 17, 2015
    Posts:
    34
    Hello, everyone. Now I'm trying to implement localization in my small project by learning from Unity Live Training.

    Unity Live Training:


    It works fine in editor, but when I build and test run on an Android device, it can't find the right path and replace the text with the localized one. I researched a lot on the internet and tried to fix it by myself, but sadly it didn't work. (I have 0 experience on json and streaming assets.) The following is the script in my project. I'm really not sure how to fix it. I'll appreciate it if anyone could help me. Thank you.




    Code (CSharp):
    1.     void LoadLocalizedText (string fileName) {
    2.         localizedTextDictionary = new Dictionary<string, string> ();
    3.         string dataAsJson;
    4.  
    5.         #if UNITY_EDITOR
    6.         string filePath = Path.Combine (Application.streamingAssetsPath, fileName);
    7.  
    8.         #elif UNITY_IOS
    9.         string filePath = Path.Combine (Application.streamingAssetsPath + "/Raw", fileName);
    10.  
    11.         #elif UNITY_ANDROID
    12.         string filePath = Path.Combine ("jar:file://" + Application.streamingAssetsPath + "!assets/", fileName);
    13.  
    14.         #endif
    15.  
    16.         if (File.Exists (filePath)) {
    17.  
    18.             #if  UNITY_EDITOR || UNITY_IOS
    19.             dataAsJson = File.ReadAllText (filePath);
    20.  
    21.             #elif UNITY_ANDROID
    22.             WWW reader = new WWW (filePath);
    23.             while (!reader.isDone) {
    24.             }
    25.             dataAsJson = reader.text;
    26.             #endif
    27.  
    28.             LocalizationData loadedData = JsonUtility.FromJson<LocalizationData> (dataAsJson);
    29.  
    30.             for (int i = 0; i < loadedData.items.Length; i++) {
    31.                 localizedTextDictionary.Add (loadedData.items [i].key, loadedData.items [i].value);
    32.             }
    33.             Debug.Log ("Data loaded, dictionary contains: " + localizedTextDictionary.Count + " entries");
    34.         } else {
    35.             Debug.LogError ("Cannot find file!");
    36.         }
    37.         isReady = true;
    38.     }
     
    DanarKayfi likes this.
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,598
    You shouldn't have to add /Raw and jar://file:// to streamingassetspath, it should already include that. (thus the purpose of using streamingassetspath)

    You may even consider putting your json in the Resource folder instead, as this will allow you to read the file with ReadAllText no matter what platform you are on. (just a suggestion, but the above should fix your issue)

    Edit: Correction on this. ReadAllText should be used for the persistantDataPath. Resources would use Resources.Load and load it in as a TextAsset (to which you can toString to get the json string out of).

    You can use log viewer on the asset store (free) https://www.assetstore.unity3d.com/en/#!/content/12047 to view your console on a device. Print out what application.streamingassetspath shows (this will also allow you to see your debugs quickly)
     
    Last edited: May 23, 2017
    vorora likes this.
  3. ra-martins13

    ra-martins13

    Joined:
    Aug 6, 2013
    Posts:
    2
    DanarKayfi likes this.
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  5. MarKotiKk

    MarKotiKk

    Joined:
    Jul 18, 2017
    Posts:
    1
    Hello !
    Did you solve the problem ? Because I'm trying to do it, but I don't really succeed.
    And I have one more question. Is anyone can help to make a save function of this code?
    Thank you
     
  6. gunjanapps

    gunjanapps

    Joined:
    Sep 2, 2015
    Posts:
    3
    Some how in android System.IO.File.Exists with StreamingAssets folder does not work


    Try it without File.Exists and it worked fine.

    filePath = Path.Combine(Application.streamingAssetsPath + "/", fileName);

    #if UNITY_EDITOR || UNITY_IOS
    dataAsJson = File.ReadAllText(filePath);

    #elif UNITY_ANDROID
    WWW reader = new WWW (filePath);
    while (!reader.isDone) {
    }
    dataAsJson = reader.text;
    #endif
     
    Turtwiggy and demid like this.
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,212
    System.IO classes can read files on a file system. StreamingAssets on Android is a directory inside APK, which is a zip file. System.IO classes can't read files from inside zip.
    UnityWebRequest and WWW can do that.
     
  8. ap_shreyas

    ap_shreyas

    Joined:
    Feb 11, 2018
    Posts:
    2
    Hi this is not working for me as well. Can someone pls tell me what is the final solution they got working? I'm trying to get this to work for Android and WebGL.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,598
    As mentioned, use www or UnityWebRequest
     
  10. ap_shreyas

    ap_shreyas

    Joined:
    Feb 11, 2018
    Posts:
    2
    Hello,
    Thanks for the reply. UnityWebRequest seems to be working partially on WebGL(it removes special characters on WebGL but shows all fine in unity game editor?). But its not working on Android. I tried printing the file path and its actually showing correct file path even then its not able to read it? Please take a look at the code below
    Code (CSharp):
    1. IEnumerator LoadLocalizedText(string fileName)
    2.     {
    3.         localizedText = new Dictionary<string, string>();
    4.         string filePath;// = Path.Combine(Application.streamingAssetsPath, fileName);
    5.         filePath = Path.Combine(Application.streamingAssetsPath + "/", fileName);
    6.         string dataAsJson;
    7.         if (filePath.Contains("://") || filePath.Contains(":///"))
    8.         {
    9.             debugText.text += System.Environment.NewLine + filePath;
    10.             UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(filePath);
    11.             yield return www.Send();
    12.             dataAsJson = www.downloadHandler.text;
    13.         }
    14.         else
    15.         {
    16.             dataAsJson = File.ReadAllText(filePath);
    17.         }
    18.         LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataAsJson);
    19.  
    20.         for (int i = 0; i < loadedData.items.Length; i++)
    21.         {
    22.             localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
    23.         }
    24.         isReady = true;
    25.     }
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,598
    So, I did a quick test to make sure I wasn't crazy and had no issues. The text printed out from the text file just fine.
    Note a couple of things I did. I used the UNITY_ANDROID platform dependent tags located here https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
    A better option for you vs checking the filepath itself.

    The other thing I did was use log viewer located here https://assetstore.unity.com/packages/tools/log-viewer-12047
    Note it's a bit outdated and will yell at you about a line in the code that references unity webplayer, but you can just delete the line and save the script and it will be fine. You can bring up log viewer on the android device to see errors, debug/print calls, and some other info.

    Code (CSharp):
    1.     IEnumerator testLoad()
    2.     {
    3.         string testPath = Path.Combine(Application.streamingAssetsPath, "testText.txt");
    4.         print(testPath);
    5. #if UNITY_ANDROID
    6.         UnityWebRequest www = UnityWebRequest.Get(testPath);
    7.         yield return www.SendWebRequest();
    8.         if (www.isNetworkError || www.isHttpError)
    9.             Debug.Log(www.error);
    10.         else
    11.             print(www.downloadHandler.text);
    12. #endif
    13.     }
    14.  
     
    ow3n likes this.
  12. tzel

    tzel

    Joined:
    Jan 9, 2017
    Posts:
    1
    Thank youuuu!!! I was getting crazzy trying to open a .json file on android. Its work 100%
     
  13. Turtwiggy

    Turtwiggy

    Joined:
    Jun 18, 2017
    Posts:
    4
    Solved this for me. File.Exists does not work with apk/obb split on android.