用Unity编写一个XML文件到iOS(使用c#)
本文关键字:文件 iOS 使用 XML 一个 Unity | 更新日期: 2023-09-27 17:50:14
我正试图在iOS上写XML,但似乎是不可能的。尝试了几种不同的方法,但没有一个有效。
我读到iOS只有对它的/Documents文件夹的写访问权限,所以我试图设置一个应用程序。data路径到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");
}
}
有什么建议,为什么我不能得到XML写到iOS和它的文件夹?我做错了什么?
您应该能够通过使用Application.persistentDataPath
保存到磁盘。它会进入一个公共文件夹,并且在应用程序更新时不会被删除。链接到unity文档
我认为问题在于你的路径不完全正确。
根据Apple的要求,您需要将文件写入:
/文件/gamexmldata.txt
你可以这样写:
/[浏览器名称].app/文件/gamexmldata.txt
要解决这个问题,可以使用以下命令:
fileSaveLocation = Application.dataPath。替换("/(浏览器名称)。app/Data", "/Documents/gamexmldata.txt");
并且要确保- [AppName]这里是你在构建应用程序时选择的开发应用程序Id名称(不是应用程序的"名称",而是Apple的Id名称)。
关于Apple所需位置的文档可以在这里找到:
https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html请告诉我是否有帮助。