I'm trying to read an already existing xml file with tasks in my game. If I use xmlDoc.Load(Application.dataPath $"/Resources/XML/tasks1.xml");
then everything works in the editor, but does not work in the finished version on android.
I have read about the possibility of using persistentDataPath
but it does not work. The file is not searched in the editor and on the device. What am I doing wrong?
private void LoadTasks()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Application.persistentDataPath $"/Resources/XML/Russian/{BurronController.mode}.xml");
XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
XmlNode trueNode = nodes[0];
XmlNode doNode = nodes[1];
trueTasks = new string[trueNode.ChildNodes.Count];
doTasks = new string[doNode.ChildNodes.Count];
for (int i = 0; i < trueTasks.Length; i )
{
trueTasks[i] = trueNode.ChildNodes[i].InnerText;
}
for (int i = 0; i < doTasks.Length; i )
{
doTasks[i] = doNode.ChildNodes[i].InnerText;
}
}
CodePudding user response:
Read the API for Application.datPath
Android: Normally it points directly to the APK. If you are running a split binary build, it points to the OBB instead.
It is the apk path and you can not simply access files packed in there.
and Application.persistentDataPath
Android: Application.persistentDataPath points to /storage/emulated/0/Android/data//files on most devices (some older phones might point to location on SD card if present), the path is resolved using android.content.Context.getExternalFilesDir.
=> unless you explicitly have put your file here there is nothing to load from.
In the editor itself you have external access to your file system and can use both if the file is located in according path.
Further you are using Resources
- in general Do not use it. Nowadays you would rather go for Addressables.
Anyway also refer to according API for Resources
=> You get assets from there via Resources.Load
/ Resources.LoadAsync
where the path would rather be e.g.
var xmlAsset = Resources.Load<TextAsset>($"XML/Russian/{BurronController.mode}");
from there you can now either directly load the xml string via XmlDocument.LoadXml
xmlDoc.LoadXml(xmlAsset.text);
or you could go for XmlReader
via a MemoryStream
Or you could copy it over to the persistentDataPath
if you want your users to be able to manipulate the file and then lo from the file via you original attempt