Android上的统一游戏无法找到或读取XML文件 [英] Unity game on android cannot find or read XML file

查看:209
本文介绍了Android上的统一游戏无法找到或读取XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在团结做了一个游戏,我试图在Android设备上进行部署。我试图使用XML文件来存储设备的信息,但问题是,当游戏的任何Android设备上无法找到正确的XML文件的路径。

I have a game made in Unity that I'm trying to deploy on Android devices. I'm trying to use XML files to store information on the device, but the problem is that when the game is on any Android device it cannot find the XML file path correctly.

为了读它我使用的XML序列化(的http:/ /wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer ),我只是想知道,如果有什么事,我需要为了得到XML文件的实际文件路径做。

In order to read it I'm using the XML serializer (http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer) and I just want to know if there's anything that I need to do in order to get the actual file path of the XML file.

我已经使用流资产的方法试图让我有一种叫与我想在它读取XML文件流的资产文件夹。我试图用这个code,试图访问它(我的ItemContainer的工作方式为MonsterContainer相同):

I've tried using a streaming assets method so I have a folder called streaming assets with the XML file I want to read in it. I tried using this code to try and access it (My ItemContainer works the same way as the MonsterContainer):

string path;
ItemContainer itemCollection;
if (Application.platform == RuntimePlatform.Android) {
    path = "file://" + Application.streamingAssetsPath + "!/assets/" + _xmlFile.name + ".xml";
} else {
    path = Application.dataPath + "/StreamingAssets/" + _xmlFile.name + ".xml";
    itemCollection = ItemContainer.Load(path);
}

不过,这并没有在Android上工作,但我的Windows桌面上所做的工作。然后我看到WWW也许能用来查找文件,但同样没有奏效:

This however didn't work on Android but did work on my Windows desktop. Then I saw that WWW might be able to be used for finding the file, but again it didn't work:

string path;
ItemContainer itemCollection;
if (Application.platform == RuntimePlatform.Android) {
    path = "jar:file://" + Application.dataPath + "!/assets/" + _xmlFile.name;
    WWW data = new WWW(path);
    StartCoroutine(MyRead(data));
    itemCollection = ItemContainer.Load(data.text);
} 
else {
    path = Application.dataPath + "/StreamingAssets/" + _xmlFile.name + ".xml";
    itemCollection = ItemContainer.Load(path);
}

是否有遗漏或任何我才能访问Android设备上的XML文件做错了?

Is there anything missing or anything that I'm doing wrong in order to access the XML file on Android devices?


我发现这是使枚举无法正常工作,所以我已经重做的程序如何访问文件了一点,所以它更符合哪一些建议在

I found out a small problem that was making the enumerator not work so I've redone how the program accesses the file a bit so that it's more in line with what some of the suggestions have hinted at

private void XMLSerializerLoad()
{
    string path;
    path = Application.streamingAssetsPath + "/assets/" + _xmlFile.name + ".xml";
    StartCoroutine(MyRead(path));
}

IEnumerator MyRead(string path)
{
    ItemContainer _items;
    WWW www = new WWW(path);
    yield return www;
    _items = ItemContainer.LoadFromText(www.text);
    _spelling_words = new List<Item>(); //Setting to an empty list
    _spelling_words = _items._items;
}

但是,当我运行此我得到:

But when I run this I get:

XmlException:文档没有出现。 1号线位置1。

XmlException: Document did not appear. Line 1 position 1.

Mono.Xml2.XmlTextReader.Read()

Mono.Xml2.XmlTextReader.Read()

推荐答案

Application.streamingAssetsPath Android中已经包含的文件://所以你需要省略android系统,以便为它工作。

Application.streamingAssetsPath in Android already contains file:// so you need to omit that in android in order for it to work.

  path = Application.streamingAssetsPath + "!/assets/" +   _xmlFile.name + ".xml";

这对Application.dataPath同你不需要添加文件://或jar:文件://

It's the same for Application.dataPath you do not need to add file:// or jar:file://

我不明白!是,在!/资产/也许该CA是一个问题,太

I don't understand what ! is for, in "!/assets/" maybe that ca be a problem too

这篇关于Android上的统一游戏无法找到或读取XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