目录为:Assets/Scripts/ConfigReader/目录下
ReadDailyBonusConfig.cs
这里主要读取关于日常奖励的Config文件
对应读取的XML文件为:
Assets/Resources/Config/
dailybonus.xml
一部分如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DailyBonus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<info n32Days="1">
<n32Type>1</n32Type>
<n32ItemID>2</n32ItemID>
<n32ItemNum>120</n32ItemNum>
</info>
<info n32Days="2">
<n32Type>1</n32Type>
<n32ItemID>100010</n32ItemID>
<n32ItemNum>1</n32ItemNum>
</info>
<info n32Days="3">
<n32Type>1</n32Type>
<n32ItemID>130002</n32ItemID>
<n32ItemNum>1</n32ItemNum>
</info>
ReadDailyBonusConfig.cs
using System;
using UnityEngine;
using System.Xml;
using System.Collections.Generic;
//日常奖励配置文件
//响应配置Config路径为:Assets/Resources/Config/dailybonus.xml
public class ReadDailyBonusConfig
{
XmlDocument xmlDoc = null;
//构造函数
public ReadDailyBonusConfig(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 ("DailyBonus").ChildNodes;
for (int i = 0; i < infoNodeList.Count; i++)
{
XmlAttribute xmlAttr = (infoNodeList [i] as XmlElement).GetAttributeNode ("n32Days");
if (xmlAttr == null)
{
continue;
}
DailyBonusConfigInfo dailyBonusInfo = new DailyBonusConfigInfo ();
foreach (XmlElement xEle in infoNodeList[i].ChildNodes)
{
#region 搜索
switch (xEle.Name)
{
case "n32Type":
dailyBonusInfo.n32Type = xEle.InnerText.Split(new char[] { ',' });
break;
case "n32ItemID":
dailyBonusInfo.n32ItemID = xEle.InnerText.Split(new char[] { ',' });
break;
case "n32ItemNum":
dailyBonusInfo.n32ItemNum = xEle.InnerText.Split(new char[] { ',' });
break;
}
#endregion
}
ConfigReader.dailyBonusXmlInfoDic.Add (Convert.ToUInt32 (xmlAttr.InnerText), dailyBonusInfo);
}
}
}
/*
相应xml文件格式:
<info n32Days="1">
<n32Type>1</n32Type>
<n32ItemID>2</n32ItemID>
<n32ItemNum>120</n32ItemNum>
</info>
*/
public class DailyBonusConfigInfo: System.Object
{
public string[] n32Type;
public string[] n32ItemID;
public string[] n32ItemNum;
}