目录为:Assets/Scripts/ConfigReader/目录下
ReadMapObjConfig.cs
对应配置文件:
Assets/Resources/Config/MapObjCfg.xml
部分如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MapObjCfg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<info un32ID="100">
<un32MapID>1000</un32MapID>
<eObjectTypeID>21006</eObjectTypeID>
<un32ObjIdx>100</un32ObjIdx>
<n32Camp>1</n32Camp>
<cBornPos>6269,6000,9998</cBornPos>
<cBornDir>0,0,1</cBornDir>
<ReplaceID>JLZJDPS001</ReplaceID>
<n32Visibility>0</n32Visibility>
<eAttackMode>1</eAttackMode>
</info>
<info un32ID="101">
<un32MapID>1000</un32MapID>
<eObjectTypeID>21002</eObjectTypeID>
<un32ObjIdx>101</un32ObjIdx>
<n32Camp>0</n32Camp>
<cBornPos>5195,6003,10952</cBornPos>
<cBornDir>0,0,1</cBornDir>
<ReplaceID>0</ReplaceID>
<n32Visibility>0</n32Visibility>
<eAttackMode>1</eAttackMode>
</info>
<info un32ID="102">
<un32MapID>1000</un32MapID>
<eObjectTypeID>21008</eObjectTypeID>
<un32ObjIdx>102</un32ObjIdx>
<n32Camp>1</n32Camp>
<cBornPos>8109,6014,9150</cBornPos>
<cBornDir>0,0,1</cBornDir>
<ReplaceID>JLBYPS001</ReplaceID>
<n32Visibility>0</n32Visibility>
<eAttackMode>1</eAttackMode>
</info>
ReadMapObjConfig.cs
using System;
using UnityEngine;
using System.Xml;
using System.Collections.Generic;
//对应配置文件:Assets/Resources/Config/MapObjCfg.xml
public class ReadMapObjConfig
{
XmlDocument xmlDoc = null;
//构造函数
public ReadMapObjConfig(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.Load (xmlfile.text);
XmlNodeList infoNodeList = xmlDoc.SelectSingleNode ("MapObjCfg").ChildNodes;
for (int i = 0; i < infoNodeList.Count; i++)
{
if ((infoNodeList[i] as XmlElement).GetAttributeNode("un32ID") == null)
{
continue;
}
string typeName = (infoNodeList [i] as XmlElement).GetAttributeNode ("un32ID").InnerText;
MapObjConfigInfo objInfo = new MapObjConfigInfo ();
objInfo.un32Id = Convert.ToInt32 (typeName);
foreach (XmlElement xEle in infoNodeList[i].ChildNodes)
{
switch(xEle.Name)
{
case "un32MapID":
objInfo.un32MapID = Convert.ToInt32 (xEle.InnerText);
break;
case "eObjectTypeID":
objInfo.eObjectTypeID = Convert.ToInt32 (xEle.InnerText);
break;
case "un32ObjIdx":
objInfo.un32ObjIdx = Convert.ToInt32 (xEle.InnerText);
break;
case "n32Camp":
objInfo.n32Camp = Convert.ToInt32 (xEle.InnerText);
break;
//case "cBornPos":
// objInfo.cBornPos = GameMethod.ResolveToVector3(xEle.InnerText);
// break;
case "cBornDir":
objInfo.cBornDir = GameMethod.ResolveToVector3 (xEle.InnerText);
break;
case "ReplaceID":
objInfo.ReplaceID = xEle.InnerText;
break;
case "n32Visibility":
objInfo.n32Visibility = Convert.ToInt32 (xEle.InnerText);
break;
case "eAttackMode":
objInfo.eAttackMode = Convert.ToInt32 (xEle.InnerText);
break;
}
}
ConfigReader.mapObjXmlInfoDict.Add (Convert.ToInt32 (typeName), objInfo);
}
}
}
/*
<info un32ID="100">
<un32MapID>1000</un32MapID>
<eObjectTypeID>21006</eObjectTypeID>
<un32ObjIdx>100</un32ObjIdx>
<n32Camp>1</n32Camp>
<cBornPos>6269,6000,9998</cBornPos>
<cBornDir>0,0,1</cBornDir>
<ReplaceID>JLZJDPS001</ReplaceID>
<n32Visibility>0</n32Visibility>
<eAttackMode>1</eAttackMode>
</info>
*/
public class MapObjConfigInfo: System.Object
{
#region NPC信息
public int un32Id;
public int un32MapID;
public int eObjectTypeID;
public int un32ObjIdx;
public int n32Camp;
public Vector3 cBornPos;
public Vector3 cBornDir;
public string ReplaceID;
public int n32Visibility;
public int eAttackMode;
#endregion
}