示例#1
0
        public static void ExportJsonCSharp()
        {
            Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            if (objs == null || objs.Length == 0)
            {
                Debug.LogError("there is no object selected!");
                return;
            }
            foreach (Object obj in objs)
            {
                string objPath = AssetDatabase.GetAssetPath(obj);
                objPath = objPath.Substring(objPath.IndexOf("/") + 1);
                objPath = Application.dataPath + @"/" + objPath;
                if (!(objPath.EndsWith(".xlsx") || objPath.EndsWith(".xls")))
                {
                    return;
                }

                //string outfilename= objPath.Substring();

                //string outfilepath = Path.ChangeExtension(objPath, ".cs1");
                string CName = Path.GetFileNameWithoutExtension(objPath);
                //CName = string.Format("{0}_Table", CName);
                //string outfilepath = objPath.Replace(string.Format("{0}", CName), string.Format("{0}_Table", CName));
                //outfilepath = Path.ChangeExtension(outfilepath, ".cs1");


                string outfilepath = Path.ChangeExtension(AssetPackageMenu.Instance.m_AssetCsharpPathOut + @"/" + string.Format("{0}_Table", CName), ".cs");



                CodeGeneratorClass cgc1 = new CodeGeneratorClass();
                CodeGeneratorClass cgc2 = new CodeGeneratorClass();

                cgc1.CName     = string.Format("{0}_Row", CName);
                cgc1.CBaseName = ": IIDataTableRow";
                cgc2.CName     = string.Format("{0}_Table", CName);
                cgc2.CBaseName = ": IDataTable";



                #region//初始化信息
                try
                {
                    using (FileStream file = new FileStream(objPath, FileMode.Open, FileAccess.Read))
                    {
                        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(file);
                        var       mResultSets        = excelReader.AsDataSet();
                        DataTable dt = mResultSets.Tables[0];

                        for (int j = 0; j < dt.Columns.Count; ++j)
                        {
                            string[] str_head = dt.Rows[0][j].ToString().Split('|');
                            cgc1.AddProperty(str_head[0], str_head[1]);
                        }
                        JsonCodeTemplate.Generate(outfilepath, "GameMain.Table", cgc1, cgc2);
                    }
                }
                catch (Exception e)
                {
                    //DebugHandler.LogError(e.ToString());
                    throw e;
                }
                #endregion
            }

            AssetDatabase.Refresh();
        }
示例#2
0
        public static void Generate(string generateFilePath, string nameSpace, CodeGeneratorClass cgc1, CodeGeneratorClass cgc2)
        {
            StreamWriter  sw         = new StreamWriter(generateFilePath, false, Encoding.UTF8);
            StringBuilder strBuilder = new StringBuilder();

            {///class 1
                StringBuilder strbuild1 = new StringBuilder();
                strbuild1.AppendLine("public bool ParseRow(JObject jobj)");
                strbuild1.AppendLine("{");
                strbuild1.AppendLine("if(jobj == null)");
                strbuild1.AppendLine("{");
                strbuild1.AppendLine("DebugHandler.LogError(\"Null jobj\");");
                strbuild1.AppendLine("}");
                foreach (var cgp in cgc1.m_ls_CGP)
                {
                    strbuild1.AppendLine(string.Format("{0} = ({1})jobj[\"{2}\"];", cgp.Name, cgp.CGType.Name, cgp.Name));
                }
                strbuild1.AppendLine("return true;");
                strbuild1.AppendLine("}");
                cgc1.AddMethod(strbuild1.ToString());
            }
            {
                cgc2.AddProperty("Name", "string");
                cgc2.AddProperty("AssetId", "int");
                cgc2.AddProperty("IsLoad", "bool");

                StringBuilder strbuild1 = new StringBuilder();
                strbuild1.AppendLine(string.Format("private Dictionary<int,{0}> m_dict;", cgc1.CName));
                strbuild1.AppendLine(string.Format("public {0} GetRowById(int id)", cgc1.CName));
                strbuild1.AppendLine("{");
                strbuild1.AppendLine(string.Format("{0} dj_row = null;", cgc1.CName));
                strbuild1.AppendLine("m_dict.TryGetValue(id, out dj_row);");
                strbuild1.AppendLine("return dj_row;");
                strbuild1.AppendLine("}");
                cgc2.AddMethod(strbuild1.ToString());

                strbuild1 = new StringBuilder();
                strbuild1.AppendLine("public bool ParseTable(JArray jay)");
                strbuild1.AppendLine("{");
                strbuild1.AppendLine(string.Format("m_dict = new Dictionary<int,{0}>();", cgc1.CName));
                strbuild1.AppendLine("for(int i=0;i<jay.Count;++i)");
                strbuild1.AppendLine("{");
                strbuild1.AppendLine("var tmpjobj= jay[i] as JObject;");
                strbuild1.AppendLine(string.Format("{0} table_row = new {1}();", cgc1.CName, cgc1.CName));
                strbuild1.AppendLine("table_row.ParseRow(tmpjobj);");//
                strbuild1.AppendLine("m_dict.Add(table_row.id , table_row);");
                strbuild1.AppendLine("}");
                strbuild1.AppendLine("IsLoad = true;");
                strbuild1.AppendLine("return true;");
                strbuild1.AppendLine("}");
                cgc2.AddMethod(strbuild1.ToString());
            }


            //strBuilder.AppendLine("using UnityEngine;");
            CodeGenerator.CodeGeneratorNameSpace(new CodeGeneratorNamespace {
                Name = "System.Collections.Generic"
            }, strBuilder);
            CodeGenerator.CodeGeneratorNameSpace(new CodeGeneratorNamespace {
                Name = "GameFramework"
            }, strBuilder);
            CodeGenerator.CodeGeneratorNameSpace(new CodeGeneratorNamespace {
                Name = "GameFramework.Table"
            }, strBuilder);
            CodeGenerator.CodeGeneratorNameSpace(new CodeGeneratorNamespace {
                Name = "Newtonsoft.Json.Linq"
            }, strBuilder);

            strBuilder.AppendLine("");
            strBuilder.AppendLine();
            strBuilder.AppendLine("namespace " + nameSpace);
            strBuilder.AppendLine("{");

            //strBuilder.Append(cgc1);
            cgc1.Append(strBuilder);
            cgc2.Append(strBuilder);

            /*
             * strBuilder.AppendFormat("\tpublic class {0} : MonoBehaviour", CName);
             * strBuilder.AppendLine();
             * strBuilder.AppendLine("\t{");
             *
             * strBuilder.AppendLine("\t}");*/

            strBuilder.AppendLine("}");

            sw.Write(strBuilder);
            sw.Flush();
            sw.Close();
        }