生成XML配置文件
定义数据格式
public class AutoGuideCfg : BaseData<AutoGuideCfg> { public int npcID; //触发任务目标NPC索引号 public string dilogArr; //对话数组 public int actID; //对应表格字段 public int coin; public int exp; }
加载和初始化配置
ResSvc.cs中
Start方法中调用初始化
private Start() { ... InitGuideCfg(PathDefine.GuideCfg); ... }
存入字典
private Dictionary<int, AutoGuideCfg> guideTaskDic = new Dictionary<int, AutoGuideCfg>();
初始化数据
private void InitGuideCfg(string path) { TextAsset xml = Resources.Load<TextAsset>(path); if (!xml) { PECommon.Log("xml file:" + path + " not exist", LogType.Error); } else { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml.text); XmlNodeList nodLst = doc.SelectSingleNode("root").ChildNodes; for (int i = 0; i < nodLst.Count; i++) { XmlElement ele = nodLst[i] as XmlElement; if (ele.GetAttributeNode("ID") == null) { continue; } int ID = Convert.ToInt32(ele.GetAttributeNode("ID").InnerText); AutoGuideCfg mc = new AutoGuideCfg { ID = ID }; foreach (XmlElement e in nodLst[i].ChildNodes) { switch (e.Name) { case "npcID": mc.npcID = int.Parse(e.InnerText); break; case "dilogArr": mc.dilogArr = e.InnerText; break; case "actID": mc.actID = int.Parse(e.InnerText); break; case "coin": mc.coin = int.Parse(e.InnerText); break; case "exp": mc.exp = int.Parse(e.InnerText); break; } } guideTaskDic.Add(ID, mc); } } }
提供获取数据的API
public AutoGuideCfg GetAutoGuideCfg(int id) { AutoGuideCfg agc = null; if (guideTaskDic.TryGetValue(id, out agc)) { return agc; } return null; }
指定路径
PathDefine.cs
public const string GuideCfg = "ResCfgs/guide"; ... #region AutoGuide public const string TaskHead = "ResImages/task"; public const string WiseManHead = "ResImages/wiseman"; public const string GeneralHead = "ResImages/general"; public const string ArtisanHead = "ResImages/artisan"; public const string TraderHead = "ResImages/trader"; public const string SelfIcon = "ResImages/assassin"; public const string GuideIcon = "ResImages/npcguide"; public const string WiseManIcon = "ResImages/npc0"; public const string GeneralIcon = "ResImages/npc1"; public const string ArtisanIcon = "ResImages/npc2"; public const string TraderIcon = "ResImages/npc3"; #endregion ...