Example #1
0
        static void SaveCSVMetaData(CSVMetaData meta, TextAsset csv)
        {
            AssetImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(csv));

            importer.userData = meta != null?meta.ToXml() : "";

            importer.SaveAndReimport();
        }
Example #2
0
        public static string Generate(string csvText, string className, CSVMetaData meta)
        {
            if (string.IsNullOrEmpty(csvText))
            {
                return(null);
            }

            string[][] grid = CsvParser.Parse(csvText);
            if (grid.Length < 1)
            {
                return(null);
            }

            string rowMemberCode = "";
            string rowReadCode   = "";
            string findCode      = "";

            for (int i = 0; i < grid[0].Length; i++)
            {
                string column = grid[0][i];
                string type   = meta == null ? "string" : meta.GetColumnType(column);
                rowMemberCode += string.Format("\t\tpublic {0} {1};\n", type, column);
                switch (type)
                {
                case "string":
                    rowReadCode += string.Format("\t\t\trow.{0} = grid[i][{1}];\n", column, i);
                    break;

                case "int":
                    rowReadCode += string.Format("\t\t\trow.{0} = string.IsNullOrEmpty(grid[i][{1}]) ? 0 : Convert.ToInt32(grid[i][{1}]);\n", column, i);
                    break;

                case "float":
                    rowReadCode += string.Format("\t\t\trow.{0} = string.IsNullOrEmpty(grid[i][{1}]) ? 0 : Convert.ToSingle(grid[i][{1}]);\n", column, i);
                    break;

                case "bool":
                    rowReadCode += string.Format("\t\t\trow.{0} = string.IsNullOrEmpty(grid[i][{1}]) ? false : Convert.ToBoolean(grid[i][{1}]);\n", column, i);
                    break;
                }
                findCode += findCodeTemplate.Replace("$COLUMN$", column).Replace("$COLUMN_TYPE$", type);
            }

            string code = codeTemplate;

            code = code.Replace("$CLASS$", className);
            code = code.Replace("$ROW_MEMBER_CODE$", rowMemberCode);
            code = code.Replace("$ROW_READ_CODE$", rowReadCode);
            code = code.Replace("$FIND_CODE$", findCode);
            code = code.Replace("$STATIC$", meta.isStatic ? "static" : "");
            code = code.Replace("$STATIC_LOAD_FUNCTION$", meta.isStatic ? staticLoadFunctionCode : "");
            code = code.Replace("$CALL_STATIC_LOAD$", meta.isStatic ? string.Format("StaticLoad();", meta.staticCsvPath) : "");
            code = code.Replace("$STATIC_CSV_PATH$", meta.isStatic ? meta.staticCsvPath : "");

            return(code);
        }
Example #3
0
        public static CSVMetaData CreateFromXml(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                return(null);
            }

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(CSVMetaData));
                CSVMetaData   data       = serializer.Deserialize(new XmlTextReader(new StringReader(xml))) as CSVMetaData;
                return(data);
            }
            catch (Exception e)
            {
                Debug.LogWarning(e.Message);
                return(null);
            }
        }
Example #4
0
        MonoScript CreateScript(TextAsset csv, string path, CSVMetaData metaData)
        {
            if (csv == null || string.IsNullOrEmpty(csv.text))
            {
                return(null);
            }

            string className = Path.GetFileNameWithoutExtension(path);
            string code      = TableCodeGen.Generate(csv.text, className, metaData);

            File.WriteAllText(path, code);
            Debug.Log("Table script generated: " + path);

            AssetDatabase.Refresh();

            // absolute path to relative
            if (path.StartsWith(Application.dataPath))
            {
                path = "Assets" + path.Substring(Application.dataPath.Length);
            }

            return(AssetDatabase.LoadAssetAtPath(path, typeof(MonoScript)) as MonoScript);
        }
