目录为:Assets/Scripts/ConfigReader/目录下
ReadGuideRewardTaskConfig.cs
对应xml配置文件:
Assets/Resources/Config/Award.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Award xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<info id="1001">
<award>1</award>
<quantity>500</quantity>
<pos>0</pos>
</info>
<info id="1002">
<award>2</award>
<quantity>0</quantity>
</info>
</Award>
ReadGuideRewardTaskConfig.cs
using System;
using UnityEngine;
using System.Xml;
using System.Collections.Generic;
//Guide相关
//对应配置文件:Assets/Resources/Config/Award.xml
public class ReadGuideRewardTaskConfig
{
XmlDocument xmlDoc = null;
public ReadGuideRewardTaskConfig(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 ("Award").ChildNodes;
for (int i = 0; i < infoNodeList.Count; i++)
{
if ((infoNodeList[i] as XmlElement).GetAttributeNode("id") == null)
{
continue;
}
string typeName = (infoNodeList [i] as XmlElement).GetAttributeNode ("id").InnerText;
//在Guide那部分定义
CRewardTask rewardInfo = new CRewardTask ();
rewardInfo.TaskId = Convert.ToInt32 (typeName);
rewardInfo.TaskType = GuideTaskType.RewardTipTask;
foreach (XmlElement xEle in infoNodeList[i].ChildNodes)
{
switch (xEle.Name)
{
case "award":
rewardInfo.RewardType = (RewardTaskType)Convert.ToInt32 (xEle.InnerText);
break;
case "quantity":
rewardInfo.RewardResult = Convert.ToInt32 (xEle.InnerText);
break;
case "pos":
string pos = Convert.ToString (xEle.InnerText);
if (pos.Length == 1)
{
rewardInfo.EffectPos = Vector3.zero;
}
else
{
List<float> posList = GameMethod.ResolveToFloatList (Convert.ToString (xEle.InnerText), ';');
rewardInfo.EffectPos = new Vector3 (posList [0], posList [1], posList [2]);
}
break;
case "path":
rewardInfo.EffectPath = Convert.ToString (xEle.InnerText);
break;
}
}
CTaskBase.rewardTaskDic.Add (rewardInfo.TaskId, rewardInfo);
}
}
}
/*
XML格式还是放在这里:
<info id="1001">
<award>1</award>
<quantity>500</quantity>
<pos>0</pos>
</info>
*/