2016-04-27 212 views
0

我正在开发使用Unity引擎的多语言游戏,我为每种语言编写了xml文件,但android apk似乎并没有读取xml文件, PC平台的构建可以很好地适用于所有语言。android apk不读取xml文件(从unity editor编译的apk版本)

这里是我的代码:

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); 
    } 
} 

回答

0

变化的读取和写入目录
Application.streamingAssetsPath

在资产 添加一个新的文件夹并命名它StreamingAssets 每个文件StreamingAssets下可以修改,读取,写入所有平台

+0

尝试过,没有运气... –