策划Excel一键转换为Unity中的类(ILRuntime)

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class CreateConfigFile : MonoBehaviour
{
   
    //配置表中的属性
    public class FactionNotice
    {
        public int ID;
        public int NoticeType;
        public int IconType;
        public string Info;
    }


    [MenuItem("Tools/生成配置表解析文件")]
    static void TestData()
    {
        CreateOne(new FactionNotice());
        AssetDatabase.Refresh();
    }


    static void CreateOne(object data)
    {
        string className = data.GetType().Name;
        var porps = data.GetType().GetFields();

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("using UnityEngine;\nusing System;\nusing System.Security;\nusing System.Collections.Generic;\n");
        sb.Append("namespace HotFix_Project.Config\n");
        sb.Append("{\n");
        sb.Append(string.Format("public class {0}Config : ConfigBase\n", className));
        sb.Append("{\n");
        sb.Append(string.Format("\tpublic class {0}Object\n", className));
        sb.Append("\t{\n");
        foreach (var pro in porps)
        {
            sb.Append(string.Format("\t\tpublic {0} {1};\n", pro.FieldType.Name, pro.Name));
        }
        sb.Append("\t}\n");
         

        // sb.Append("#if !NETFX_CORE\n");


        System.Text.StringBuilder sb2 = new System.Text.StringBuilder();

        string cName = className + "Config";
        string pName = className + "Object";
        string sName = className.ToLower() + "Obj";
        string dicName = className.ToLower() + "Dic";

        sb2.Append("\t\t\t\t" + pName + " " + sName + " = new " + pName + "();\n");
        foreach (var pro in porps)
        {
            if (pro.FieldType == typeof(string))
            {
                sb2.Append("\t\t\t\t" + sName + "." + pro.Name + " = childrenElement.Attribute(\"" + pro.Name + "\");\n");
            }
            else
            {
                //sb2.Append("\t\t\t\t" + sName + "." + pro.Name + " = " + pro.FieldType.Name + ".Parse(childrenElement.Attribute(\"" + pro.Name + "\"));\n");
                sb2.Append("\t\t\t\t" + pro.FieldType.Name + ".TryParse(childrenElement.Attribute(\"" + pro.Name + "\")" + ", out " + sName + "." + pro.Name + ");\n");
            }

        }
        sb2.Append("\t\t\t\t" + dicName + "[" + sName + ".ID]=  " + sName + ";\n");
        string fo = "\tpublic override bool Load(SecurityElement element)\n\t{\n\t\tif (element.Tag != \"Items\")\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t\tif (element.Children != null)\n\t\t{\n\t\t\tforeach (SecurityElement childrenElement in element.Children)\n\t\t\t{\n" + sb2.ToString() + "\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n";


        sb.Append(fo);

       

        string st = "\tpublic " + cName + " ." + pName + " GetConfigElementByID(int id)\n\t{\n\t\t" + pName + " " + sName + "= null;\n\t\t" + dicName + ".TryGetValue(id, out " + sName + ");\n\t\treturn " + sName + ";\n\t}\n";
        sb.Append(st);



        string st2 = string.Format("\tpublic Dictionary<int, {0}> {1} = new Dictionary<int, {0}>();\n", pName, dicName);
        sb.Append(st2);

        sb.Append("}\n");

        sb.Append("}");
        
        //输出的路径
        string path = Application.dataPath + "/../HotFix_Project/Config/" + className + "Config.cs";
        using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
        {
            using (StreamWriter sw = new StreamWriter(file))
            {
                sw.Write(sb.ToString());
            }
        }
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值