Example #5
0
        public override void OnInspectorGUI()
        {
            GUI.enabled = true;

            if (!IsCsvAsset(target))
            {
                DrawText();
                return;
            }

            TextAsset csv = target as TextAsset;

            // Retrieve metaData from *.csv.meta file's userData
            AssetImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(csv));
            CSVMetaData   metaData = CSVMetaData.CreateFromXml(importer.userData);

            // Prevent UnityEditor Serialization...
            //if (metaData != null && metaData.columns.Count == 0)
            //  metaData = null;

            // Is Table?
            bool isTable = EditorGUILayout.Toggle("Is Table", metaData != null);

            if (isTable && metaData == null)
            {
                metaData = new CSVMetaData();
                metaData.UpdateColumns(csv);
                SaveCSVMetaData(metaData, csv);
                return;
            }
            if (!isTable && metaData != null)
            {
                SaveCSVMetaData(null, csv);
                return;
            }

            // if csv asset is not a table, early return.
            if (metaData == null)
            {
                if (!string.IsNullOrEmpty(importer.userData))
                {
                    string warn = string.Format("AssetImporter.userData is not empty: {0}\nMark 'Is Table' then reset AssetImporter.userData.",
                                                importer.userData);
                    EditorGUILayout.HelpBox(warn, MessageType.Warning);
                }
                DrawText();
                return;
            }

            // Is static?
            metaData.isStatic = EditorGUILayout.Toggle("Is Static", metaData.isStatic);
            if (metaData.isStatic)
            {
                string path = AssetDatabase.GetAssetPath(csv);
                if (!path.Contains("Resources/"))
                {
                    EditorGUILayout.HelpBox("'static table csv asset' must exists in Resources folder", MessageType.Warning);
                }
                metaData.staticCsvPath = AssetDatabase.GetAssetPath(csv).Replace("Assets/Resources/", "").Replace(".csv", "");
            }

            // csv asset
            EditorGUI.BeginDisabledGroup(true);
            EditorGUILayout.ObjectField("CSV", csv, typeof(TextAsset), false);
            EditorGUI.EndDisabledGroup();

            // Generated script
            MonoScript script = null;

            if (!string.IsNullOrEmpty(metaData.generatedScriptPath))
            {
                script = AssetDatabase.LoadAssetAtPath <MonoScript>(metaData.generatedScriptPath);
            }
            script = EditorGUILayout.ObjectField("Script", script, typeof(MonoScript), false) as MonoScript;
            metaData.generatedScriptPath = script == null ? "" : AssetDatabase.GetAssetPath(script);

            // buttons
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Update Columns"))
            {
                metaData.UpdateColumns(csv);
            }
            if (GUILayout.Button("Generate Script"))
            {
                if (string.IsNullOrEmpty(metaData.generatedScriptPath))
                {
                    metaData.generatedScriptPath = EditorUtility.SaveFilePanel("Save Script", "Assets", csv.name + "Table.cs", "cs");
                }
                if (!string.IsNullOrEmpty(metaData.generatedScriptPath))
                {
                    script = CreateScript(csv, metaData.generatedScriptPath, metaData);
                    metaData.generatedScriptPath = AssetDatabase.GetAssetPath(script);
                    EditorGUIUtility.PingObject(script);
                }
            }
            EditorGUILayout.EndHorizontal();

            // columns
            foldout = EditorGUILayout.Foldout(foldout, "Columns");
            if (foldout)
            {
                EditorGUI.indentLevel++;
                for (int i = 0; i < metaData.columns.Count; i++)
                {
                    string column    = metaData.columns[i];
                    string type      = metaData.columnTypes[i];
                    int    typeIndex = GetColumnTypeIndex(type);

                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.PrefixLabel(column);
                    int newTypeIndex = EditorGUILayout.Popup(typeIndex, CSVMetaData.ColumnType);
                    if (newTypeIndex != typeIndex)
                    {
                        metaData.columnTypes[i] = CSVMetaData.ColumnType[newTypeIndex];
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUI.indentLevel--;
            }

            if (GUI.changed)
            {
                SaveCSVMetaData(metaData, csv);
            }

            DrawText();
        }