目录为:Assets/Scripts/ConfigReader/目录下
ReadCombineConfig.cs
看看要读取的XML文件:
Assets/Resources/Config/
CombineCfg.xml
一部分如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CombineCfg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<info un32ItemID="30001">
<un32ChildID1>0</un32ChildID1>
<un32ChildID2>0</un32ChildID2>
</info>
<info un32ItemID="30002">
<un32ChildID1>0</un32ChildID1>
<un32ChildID2>0</un32ChildID2>
</info>
<info un32ItemID="30003">
<un32ChildID1>0</un32ChildID1>
<un32ChildID2>0</un32ChildID2>
</info>
ReadCombineConfig.cs
using System;
using UnityEngine;
using System.Xml;
using System.Collections.Generic;
//暂时不明白这里的Combine指的什么
//读取的config文件路径为:Assets/Resources/Config/CombineCfg.xml
class ReadCombineConfig
{
XmlDocument xmlDoc = null;
//构造函数
public ReadCombineConfig(string xmlFilePath)
{
ResourceUnit xmlfileUnit = ResourcesManager.Instance.loadImmediate (xmlFilePath, ResourceType.ASSET);
TextAsset xmlfile = xmlfileUnit.Asset as TextAsset;
if (!xmlfile)
{
Debug.LogError(" error infos: 没有找到指定的xml文件:" + xmlFilePath);
}
xmlDoc = new XmlDocument ();
xmlDoc.LoadXml (xmlfile.text);
XmlNodeList infoNodeList = xmlDoc.SelectSingleNode ("CombineCfg").ChildNodes;
for (int i = 0; i < infoNodeList.Count; i++)
{
XmlAttribute xmlAttr = (infoNodeList [i] as XmlElement).GetAttributeNode ("un32ItemID");
if (xmlAttr == null)
{
continue;
}
string itemId = xmlAttr.InnerText;
CombineConfigInfo combineInfo = new CombineConfigInfo ();
foreach (XmlElement xEle in infoNodeList[i].ChildNodes)
{
#region 搜索
switch (xEle.Name)
{
case "un32ChildID1":
combineInfo.un32ChildID1 = Convert.ToInt32(xEle.InnerText);
break;
case "un32ChildID2":
combineInfo.un32ChildID2 = Convert.ToInt32(xEle.InnerText);
break;
}
#endregion
}
ConfigReader.combineXmlInfoDict.Add (Convert.ToInt32 (itemId), combineInfo);
}
}
}
/*
对应的XML文件格式是这样的
<info un32ItemID="30001">
<un32ChildID1>0</un32ChildID1>
<un32ChildID2>0</un32ChildID2>
</info>
*/
public class CombineConfigInfo: System.Object
{
public int un32ChildID1;
public int un32ChildID2;
}