接着上一篇,我们用脚本读取配置文件生成了一个C#类 现在根据配置文件中的需求来生成对应的脚本调用这个类
首先用同样的方式生成脚本的头文件和类名
/*
*生成第二个脚本 将数据存入
*/
String FilePath2 = CurDir + Pathname + "CfgTable.cs";
System.IO.StreamWriter file2 = new System.IO.StreamWriter(FilePath2, false);
//保存数据到文件
file2.Write("using System.Collections;\n");
file2.Write("using System.Collections.Generic;\n");
file2.Write("using UnityEngine;\n");
file2.Write("\n");
file2.Write("public class ");
file2.Write(Pathname+"CfgTable");
file2.Write("\n{\n\t");
定义一些通用的变量和函数
//生成一个数组将所有的类添加进来
file2.Write("private List<"+ Pathname + "> Data = new List<"+Pathname+">();\n\t");
//定义一个整型计算大小
file2.Write("private int size;\n\t");
此时生成后这个脚本的内容如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemCfgTable
{
private List<Item> Data = new List<Item>();
private int size;
}//可能没有这个大括号
这个时候我们需要根据配置信息需求来定义字典(Dictionary),用来通过字段找到相应的数据,配置文件如下
[func]为标识 每一行3条数据:
第一条数据为0或1 指返回的是类还是数组 例如:为0时定义字典为Dictionary<字段类型,Item> 为1 1时定义字典为Dictionary<字段类型,List<Item>>
第二条数据为用来作标识的字段序号,通过定义类的属性时候 第一个属性为0以此类推
public class Item
{
public readonly int id;//id
public readonly string name;//名字
public readonly string des;//描述
public readonly string des_extra;//额外描述
public readonly string des_exchange;//兑换描述
public readonly string des_produce;//产出描述
public readonly int type;//类型
public readonly int quality;//品质
public readonly int sex;//性别
public readonly int job;//职业
public readonly int kingdom;//阵营
public readonly int level;//使用等级
public readonly int out_value;//出售价格
public readonly int overlay;//叠加上限
public readonly int flag;//标志位[NL]
public readonly int expire_type;//过期类型
public readonly int expire_param;//过期参数
public readonly int open_gold;//开启所需元宝
public readonly string effect;//作用效果
public readonly int auction_type;//寄售类型
public readonly string small_icon;//40图标
public readonly string large_icon;//56icon
如图 id序号为0 name为1 以此类推
第三条数据是服务器设置配置信息传出来的备注 可以无视
因此,这里有4个需要生产的字典类型和函数,我们用如下方式生成
//生成对应的字典和查找函数
while ((line = sr.ReadLine()) != null)
{
if (line == "[func]")
{
continue;
}
else if (line == "")
{
break;
}
//读取每一行的数据
//定义字典的头部
file2.Write("private Dictionary<");
//用\t分割字符串将3个数据放入数组中
string[] vs = line.Split('\t');
//定义了2个数组将需要的2个数据添加进去
IsArray.Add(int.Parse(vs[0]));
Parameter.Add(vs[1]);
//判断用作标识的是有2个参数还是1个参数
string[] typelist = vs[1].Split(',');
//如果长度为0说明是单个标识的参数查找
if (typelist.Length == 1)
{
file2.Write(Types[int.Parse(vs[1])] + ",");
//判断返回的是数组还是类
if (int.Parse(vs[0]) == 0)
{
//将对应的标识名词作为字典名词的一部分 尽量规范代码
file2.Write(Pathname+"> DataBy" + PropertyName[int.Parse(vs[1])] + "Dic= new Dictionary<" + Types[int.Parse(vs[1])] + ","+ Pathname + ">();\n\t");
DicName.Add("DataBy" + PropertyName[int.Parse(vs[1])] + "Dic");
}
else if (int.Parse(vs[0]) == 1)
{
file2.Write(" List<"+ Pathname+">> DataBy" + PropertyName[int.Parse(vs[1])] + "Table= new Dictionary<" + Types[int.Parse(vs[1])] + ",List<"+ Pathname+">>();\n\t");
DicName.Add("DataBy" + PropertyName[int.Parse(vs[1])] + "Table");
}
}
//多个参数查找,需要用到Dictionary嵌套来实现
else
{
//先放入第一个
file2.Write(Types[int.Parse(typelist[0])] + ",");
//用for循环遍历所有参数并嵌套进来
for (int i = 1; i< typelist.Length; i++)