资源配置的写入与读取(射击游戏)

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System.Reflection;
using System;
//处理加载缓存 获取配置的管理器
public class ConfigManager : Singleton<ConfigManager>
{
//三个字典分别对三个类进行存储
    private Dictionary<int, BoosCfg> booscfgDic = new Dictionary<int, BoosCfg>();
    private Dictionary<int, WeaponCfg> weaponfgDic = new Dictionary<int, WeaponCfg>();
    private Dictionary<int, ShieldCfg> shieldDic = new Dictionary<int, ShieldCfg>();
    public void LoadAllConfig()
    {
        booscfgDic = LoadBooscfg<BoosCfg>("BoosCofig");
        weaponfgDic = LoadBooscfg<WeaponCfg>("AttackWeapon");
        shieldDic = LoadBooscfg<ShieldCfg>("Defense");
    }
    public Dictionary<int, T> LoadBooscfg<T>(string filename) where T : class, new()
    {
        Dictionary<int, T> dic = new Dictionary<int, T>();
        //通过filename寻找到Resources下的文件
        TextAsset boosCfgInfo = Resources.Load<TextAsset>("Config/"
          + filename);
        //Debug.Log(boosCfgInfo);
        //新建xml文档 命名空间using System.Xml自带
        XmlDocument document = new XmlDocument();
        //把字符串加载到对象中
        document.LoadXml(boosCfgInfo.text);
        //获取根节点
        XmlNode rootNode = document.SelectSingleNode("Root");
        //取子节点集合
        XmlNodeList nodelist = rootNode.ChildNodes;
        //遍历
        foreach (XmlNode node in nodelist)
        {
            //节点 转换 元素类型
            XmlElement element = node as XmlElement;
            //Debug.Log(element.GetAttribute("ID"));
            //Debug.Log(element.InnerText);
            //BoosCfg booscfg = new BoosCfg();
            //booscfg.ID = int.Parse(element.GetAttribute("ID"));
            //booscfg.Hp = int.Parse(element.GetAttribute("Hp"));
            //booscfg.Name = element.GetAttribute("Name");
            //booscfg.AttackType = element.GetAttribute("AttackType");
            //booscfgDic.Add(booscfg.ID, booscfg);
            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 obj=Activator.CreateInstance<T>();
        //获取对象类型及旗下字段
        FieldInfo[] fieldInfos = typeof(T).GetFields();
        //遍历旗下字段
        foreach (FieldInfo fieldInfo in fieldInfos)
        {
        //获取当前子节点下各个字段内容值(value)
            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);
    }
    public BoosCfg GetBoosCfgByID(int id)
    {
        if (booscfgDic.ContainsKey(id))
        {
            return booscfgDic[id];
        }
        return null;
    }
    public class BoosCfg
    {
        public int ID;
        public int Hp;
        public string Name;
        public string AttackType;
    }
    public class WeaponCfg
    {
        public int ID;
        public string Name;
        public int EffectiveBulletCount;
        public int EffectiveTime;
        public int Hurt;
        public float AttackDistance;
        public string Display;
    }
    public class ShieldCfg
    {
        public int ID;
        public string Name;
        public int EffectiveParryTime;
        public int EffectiveTime;
        public string Display;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值