Exemplo n.º 1
0
    /// <summary>
    /// 生成配置表对应的脚本文件
    /// </summary>
    private void GenerateCSFile()
    {
        if (dataTable == null || dataTable.Count <= 0)
        {
            if (generateType == GenerateType.Single_File)
            {
                ReadFileTable();
            }
            else if (generateType == GenerateType.Folder_Files)
            {
                ReadFoldTable();
            }
        }
        if (dataTable == null || dataTable.Count <= 0)
        {
            Debug.LogError("Excel read error");
        }
        for (int i = 0; i < dataTable.Count; i++)
        {
            //获取表里的字段参数
            int row = dataTable[i].Rows.Count;
            int col = dataTable[i].Columns.Count;
            List <ParameterDto> parameters = new List <ParameterDto>();
            string parName = "";
            string parNote = "";
            for (int j = 0; j < col; j++)
            {
                parNote = dataTable[i].Rows[2][j].ToString();
                parName = dataTable[i].Rows[1][j].ToString();
                if (string.IsNullOrEmpty(parName))
                {
                    continue;
                }
                Type         parType   = GetTypeByString(dataTable[i].Rows[0][j].ToString());
                ParameterDto parameter = new ParameterDto(j, parName, parNote, parType.ToString());
                parameters.Add(parameter);
            }

            //生成配置脚本
            CSharpCodeDom sharpCodeDom = new CSharpCodeDom();
            //根
            sharpCodeDom.CreatCodeDomUnit();
            //命名空间
            sharpCodeDom.AddNamespace("ED");
            //配置脚本类
            object class1 = sharpCodeDom.CreateCustomerClass(dataTable[i].TableName);
            //添加字段
            sharpCodeDom.AddFieldForClass(class1, parameters);
            //添加特性
            sharpCodeDom.AddAttributrDeclaration(class1, typeof(System.SerializableAttribute));

            string outPath = csOutPath + "/" + dataTable[i].TableName + ".cs";
            //生成代码文件
            if (sharpCodeDom.CodeDomGenerate(outPath))
            {
                //生成配置列表类
                GenerateListCS(dataTable[i]);
            }
        }
    }
Exemplo n.º 2
0
    /// <summary>
    /// 生成配置表列表文件
    /// </summary>
    /// <param name="dataTable"></param>
    private void GenerateListCS(DataTable dataTable)
    {
        //生成配置脚本
        CSharpCodeDom sharpCodeDom = new CSharpCodeDom();

        //根
        sharpCodeDom.CreatCodeDomUnit();
        //命名空间
        sharpCodeDom.AddNamespace("ED");
        //配置列表信息
        List <ParameterDto> fields = new List <ParameterDto>();

        fields.Add(new ParameterDto(0, "ConfigInfoList", "数据集合", $"List<{dataTable.TableName}>"));
        fields.Add(new ParameterDto(1, "ConfigInfoDic", "数据字典", $"Dictionary<{GetTypeByString(dataTable.Rows[0][0].ToString())},{dataTable.TableName}>"));
        //添加using命名空间
        sharpCodeDom.AddUsingNamespace("UnityEngine", "System.Collections.Generic", "System.Linq");
        //在对应配置cs文件下面添加列表类
        object class2 = sharpCodeDom.CreateCustomerClass(dataTable.TableName + "Config", "ScriptableObject");

        //添加字段
        sharpCodeDom.AddFieldForClass(class2, fields);
        //添加特性
        sharpCodeDom.AddAttributrDeclaration(class2, typeof(System.SerializableAttribute));

        //添加方法
        MethodParameter parameter = new MethodParameter();

        parameter.methodName = "GetConfigByKey";
        Dictionary <string, string> parDic = new Dictionary <string, string>();

        parDic.Add(GetTypeByString(dataTable.Rows[0][0].ToString()).ToString(), "key");
        parameter.methodParameter  = parDic;
        parameter.methodReturnType = dataTable.TableName;
        parameter.methodSpinnet    = $@"
            if (ConfigInfoList == null || ConfigInfoList.Count <= 0)
                return null;
            if(ConfigInfoDic==null)ConfigInfoDic = new Dictionary<{ GetTypeByString(dataTable.Rows[0][0].ToString())},{ dataTable.TableName}>();
            if(ConfigInfoDic.Count<=0)
                for(int i=0;i<ConfigInfoList.Count;i++)
                    ConfigInfoDic.Add(ConfigInfoList[i].{dataTable.Rows[1][0]},ConfigInfoList[i]);
            {dataTable.TableName} target = null;
            ConfigInfoDic.TryGetValue( key, out target);";
        parameter.methodReturn     = "target";

        sharpCodeDom.AddMemberMethod(class2, parameter);
        //生成
        if (sharpCodeDom.CodeDomGenerate(csOutPath + "/" + dataTable.TableName + "Config" + ".cs"))
        {
            AssetDatabase.Refresh();
            Debug.Log($"Generate {dataTable.TableName}.cs is Succeed");
        }
        else
        {
            Debug.LogError($"Generate {dataTable.TableName}.cs is Failed!");
        }
    }