用Unity编写XML文件到iOS(使用C#)

我正在尝试在iOS上写入XML,但似乎是不可能的。 尝试了几个不同的这个,但没有工作。

我已经阅读,iOS只有写/访问它的/ Documents文件夹,所以我试图设置一个Application.dataPath到Documents文件夹。 但是我在Xcode中得到这个错误:

DirectoryNotFoundException: Could not find a part of the path "/Users/mariusmathisen/Library/Application Support/iPhone Simulator/7.1/Applications/6AE4AF70-EFDF-46F0-9947-5C0936F3AD27/DaXml.app/Data/Documents/gamexmldata.txt". 

这是我写入XML的代码:

 lic void WriteToXml() { Debug.Log ("Du nådde metoden"); //string filepath = Application.dataPath + @"/Data/gamexmldata.xml"; XmlDocument xmlDoc = new XmlDocument(); GameControl.control.outputXml = "Du nådde WriteXML"; if(File.Exists(Application.dataPath + "/Documents/gamexmldata.txt")) { Debug.Log("Filen Fantes"); GameControl.control.outputXml = "DU ER I Writetoxml og filen fantes"; xmlDoc.Load(Application.dataPath + "/Documents/gamexmldata.txt"); //xmlDoc.Load(filepath); XmlElement elmRoot = xmlDoc.DocumentElement; // elmRoot.RemoveAll(); // remove all inside the transforms node. XmlElement elmNew = xmlDoc.CreateElement("rotation"); // create the rotation node. XmlElement rotation_X = xmlDoc.CreateElement("x"); // create the x node. rotation_X.InnerText = x; // apply to the node text the values of the variable. XmlElement rotation_Y = xmlDoc.CreateElement("y"); // create the y node. rotation_Y.InnerText = y; // apply to the node text the values of the variable. XmlElement rotation_Z = xmlDoc.CreateElement("z"); // create the z node. rotation_Z.InnerText = z; // apply to the node text the values of the variable. XmlElement navnElement = xmlDoc.CreateElement("name"); navnElement.InnerText = navn; GameControl.control.inputXml = navnElement.InnerText; elmNew.AppendChild(rotation_X); // make the rotation node the parent. elmNew.AppendChild(rotation_Y); // make the rotation node the parent. elmNew.AppendChild(rotation_Z); // make the rotation node the parent. elmNew.AppendChild(navnElement); elmRoot.AppendChild(elmNew); // make the transform node the parent. xmlDoc.Save(Application.dataPath + "/Documents/gamexmldata.txt"); // save file. Debug.Log("Filen er lagret"); }else{ xmlDoc.Save(Application.dataPath + "/Documents/gamexmldata.txt"); } } 

有什么build议,为什么我不能让XML写入到iOS和它的文件夹? 我究竟做错了什么?

您应该能够通过使用Application.persistentDataPath保存到磁盘。 它进入公共文件夹,并在应用程序更新时不会被删除。 链接到统一文档 。

我认为问题在于你的道路不完全正确。

根据苹果,你需要将文件写入:

/Documents/gamexmldata.txt

相反,你是这样写的:

/[AppName].app/Documents/gamexmldata.txt

要解决这个问题,你可以使用以下方法:

fileSaveLocation = Application.dataPath.Replace(“/[AppName].app/Data”,“/Documents/gamexmldata.txt”);

并确保 – 这里的[AppName]是您在构build应用程序时select的开发应用程序Id名称(不是应用程序的“名称”,但是是Apple的Id名称)。

关于Apple所需位置的文档可以在这里find:

https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

让我知道是否有帮助。