我準備了一些含有某些內容的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;
}
}
您確定您一直都在使用http://stackoverflow.com/a/36236745/294884對不對? – Fattie
所以我必須一直使用Application.persistentDataPath?但該文件夾是哪裏? – user2686299
您只能使用Application.persistentDataPath。 (這是非常令人困惑的是,Unity文檔曾經提到*其他任何內容。)你無法知道「它在哪裏」,每次都會決定。如果你的意思是,在你的Mac或PC上,你想查看文件夾(檢查某些內容),只需在Debug.Log(Application.persistentDataPath);'任何位置添加此代碼即可看到它。希望能幫助到你。 – Fattie