标签:
很多时候我们需要使用策划的Excel表来做游戏的静态数据配置, 而不是采用自己定义的xml或者U3D的scriptobject。
因为很多数据都是策划处理的,而策划最喜欢的就是excel,也只会用这个。如果不用excel,意味着数据需要你自己输入,纯属浪费时间。
所以很多时间我们和策划约定好VO类的字段,直接从Excel映射出VO类。
比如下面的这张表
另存为.csv 然后转换为.txt 注意编码都改为UTF-8
Vo 类
1 public class HeroVo 2 { 3 public int heroId; 4 5 public string heroName;///武将名称 6 7 //public int heroStar;///品质 8 public int bingZhongId;///职业 9 10 public string heroIcon;///icon(头像) 11 12 public string heroFace;///face(形象) 13 14 public string heroDescription;///描述 15 16 public float hp; 17 18 public float grownHp; 19 20 public float attack; 21 22 public float grownAttack; 23 24 public float defense; 25 26 public float grownDefense; 27 28 public float luckRate; /// 暴击率 29 30 public float missRate;///闪避 31 32 public int skill1_Id; 33 34 public int skill2_Id; 35 36 public HeroVo (Dictionary<string,string> paramters) 37 { 38 FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); 39 40 foreach (var item in paramters.Keys) 41 { 42 FieldInfo[] curfields = fields.Where(t => t.Name == item).ToArray(); 43 44 if (curfields != null && curfields.Length == 1) 45 { 46 //Debug.Log("key:" + item + "value:" + paramters[item]); 47 48 FieldInfo curField = curfields[0]; 49 50 if (curField.FieldType == typeof(int)) 51 { 52 curField.SetValue(this, int.Parse(paramters[item])); 53 } 54 else if (curField.FieldType == typeof(string)) 55 { 56 curField.SetValue(this, paramters[item]); 57 } 58 else if (curField.FieldType == typeof(float)) 59 { 60 curField.SetValue(this, float.Parse(paramters[item])); 61 } 62 } 63 } 64 } 65 66 67 public override string ToString() 68 { 69 StringBuilder builder = new StringBuilder(); 70 71 FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); 72 foreach (var item in fields) 73 { 74 builder.Append(" " + item.Name + " : " + item.GetValue(this) + " "); 75 } 76 77 return builder.ToString(); 78 } 79 }
Vo 类集合解析
1 public class StaticHeroDataPool 2 { 3 public static string ResourceDataPath = "battle/Data/Hero/StaticHeroDataModel"; 4 5 public List<HeroVo> heros = new List<HeroVo>(); 6 7 static StaticHeroDataPool _instatnce = null; 8 public static StaticHeroDataPool Instatnce 9 { 10 get 11 { 12 if (_instatnce == null) 13 { 14 _instatnce = new StaticHeroDataPool(); 15 16 _instatnce.AddData(LoadData(ResourceDataPath)); 17 } 18 return _instatnce; 19 } 20 } 21 22 private static string[] LoadData(string loadUrl) 23 { 24 TextAsset binAsset = Resources.Load(loadUrl, typeof(TextAsset)) as TextAsset; 25 26 string[] lineArray = binAsset.text.Split("\n"[0]); 27 28 /// 去掉最后一空行 29 int length = lineArray.Length; 30 string[] newlineArray = lineArray.Where((t, index) => (index < length - 1)).ToArray(); 31 32 return newlineArray; 33 } 34 35 private void AddData(string[] lineArray) 36 { 37 string[] fieldNames = lineArray[0].Replace("\r", "").Split(";"[0]); 38 39 for (int i = 1; i < lineArray.Length; i++) 40 { 41 string[] Values = lineArray[i].Replace("\r", "").Split(";"[0]); 42 43 Dictionary<string, string> paramters = new Dictionary<string, string>(); 44 for (int j = 0; j < fieldNames.Length; j++) 45 { 46 paramters.Add(fieldNames[j], Values[j]); 47 } 48 49 HeroVo heroVo = new HeroVo(paramters); 50 heros.Add(heroVo); 51 } 52 } 53 }
标签:
原文地址:http://www.cnblogs.com/chongxin/p/4454232.html