当前位置:   article > 正文

Unity 解析XML 《一》_unity link.xml

unity link.xml

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

解析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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/115354
推荐阅读
相关标签
  

闽ICP备14008679号