简体   繁体   English

统一找不到 xml 文件

[英]Can't find xml file in unity

I'm trying to read an already existing xml file with tasks in my game.我正在尝试读取一个已经存在的 xml 文件,其中包含我的游戏中的任务。 If I use xmlDoc.Load(Application.dataPath + $"/Resources/XML/tasks1.xml");如果我使用xmlDoc.Load(Application.dataPath + $"/Resources/XML/tasks1.xml"); then everything works in the editor, but does not work in the finished version on android.然后在编辑器中一切正常,但在 android 上的完成版本中不起作用。 I have read about the possibility of using persistentDataPath but it does not work.我已阅读有关使用persistentDataPath的可能性,但它不起作用。 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;
        }
    }

Read the API for Application.datPath阅读 API 以获得Application.datPath

Android : Normally it points directly to the APK. Android :通常它直接指向 APK。 If you are running a split binary build, it points to the OBB instead.如果您正在运行拆分二进制构建,则它指向 OBB。

It is the apk path and you can not simply access files packed in there.它是 apk 路径,您不能简单地访问其中打包的文件。

and Application.persistentDataPathApplication.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. Android :Application.persistentDataPath 指向大多数设备上的 /storage/emulated/0/Android/data//files(如果存在,一些旧手机可能指向 SD 卡上的位置),使用 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 .此外,您正在使用Resources - 通常不要使用它 Nowadays you would rather go for Addressables .现在你宁愿 go 用于Addressables

Anyway also refer to according API for Resources无论如何也参考API资源

=> You get assets from there via Resources.Load / Resources.LoadAsync where the path would rather be eg => 你通过Resources.Load / Resources.LoadAsync从那里获得资产,路径宁愿是例如

var xmlAsset = Resources.Load<TextAsset>($"XML/Russian/{BurronController.mode}");

from there you can now either directly load the xml string via XmlDocument.LoadXml从那里您现在可以通过XmlDocument.LoadXml直接加载 xml 字符串

xmlDoc.LoadXml(xmlAsset.text);

or you could go for XmlReader via a MemoryStream或者您可以通过MemoryStreamXmlReader提供 go

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或者,如果您希望您的用户能够操作文件,然后通过您最初的尝试从文件中查找,您可以将其复制到persistentDataPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM