您的位置:首页 > 移动开发 > Unity3D

Unity3D-动态读取配置文件,加载游戏对象

2017-10-30 20:50 513 查看
private Dictionary<int,T> LoadConfig<T>(string fileName) where T : class,new()
{
Dictionary<int, T> dic = new Dictionary<int, T>();
TextAsset bossCfgInfo = Resources.Load<TextAsset>("Config/" + fileName);
//新建xml文档对象
XmlDocument document = new XmlDocument();
//把字符串加载到对象中
document.LoadXml(bossCfgInfo.text);
//获取根节点
XmlNode rootNode = document.SelectSingleNode("Root");
//获取根节点的子节点列表
XmlNodeList nodeList = rootNode.ChildNodes;
//遍历
foreach (XmlNode node in nodeList)
{
//节点 转 元素类型
XmlElement element = node as XmlElement;
T obj = CreateAndSetValue<T>(element);
dic.Add(int.Parse( element.GetAttribute("ID")), obj);
}
return dic;
}


配置文件要对应一个公有类,该类中的字段唯一对应配置文件中的一组属性,变量名必须和配置文件一致!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息