目录为Assets/Scripts/ConfigReader/目录下
ReadBuySkinConfig.cs
主要读取英雄皮肤相关配置文件。
要读的XML文件如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SkinsPurchaseCfg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<info CommodityID="1">
<UnlockHeroID>10001</UnlockHeroID>
<HalfPhotoIcon>10</HalfPhotoIcon>
<ConsumeType>1,2</ConsumeType>
<n32Price>100,100</n32Price>
<TexturePick>Texture</TexturePick>
<DefaultSkin>1</DefaultSkin>
</info>
<info CommodityID="2">
<UnlockHeroID>10002</UnlockHeroID>
<HalfPhotoIcon>16</HalfPhotoIcon>
<ConsumeType>2</ConsumeType>
<n32Price>150</n32Price>
<TexturePick>Texture</TexturePick>
<DefaultSkin>1</DefaultSkin>
</info>
<info CommodityID="3">
<UnlockHeroID>10003</UnlockHeroID>
<HalfPhotoIcon>12</HalfPhotoIcon>
<ConsumeType>1,2</ConsumeType>
<n32Price>3000,255</n32Price>
<TexturePick>Texture</TexturePick>
<DefaultSkin>1</DefaultSkin>
</info>
<info CommodityID="4">
<UnlockHeroID>10004</UnlockHeroID>
<HalfPhotoIcon>7</HalfPhotoIcon>
<ConsumeType>1,2</ConsumeType>
<n32Price>6000,465</n32Price>
<TexturePick>Texture</TexturePick>
<DefaultSkin>1</DefaultSkin>
</info>
</SkinsPurchaseCfg>
ReadBuySkinConfig.cs
using System;
using UnityEngine;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
//这个类读取英雄皮肤相关配置文件
public class ReadBuySkinConfig
{
XmlDocument xmlDoc = null;
//构造函数
//配置文件目录:Assets/Resources/Config/SkinsPurchaseCfg.xml
public ReadBuySkinConfig(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 ("SkinsPuchaseCfg").ChildNodes;
//(XmlNode xNode in infoNodeList)
for (int i = 0; i < infoNodeList.Count; i++)
{
if ((infoNodeList[i] as XmlElement).GetAttributeNode("CommondityID") == null)
{
continue;
}
string typeName = (infoNodeList [i] as XmlElement).GetAttributeNode ("CommondityID").InnerText;
BuySkinConfigInfo buySkinInfo = new BuySkinConfigInfo ();
buySkinInfo.skinId = Convert.ToInt32 (typeName);
string cost = "";
string price = "";
foreach (XmlElement xEle in infoNodeList[i].ChildNodes)
{
#region 搜索
switch (xEle.Name)
{
case "UnlockHeroID":
buySkinInfo.heroId = Convert.ToInt32(xEle.InnerText);
break;
case "HalfPhotoIcon":
buySkinInfo.halfPhotoIcon = Convert.ToString(xEle.InnerText);
break;
case "DefaultSkin":
buySkinInfo.isDefaultSkin = Convert.ToInt32(xEle.InnerText) == 1 ? true : false;
break;
case "TexturePick":
buySkinInfo.skinTextureName = Convert.ToString(xEle.InnerText);
break;
}
#endregion
}
ConfigReader.buySkinXmlInfoDict.Add (buySkinInfo.skinId, buySkinInfo);
HeroWithSkinHelp.Instance ().LinkHeroWithSkin (buySkinInfo);
}
}
}
//一个英雄有多个皮肤
public class HeroWithSkin
{
public int heroId;
//skin
public List<int> skinList = new List<int> ();
//默认skinId
public int defaultSkinId;
//添加皮肤
public void AddSkin(BuySkinConfigInfo info)
{
if (skinList.Contains(info.skinId))
{
return;
}
skinList.Add (info.skinId);
//如果这个皮肤是默认皮肤,就设置成默认皮肤
if (info.isDefaultSkin)
{
defaultSkinId = info.skinId;
}
}
}
public class HeroWithSkinHelp
{
private static HeroWithSkinHelp instance = null;
public static HeroWithSkinHelp Instance()
{
if (instance == null)
{
instance = new HeroWithSkinHelp ();
}
return instance;
}
public Dictionary<int, HeroWithSkin> heroWithSkin = new Dictionary<int, HeroWithSkin> ();
public void LinkHeroWithSkin(BuySkinConfigInfo info)
{
HeroWithSkin heroInfo;
//如果heroWithSkin里面有这个皮肤对应的英雄,那就添加皮肤
if (heroWithSkin.TryGetValue(info.heroId, out heroInfo))
{
heroInfo.AddSkin (info);
return;
}
//没有这个皮肤对应的英雄,那就先创建HeroWithSkin,再添加皮肤
heroInfo = new HeroWithSkin ();
heroInfo.heroId = info.heroId;
heroInfo.AddSkin (info);
heroWithSkin.Add (heroInfo.heroId, heroInfo);
}
//用heroId来检索HeroWithSkin
public HeroWithSkin GetHeroWithSkinInfo(int heroId)
{
HeroWithSkin info;
if (heroWithSkin.TryGetValue(heroId, out info))
{
return info;
}
return null;
}
}
//对应的xml文件格式是这样的
/*
<info CommodityID="1">
<UnlockHeroID>10001</UnlockHeroID>
<HalfPhotoIcon>10</HalfPhotoIcon>
<ConsumeType>1,2</ConsumeType>
<n32Price>100,100</n32Price>
<TexturePick>Texture</TexturePick>
<DefaultSkin>1</DefaultSkin>
</info>
*/
public class BuySkinConfigInfo: System.Object
{
public int skinId;
public int heroId;
public string halfPhotoIcon;
public string skinTextureName;
public bool isDefaultSkin;
}