コード例 #1
0
    void TableParseMethod(TableProcessInfo _tableInfo)
    {
        ScriptableObject table
            = EditorHelper.CreateNewEditorProfile<ScriptableObject>(_tableInfo.ouput_asset_name,
                                                                    assetOutputPath,
                                                                    _tableInfo.table_class_name);
        if (table == null) {
            Debug.LogError("create table " + _tableInfo.table_class_name + " failed! output path : " + assetOutputPath + ", table class name : " + _tableInfo.table_class_name);
            return;
        }

        foreach (Dictionary<string, string> row in excelData.Values) {
            int index = 0;

            Type t = asm.GetType(_tableInfo.data_class_name);
            System.Object c = Activator.CreateInstance(t);
            foreach (string title in row.Keys) {
                FieldInfo mInfo = t.GetField(header[index], BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                if (mInfo != null) {
                    object param = EditorHelper.GetTableValue(type[index], row[title]);
                    if (param != null) {
        //                         try
        //                         {
                            t.InvokeMember(mInfo.Name, BindingFlags.SetField | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, c, new object[] { param });
        //                         }
        //                         catch(Exception e)
        //                         {
        //                             Debug.LogError("cannot set field " + header[index]);
        //                         }
                    }
                }

                PropertyInfo pInfo = t.GetProperty(header[index], BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                if (pInfo != null)
                {
                    object param = EditorHelper.GetTableValue(type[index], row[title]);
                    if (param != null)
                    {
                        t.InvokeMember(pInfo.Name, BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.Public, null, c, new object[] { param });
                    }
                }
                if (mInfo == null && pInfo == null)
                {
                    Debug.LogError("cannot find field " + header[index]);
                }
                index++;
            }

            IDataCollection lst = table as IDataCollection;
            if (lst != null) {
                lst.Add(c);
            }
            else {
                Debug.LogError(table.GetType() + " " + "isn't implemente IDataList interface!");
            }
        }
        EditorUtility.SetDirty(table);
        AssetDatabase.SaveAssets();
    }
コード例 #2
0
    void ExportTable(TableProcessInfo _tableInfo)
    {
        excelData = new Dictionary<string, Dictionary<string, string>>();
        Worksheet sheet
            = EditorHelper.LoadExcelSheet(AssetDatabase.GetAssetPath(_tableInfo.input_excel), _tableInfo.sheet_name);

        int maxColumns = sheet.Cells.MaxDataColumn + 1;
        Debug.Log("sheet Cells Columns Count " + maxColumns);
        // parse title
        header = new string[maxColumns];
        type = new string[maxColumns];
        for (int i = 0; i < maxColumns; ++i)
        {
            header[i] = sheet.Cells.Rows[characterTableTitleRow].GetCellOrNull(i)
                        != null ? sheet.Cells.Rows[1].GetCellOrNull(i).StringValue : null;
            if (header[i] != null) {
                header[i].Trim();
            }

            if (string.IsNullOrEmpty(header[i]))
            {
                Debug.LogError("null column name :" + i);
            }

            type[i] = sheet.Cells.Rows[characterTableValueTypeRow].GetCellOrNull(i)
                        != null ? sheet.Cells.Rows[2].GetCellOrNull(i).StringValue : null;
            if (type[i] != null) {
                type[i].Trim();
            }
            if (string.IsNullOrEmpty(type[i]))
            {
                Debug.LogError("null column type :" + i);
            }
            if (type[i] == "varchar")
            {
                type[i] = "string";
            }

            if (type[i] == "bit")
            {
                type[i] = "bool";
            }

            if (type[i] == "char")
            {
                type[i] = "byte";
            }

            Debug.Log("column : " + header[i] + ", type : " + type[i]);
        }

        // parse table content
        foreach (Row row in sheet.Cells.Rows)
        {
            if (row.Index <= characterTableValueTypeRow)
                continue;

            string key = row.GetCellOrNull(0) != null ? row.GetCellOrNull(0).StringValue : null;
            if (string.IsNullOrEmpty(key) == false)
            {
                if (excelData.ContainsKey(key) == false)
                {
                    excelData.Add(key, new Dictionary<string, string>());
                }

                for (int i = 0; i < maxColumns; ++i)
                {
                    string value = row.GetCellOrNull(i) != null ? row.GetCellOrNull(i).StringValue : null;

                    if (string.IsNullOrEmpty(value) == false)
                    {
                        if (excelData[key].ContainsKey(header[i]))
                        {
                            Debug.LogError("Try to add duplicate key " + header[i] + " for animation " + key);
                            continue;
                        }
                        excelData[key].Add(header[i], value);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(header[i]) == false)
                        {
                            excelData[key].Add(header[i], "");
                        }
                    }
                }
            }
        }
        /*
        foreach (string s in excelData.Keys)
        {
            string str = "";
            Dictionary<string, string> dict = excelData[s];
            foreach (string k in dict.Keys)
            {
                str += ((dict[k]) + " ");
            }
            Debug.Log(str);
        }
        */
        // parse excelData
        TableParseMethod(_tableInfo);
    }
コード例 #3
0
ファイル: TableProcess.cs プロジェクト: BobbyBao/Aegis
    void WriteXML(TableProcessInfo _tableInfo)
    {
        //    MonsterInfoTable test = new MonsterInfoTable();
        //    test.Elements.Add(new MonsterInfo());
        //    Utils.Save<MonsterInfoTable>(Application.dataPath + "test.xml", test);

        XmlDocument doc = new XmlDocument();
        XmlElement root = doc.CreateElement(_tableInfo.table_class_name);
        doc.AppendChild(root);
        XmlElement elements = doc.CreateElement("Elments");
        root.AppendChild(elements);

        foreach (Dictionary<string, string> row in excelData.Values)
        {
            int index = 0;
            XmlElement e = doc.CreateElement(_tableInfo.data_class_name);
            elements.AppendChild(e);
            foreach (string title in row.Keys)
            {
                XmlElement field = doc.CreateElement(header[index]);
                XmlText value = doc.CreateTextNode(row[title]);
                field.AppendChild(value);
                e.AppendChild(field);
                index++;
            }

        }

        string path = Path.Combine(assetOutputPath, _tableInfo.data_class_name + ".xml");

        doc.Save(path);
    }