使用Unity将XML文件写入iOS(使用C#) [英] Writing a XML file to iOS with Unity (Using C#)
问题描述
我正在尝试在iOS上写入XML,但这似乎是不可能的。试过几个不同的但是没有用过。
I am trying to write to a XML on iOS, but it seems to be impossible. Tried several different this but none have worked.
我已经读过iOS只对它/ Documents文件夹有写访问权限,所以我试图设置一个应用程序。 dataPath到Documents文件夹。但我在Xcode中收到此错误:
I have read that iOS only have write access to it´s /Documents folder, so I have tried to set a Application.dataPath to the Documents folder. But I get this error in 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的代码:
This is my code to write to 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");
}
}
有什么建议我为什么不能得到要写入iOS及其文件夹的XML?我做错了什么?
Are there any suggestion to why I can´t get the XML to write to iOS and it´s folder? What am I doing wrong?
推荐答案
我认为问题在于你的路径不完全正确。
I think that the problem lies in the fact that your path is not completely correct.
根据Apple的说法,您需要将文件写入:
According to Apple you are required to write the file into:
/Documents/gamexmldata.txt
/Documents/gamexmldata.txt
而不是你把它写成:
/ [AppName] .app / Documents / gamexmldata.txt
/[AppName].app/Documents/gamexmldata.txt
要解决此问题,您可以使用以下内容:
To solve this you can use the following:
fileSaveLocation = Application.dataPath.Replace(/ [AppName] .app /数据,/文档/ gamexmldata.txt);
fileSaveLocation = Application.dataPath.Replace( "/[AppName].app/Data", "/Documents/gamexmldata.txt" );
并确保 - [AppName]这里是您在构建应用程序时选择的开发应用ID名称(不是应用程序的名称,但它是Apple的ID名称。)
And to make sure - [AppName] here is the development app Id name you selected when you build the application (not the 'name' of the application, but it's Id name with Apple).
有关Apple所需位置的文档,请访问:
Documentation about the required location by Apple can be found here:
如果有帮助,请告诉我。
Let me know if it helped.
这篇关于使用Unity将XML文件写入iOS(使用C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!