Search Unity

Android path slash prepended

Discussion in 'Scripting' started by atelios, Sep 11, 2019.

  1. atelios

    atelios

    Joined:
    Dec 5, 2017
    Posts:
    26
    Excuse my previous thread, i didn't know how to delete it.

    I have a strange issue where I just use the streamingassetpath to load a file, but it appears as though a slash is being prepended.

    Code (CSharp):
    1.     public static readonly string LANG_DIR      = Application.streamingAssetsPath + Path.DirectorySeparatorChar + "Languages";
    Code (CSharp):
    1.    isoCode = enumToISO639(langType);
    2.         string languageFile = Path.Combine(Config.LANG_DIR + String.Format(Path.DirectorySeparatorChar + "{0}.json", isoCode.ToUpper()));
    3.         File.ReadAllText(languageFile);
    4.         addTranslationKeys(File.ReadAllText(languageFile));

    Code (CSharp):
    1. Could not find a part of the path "/jar:file:/data/app/com.oculus.ClickVRPlayerDev-M_ZpiDMPbfCCiAW0_-xpCw==/base.apk!/assets/Languages/ENG.json".
    2.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  3. atelios

    atelios

    Joined:
    Dec 5, 2017
    Posts:
    26
    I can't remove the starting slash because i tried it with substring and it will just start changing the "/jar:file" to "/ar:file", so the first / is added by something external?

    I already use path combine and I don't see why loadalltext wouldn't work as I also used a different method of loading the file using streamreader and that didn't work either and gave the same path error.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    The link I gave you tells you how you load from streaming assets on Android and explains why you have to do it that way.

    I don't know where your starting slash is coming from.
     
  5. atelios

    atelios

    Joined:
    Dec 5, 2017
    Posts:
    26
    I tried doing it like this but to no avail

    Code (CSharp):
    1. private void initTranslations() {
    2.  
    3.         isoCode = enumToISO639(langType);
    4.         string languageFile = Path.Combine(Config.LANG_DIR + String.Format(Path.DirectorySeparatorChar + "{0}.json", isoCode.ToUpper()));
    5.  
    6.         try {
    7. #if UNITY_EDITOR
    8.             addTranslationKeys(File.ReadAllText(languageFile));
    9. #elif !UNITY_EDITOR && UNITY_ANDROID
    10.             readFileAndroid(languageFile);
    11. #endif
    12.         } catch (Exception e) {
    13.             if (Debug.isDebugBuild) Debug.Log("Failed to load language file");
    14.         }
    15.     }
    16.  
    17.     private IEnumerator readFileAndroid(string path) {
    18.         UnityWebRequest request = new UnityWebRequest(path);
    19.         yield return request.SendWebRequest();
    20.         addTranslationKeys(request.downloadHandler.text);
    21.     }
    for now i will just put the json lines into a static class because this just doesn't appear to be working
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    You can put a JSON file in your Resources directory for this. This will work on all platforms, and in the editor.

    e.g. for Resources\language.json:
    Code (CSharp):
    1. var textAsset = Resources.Load<TextAsset>("language");
    2. string text = textAsset.text;
    3.