所以我最近在这里有一个关于如何在我的Android设备上找不到我的xml文件的问题(Unity game on android cannot find or read XML file)我现在能够使用WWW类找到该文件并使用Application .streamingAssetsPath,我已经设法让它访问文件并通过xml反序列化器(http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer)运行它。但是,该程序仍然拒绝在我的Android设备上找到该文件。我的问题是:我哪里出错了?
查找和读取文件的代码(反序列化与链接相同):
private void XMLSerializerLoad()
{
string path;
path = Application.streamingAssetsPath + "/assets/" + _xmlFile.name + ".xml";
print("Loading from: " + path);
StartCoroutine(MyRead(path));
}
IEnumerator MyRead(string path)
{
ItemContainer _items;
print("Access file: " + path);
WWW www = new WWW(path);
yield return www;
_items = ItemContainer.Load(www.url);
_spelling_words = new List<Item>(); //Setting to an empty list
_spelling_words = _items._items;
}
注意:_spelling_words是Item类型的全局列表,其中包含单词的字符串,year组的int和单词描述的字符串
[编辑]
我还决定使用7-zip来尝试解压apk以查找文件路径,发现该文件实际上位于apk内部的assets目录内,并且其中有另一个资产目录再次是相同的xml文件,我预计因为我如何构建流资产文件夹
[编辑]
XML示例:
<word name="WINNER">
<year>2</year> >
<Description>A person that wins</Description>
</word>
<word name="HUGE">
<year>2</year>
<Description>Extraordinary large in quantity</Description>
</word>
<word name="TRUE">
<year>2</year>
<Description>The opposite of false</Description>
</word>
<word name="GREW">
<year>2</year> >
<Description>Past tense of grow</Description>
</word>
序列化程序示例
public class Item
{
[XmlAttribute("name")]
public string _name;
[XmlElement("year")]
public int _year;
[XmlElement("Description")]
public string _description;
}
[XmlRoot("spelling_game")]
public class ItemContainer
{
[XmlArray("words"), XmlArrayItem("word")]
public List<Item> _items = new List<Item> ();
public void Save(string path)
{
var serializer = new XmlSerializer(typeof(ItemContainer));
using(var stream = new FileStream(path, FileMode.Create))
{
serializer.Serialize(stream, this);
}
}
public static ItemContainer Load(string path)
{
var serializer = new XmlSerializer(typeof(ItemContainer));
using(var stream = new FileStream(path, FileMode.Open))
{
return serializer.Deserialize(stream) as ItemContainer;
}
}
public static ItemContainer LoadFromText(string text)
{
var serializer = new XmlSerializer(typeof(ItemContainer));
return serializer.Deserialize(new StringReader(text)) as ItemContainer;
}
}