2016-05-13 55 views
1

我準備了一些含有某些內容的xml文件,並希望在iOS設備上播放時加載它,但我也想更改加載的數據並將其再次序列化到同一文件中。 在Unity編輯器(Windows)中,它完美地工作,但是當我在iOS設備上測試它時,似乎我可以使用WWW類從StreamingAssets讀取,但我無法寫入它。 另外我發現我可以讀寫由Application.persistentDataPath創建的路徑。但似乎在設備的某個位置,我不能把我的XML到該位置,用戶有權訪問該文件夾,所以這不是很好的解決方案,不是嗎?如何讀取和寫入一個XML文件到相同的位置?

這裏用於加載和保存數據的代碼。

using UnityEngine; 
using UnityEngine.UI; 
using System.Collections; 
using System.Collections.Generic; 
using System.Xml.Serialization; 
using System.IO; 
using System.Xml; 

public class testxml : MonoBehaviour { 

public Text result; 
public InputField firstPart, secondPart; 
public Toggle toggle; 

private List<int> listToSave; 

// Use this for initialization 
void Start() { 
    listToSave = new List<int>(); 
} 

public void Save() 
{ 
    Serialize(); 
} 

public void Load() 
{ 
    StartCoroutine(Deserialize()); 
} 

private void Serialize() 
{ 
    string path = GetPath(); 
    try 
    { 
     Debug.Log("trying to save"); 

     var serializer = new XmlSerializer(typeof(List<int>)); 
     using (var fs = new FileStream(path, FileMode.OpenOrCreate)) 
     { 
      serializer.Serialize(fs, listToSave); 
     } 
    } 
    catch (XmlException e) 
    { 
     result.text = "error"; 
     Debug.LogError(path + " with " + (toggle.isOn ? "persistent data path" : "data path")); 
     Debug.LogError("xml exc while des file : " + e.Message); 
    } 
    catch (System.Exception e) 
    { 
     result.text = "error"; 
     Debug.LogError("exc while des file : " + e.Message); 
     Debug.LogError(path + " with " + (toggle.isOn ? "persistent data path" : "data path")); 
     System.Exception exc = e.InnerException; 
     int i = 0; 
     while (exc != null) 
     { 
      Debug.Log("inner " + i + ": " + exc.Message); 
      i++; 
      exc = exc.InnerException; 
     } 
    } 
} 

private IEnumerator Deserialize() 
{ 
    Debug.Log("trying to load"); 
    string path = GetPath(); 
    var www = new WWW(path); 
    yield return www; 
    if (www.isDone && string.IsNullOrEmpty(www.error)) 
    { 
     try 
     { 
      var serializer = new XmlSerializer(typeof(List<int>)); 
      MemoryStream ms = new MemoryStream(www.bytes); 
      listToSave = serializer.Deserialize(ms) as List<int>; 
      ms.Close(); 
      result.text += "Done\n"; 
      foreach (var i in listToSave) 
       result.text += i + "\n"; 
     } 
     catch (XmlException e) 
     { 
      result.text = "error"; 
      Debug.LogError(path + " with " + (toggle.isOn?"persistent data path":"data path")); 
      Debug.LogError("xml exc while des file : " + e.Message); 
     } 
     catch (System.Exception e) 
     { 
      result.text = "error"; 
      Debug.LogError("exc while des file : " + e.Message); 
      Debug.LogError(path + " with " + (toggle.isOn ? "persistent data path" : "data path")); 
      System.Exception exc = e.InnerException; 
      int i = 0; 
      while(exc!=null) 
      { 
       Debug.Log("inner "+i+": " + exc.Message); 
       i++; 
       exc = exc.InnerException; 
      } 
     } 

     yield break; 
    } 
    else 
    { 
     Debug.LogError("www exc while des file " + www.error); 
     Debug.LogError(path + " with " + (toggle.isOn ? "persistent data path" : "data path")); 
     yield break; 
    } 
} 

private string GetPath() 
{ 
    string path = firstPart.text; 
    if (toggle.isOn) 
    { 
     path += Application.persistentDataPath; 
    } 
    else 
     path += Application.dataPath; 
    path += secondPart.text; 
    return path; 
} 
} 
+0

您確定您一直都在使用http://stackoverflow.com/a/36236745/294884對不對? – Fattie

+0

所以我必須一直使用Application.persistentDataPath?但該文件夾是哪裏? – user2686299

+0

您只能使用Application.persistentDataPath。 (這是非常令人困惑的是,Unity文檔曾經提到*其他任何內容。)你無法知道「它在哪裏」,每次都會決定。如果你的意思是,在你的Mac或PC上,你想查看文件夾(檢查某些內容),只需在Debug.Log(Application.persistentDataPath);'任何位置添加此代碼即可看到它。希望能幫助到你。 – Fattie

回答

2

「我希望把我的xml文件這個文件夾中,然後讀取它。這就像遊戲默認信息」

容易,只要把它放到你的資產。像這樣...

public TextAsset myXMLFile; 

在Inspector中拖動文件。你完成了。


「但後來我也想改變這個文件並保存」

很公平。你所要做的就是

(1)做一個路徑p = Application.persistentDataPath + 「values.txt」

(2)程序啓動。

(3)檢查是否存在「p」。如果是,讀它,去(6)

(4)如果沒有,閱讀textasset並保存到 「P」

(5)去點(3)

(6 ) 你完成了。

這是做到這一點的唯一方法。這確實是Unity中的正常程序,您可以在每個Unity應用程序中執行此操作。沒有別的辦法!

+0

是的默認信息,但是我也想要更改該文件並保存 – user2686299

+0

gotchya,請參閱答案 – Fattie

+1

正如我所料,謝謝你的答案xD – user2686299