Unity 解析XML
xml可以反序列化为对象,如果是采用Mono打包的方式,不会有任何问题,如果采用IL2CPP方式打包,会反序列化失败,主要是由于IL2CPP裁剪导致的错误,解决的方式有2种,推荐第一种做法,简单粗暴
(1)不要使用反序列化的方式,而是直接解析XML为具体的对象,这样打android 和 IOS 都不会出 Bug
(2)使用 link.xml 方式
XML数据
<?xml version="1.0" encoding="utf-8"?>
<AssetBundleConfig xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ABList path="Assets/04zcl/02AR/04dixingtu/DGX.jpg" Crc="3522942012" ABName="04dixingtu" AssetName="DGX.jpg" />
<ABList path="Assets/04zcl/02AR/04dixingtu/DX2.jpg" Crc="4158770841" ABName="04dixingtu" AssetName="DX2.jpg" />
<ABList path="Assets/04zcl/02AR/04dixingtu/FST.jpg" Crc="1807332153" ABName="04dixingtu" AssetName="FST.jpg" />
<ABList path="Assets/04zcl/02AR/04dixingtu/shantihasLine.mat" Crc="958141478" ABName="04dixingtu" AssetName="shantihasLine.mat" >
<ABDependence>XXX</ABDependence>
<ABDependence>XXXXX</ABDependence>
<ABDependence>XXXXXXX</ABDependence>
</ABList>
</AssetBundleConfig>
解析XML的函数
public static AssetBundleConfig ParseABConfigXML(string path, string rootName)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodlist = doc.SelectSingleNode(rootName).ChildNodes;
AssetBundleConfig ABConfig = new AssetBundleConfig();
ABConfig.ABList = new List<ABBase>();
for(int i = 0; i < nodlist.Count; i++)
{
ABBase aBBase = new ABBase();
XmlNode ele = nodlist[i] as XmlNode;
aBBase.Path= ele.Attributes["path"].Value;
aBBase.Crc = uint.Parse(ele.Attributes["Crc"].Value);
aBBase.ABName = ele.Attributes["ABName"].Value;
aBBase.AssetName = ele.Attributes["AssetName"].Value;
XmlNodeList delist = ele.SelectNodes("ABDependence");
if (delist!=null)
{
aBBase.ABDependence = new List<string>();
for(int j = 0; j < delist.Count; j++)
{
aBBase.ABDependence.Add(delist[j].InnerText);
}
}
ABConfig.ABList.Add(aBBase);
}
return ABConfig;
}