A
A
ArtemCheck2021-11-04 22:57:34
Android
ArtemCheck, 2021-11-04 22:57:34

Unity Json file not loading on Android. Specific file path?

Hello!
Help, my head is spinning.
The problem is, I made multilanguage in the game on unity using json files in the StreamingAssets folder. In the editor (editor) everything works cool, but when installed on the phone json Files do not load the text. After a whole day of googling, the thought appeared that this was due to the fact that it was necessary to somehow specifically upload files.
Tried a bunch of options, zero result.
Help, I'm attaching the code:

using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;

public class LocalizationManager : MonoBehaviour
{
    private string currentLanguage;
    private Dictionary<string, string> localizedText;
    public static bool isReady = false;

    public delegate void ChangeLangText();
    public event ChangeLangText OnLanguageChanged;

    private void Awake()
    {
        if (!PlayerPrefs.HasKey("Language"))
        {
            if (Application.systemLanguage == SystemLanguage.Russian || Application.systemLanguage == SystemLanguage.Belarusian)
            {
                PlayerPrefs.SetString("Language", "ru_RU");
            }else if (Application.systemLanguage == SystemLanguage.English)
            {
                PlayerPrefs.SetString("Language", "en_US");
            }
            else if (Application.systemLanguage == SystemLanguage.Ukrainian)
            {
                PlayerPrefs.SetString("Language", "uk_UA");
            }
            else if (Application.systemLanguage == SystemLanguage.Polish)
            {
                PlayerPrefs.SetString("Language", "pl_PL");
            }
            else if (Application.systemLanguage == SystemLanguage.German)
            {
                PlayerPrefs.SetString("Language", "de_DE");
            }
            else if (Application.systemLanguage == SystemLanguage.French)
            {
                PlayerPrefs.SetString("Language", "fr_FR");
            }
            else if (Application.systemLanguage == SystemLanguage.Portuguese)
            {
                PlayerPrefs.SetString("Language", "pt_PT");
            }
            else if (Application.systemLanguage == SystemLanguage.Spanish)
            {
                PlayerPrefs.SetString("Language", "es_ES");
            }else
            {
                PlayerPrefs.SetString("Language", "en_US");
            }
        }
        currentLanguage = PlayerPrefs.GetString("Language");
        LoadLocalizedText(currentLanguage);
    }

    public void LoadLocalizedText(string langName)
    {
        //string path = Application.streamingAssetsPath + "/Languages/" + langName + ".json";
        string dataAsJson;

        if (Application.platform == RuntimePlatform.Android)
        {
            string path = "file://" + Application.streamingAssetsPath + "/Languages/" + langName + ".json";
#pragma warning disable 618
            WWW reader = new WWW(path);
            while (!reader.isDone) { }

            dataAsJson = reader.text;
        }
        else
        {
            string path = Application.streamingAssetsPath + "/Languages/" + langName + ".json";
            dataAsJson = File.ReadAllText(path);
        }

        LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataAsJson);
        localizedText = new Dictionary<string, string>();
        for (int i = 0; i < loadedData.items.Length; i++)
        {
            localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
        }

        PlayerPrefs.SetString("Language", langName);
        
        isReady = true;

        if (OnLanguageChanged != null)
        {
            OnLanguageChanged.Invoke();
        }
    }

    public string GetLocalizedValue(string key)
    {
        if (localizedText.ContainsKey(key))
        {
            return localizedText[key];
        }
        else
        {
            throw new Exception("Localized text with key \"" + key + "\" not found");
        }
    }

    public string CurrentLanguage
    {
        get
        {
            return currentLanguage;
        }set
        {
            PlayerPrefs.SetString("Language", value);
            currentLanguage = PlayerPrefs.GetString("Language");
            LoadLocalizedText(currentLanguage);
        }
    }

    public bool IsReady
    {
        get
        {
            return isReady;
        }
    }
}


The problem, I think, is in this part. But if I knew for sure, I would not have asked you this question.
string dataAsJson;

        if (Application.platform == RuntimePlatform.Android)
        {
            string path = "file://" + Application.streamingAssetsPath + "/Languages/" + langName + ".json";
#pragma warning disable 618
            WWW reader = new WWW(path);
            while (!reader.isDone) { }

            dataAsJson = reader.text;
        }
        else
        {
            string path = Application.streamingAssetsPath + "/Languages/" + langName + ".json";
            dataAsJson = File.ReadAllText(path);
        }


Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ArtemCheck, 2021-11-05
@ArtemCheck

Managed to resolve the issue.
1. Move
string dataAsJson;
up to the rest of the variables 2. Then replace that piece of code with this one:
public string dataAsJson;

string path = System.IO.Path.Combine(Application.streamingAssetsPath, "Languages/" + langName + ".json");
        if (Application.platform == RuntimePlatform.Android)
        {
#pragma warning disable 618
            WWW reader = new WWW(path);
            while (!reader.isDone) { }
            dataAsJson = reader.text;
        }
        else
        {
            dataAsJson = System.IO.File.ReadAllText(path);
        }

PROFIT!
I hope for those who come across this problem, my answer will help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question