反射+泛型读取XML配置文件
2017-09-20 19:40
225 查看
新建文件夹Resources/Config
新建文件夹Scripts
脚本ConfigManager
using UnityEngine;
using System.Collections;
//游戏入口
public class VRGameManager : MonoBehaviour {
}
“`
新建文件夹Scripts
脚本ConfigManager
using UnityEngine; using System.Collections; using System.Xml; using System.Collections.Generic; using System; using System.Reflection; //逻辑相同 类型不同 ---- 泛型 //处理加载,缓存,获取配置的管理器 public class ConfigManager : Singleton<ConfigManager> { private Dictionary<int, BossCfg> _bossCfgDic = new Dictionary<int, BossCfg>(); private Dictionary<int, WeaponCfg> _weaponCfgDic = new Dictionary<int, WeaponCfg>(); private Dictionary<int, ShieldCfg> _shieldCfgDic = new Dictionary<int, ShieldCfg>(); public void LoadAllConfig() { _bossCfgDic = LoadConfig<BossCfg>("BossConfig"); _weaponCfgDic = LoadConfig<WeaponCfg>("WeaponConfig"); _shieldCfgDic = LoadConfig<ShieldCfg>("ShieldConfig"); } 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); Debug.Log(bossCfgInfo.text); //新建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; } //创建映射对象 以及 设置对象的值 public T CreateAndSetValue<T>(XmlElement element) where T : class,new() { T obj = new T();// T obj1 = Activator.CreateInstance<T>(); FieldInfo[] fieldInfos = typeof(T).GetFields(); foreach (FieldInfo fieldInfo in fieldInfos) { string value = element.GetAttribute(fieldInfo.Name); ParsePropertyValue<T>(fieldInfo, obj, value); } return obj; } //转换对应的类型 public void ParsePropertyValue<T>(FieldInfo fieldInfo,T obj,string valueString) where T : class,new() { System.Object value = null; if (fieldInfo.FieldType.IsEnum) { value = Enum.Parse(fieldInfo.FieldType, valueString); } else { if (fieldInfo.FieldType == typeof(int)) { value = int.Parse(valueString); } if (fieldInfo.FieldType == typeof(float)) { value = float.Parse(valueString); } if (fieldInfo.FieldType == typeof(bool)) { value = bool.Parse(valueString); } if (fieldInfo.FieldType == typeof(string)) { value = valueString; } } fieldInfo.SetValue(obj, value); } //get public BossCfg GetBossCfgByID(int id) { if (_bossCfgDic.ContainsKey(id)) { return _bossCfgDic[id]; } return null; } } //映射对象 + 字典 = save public class BossCfg { public int ID; public string Name; public int Hp; public string AttackType; } //武器数据映射的对象模板 public class WeaponCfg { public int ID; public string Name; public int EffectiveBulletCount; public int EffectiveTime; public ini public float AttackDistance; public string Display; } //盾牌数据映射的对象模板 //盾牌数据映射的对象模板 public class ShieldCfg { public int ID; public string Name; public int EffectiveParryTime; public int EffectiveTime; public string Display ; } using UnityEngine; using System.Collections; public class Singleton<T> where T : class,new() { private static T _instance; public static T Instance { get { if(_instance == null) { _instance = new T(); } return _instance; } } }
using UnityEngine;
using System.Collections;
//游戏入口
public class VRGameManager : MonoBehaviour {
// Use this for initialization void Start () { ConfigManager.Instance.LoadAllConfig(); BossCfg dog = ConfigManager.Instance.GetBossCfgByID(1); BossCfg bigdog = ConfigManager.Instance.GetBossCfgByID(5); Debug.Log(1234); } // Update is called once per frame void Update () { }
}
“`
相关文章推荐
- java读取xml配置文件(小结)
- 利用java反射读取xml配置文件
- [drp 3]读取Xml配置文件,连接数据库
- 通过读取web.xml文件中的配置参数连接数据库(原)
- drp—Dom4j读取xml配置文件
- Sqlserver:从XML文件中读取配置信息
- java读取xml配置文件(小结)
- delta3d中,读取自己的xml配置文件。
- 使用xml作为数据库的配置文件的路径读取问题
- 采用单例模式读取xml配置文件xml
- 使用dom4j读取xml配置文件
- 使用Spring中Src下 -context.xml : 用资源文件夹保存数据库的链接信息 并在配置文件里读取
- JAVA中读取XML文件中的配置信息
- [drp 3]读取Xml配置文件,连接数据库
- XML配置文件的读取处理
- PHP读取配置文件类(php,ini,yaml,xml)
- java读取xml配置文件
- 读取xml文件中的配置参数实例
- java读取xml配置文件(sax)
- Servlet(19)web.xml 配置之Servlet参数配置和读取资源文件,获取文件全路径