简体   繁体   中英

android apk not reading xml file (apk build from unity editor)

i am developing a multi-language game using unity engine, i have xml files written for each language but android apk doesn't seem to read the xml files where as the PC platform build works fine with all the languages.

Here is my code:

void Awake()
{
    languagePath = Application.dataPath + "/Languages/";
    CollectLanguages();
}

private void CollectLanguages()
{
    try
    {
        DirectoryInfo langDir = new DirectoryInfo(languagePath);
        FileInfo[] files = langDir.GetFiles("*.xml");
        languageFiles = new string[files.Length];
        int i = 0;
        foreach (FileInfo fileGo in files)
        {
            languageFiles[i] = fileGo.FullName;
            i++;
        }
    }
    catch (System.Exception e)
    {
        Debug.Log(e.Message);
    }
}

private string GetLanguageFile(string language)
{
    foreach (string langGo in languageFiles)
    {
        if (langGo.EndsWith(language + ".xml"))
        {
            return langGo;
        }
    }
    return string.Empty;
}

public void LoadLanguage(string language)
{
    try
    {

        string filepath = Application.persistentDataPath + "/" + language+".xml";
        if (Application.platform == RuntimePlatform.Android)
        {
            WWW path = new WWW("jar:file://" + Application.dataPath + "!/assets/"+"Languages/"+language+".xml");
            while(!path.isDone)
            {
                Debug.Log("Loading File");
            }
            File.WriteAllBytes(filepath, path.bytes);
        }
        mainDoc = new XmlDocument();
        StreamReader streamReader = new StreamReader (filepath);
        mainDoc.Load(streamReader);
        root = mainDoc.DocumentElement;
        streamReader.Close();

    }
    catch (System.Exception e)
    {
        Debug.Log(e.Message);
    }
}

change the read and write directory to
Application.streamingAssetsPath

add a new folder in Assets and name it to StreamingAssets every file under the StreamingAssets can modify , read , write in all of platforms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM