Esempio n. 1
0
        private void Export()
        {
            string newFilename = EditorUtility.SaveFilePanel("Export to CSV", EditorWindowTools.GetDirectoryName(csvFilename), csvFilename, "csv");

            if (!string.IsNullOrEmpty(newFilename))
            {
                csvFilename = newFilename;
                if (Application.platform == RuntimePlatform.WindowsEditor)
                {
                    csvFilename = csvFilename.Replace("/", "\\");
                }
                using (StreamWriter file = new StreamWriter(csvFilename, false, EncodingTypeTools.GetEncoding(encodingType)))
                {
                    // Write heading:
                    StringBuilder sb = new StringBuilder();
                    sb.Append("Field");
                    foreach (var language in table.languages)
                    {
                        sb.AppendFormat(",{0}", CSVExporter.CleanField(language));
                    }
                    file.WriteLine(sb);

                    // Write fields:
                    foreach (var field in table.fields)
                    {
                        sb = new StringBuilder();
                        sb.Append(CSVExporter.CleanField(field.name));
                        foreach (var value in field.values)
                        {
                            sb.AppendFormat(",{0}", CSVExporter.CleanField(value));
                        }
                        file.WriteLine(sb);
                    }
                }
                EditorUtility.DisplayDialog("Export Complete", "The localized text table was exported to CSV (comma-separated values) format. ", "OK");
            }
        }
 private static string CleanField(string s)
 {
     return(CSVExporter.CleanField(s));
 }
        private void Import()
        {
            if (!EditorUtility.DisplayDialog("Import CSV?",
                                             "Importing from CSV will overwrite the current contents. Are you sure?",
                                             "Import", "Cancel"))
            {
                return;
            }
            string newFilename = EditorUtility.OpenFilePanel("Import from CSV", EditorWindowTools.GetDirectoryName(csvFilename), "csv");

            if (!string.IsNullOrEmpty(newFilename))
            {
                csvFilename = newFilename;
                if (Application.platform == RuntimePlatform.WindowsEditor)
                {
                    csvFilename = csvFilename.Replace("/", "\\");
                }
                try {
                    using (StreamReader file = new StreamReader(csvFilename, Encoding.UTF8)) {
                        // Work with a temporary, new table:
                        LocalizedTextTable newTable = ScriptableObject.CreateInstance <LocalizedTextTable>();

                        // Read heading:
                        string[] values = CSVExporter.GetValues(file.ReadLine());
                        newTable.languages = new List <string>(values);
                        newTable.languages.RemoveAt(0);

                        // Read fields:
                        newTable.fields.Clear();
                        while (!file.EndOfStream)
                        {
                            values = CSVExporter.GetValues(file.ReadLine());
                            LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                            field.name = values[0];
                            for (int i = 1; i < values.Length; i++)
                            {
                                field.values.Add(values[i]);
                            }
                            newTable.fields.Add(field);
                        }

                        // If we got to the end, use the new table:
                        table.languages.Clear();
                        foreach (var newLanguage in newTable.languages)
                        {
                            table.languages.Add(newLanguage);
                        }
                        table.fields.Clear();
                        foreach (var newField in newTable.fields)
                        {
                            LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                            field.name   = newField.name;
                            field.values = new List <string>(newField.values);
                            table.fields.Add(field);
                        }
                        DestroyImmediate(newTable);
                    }
                } catch (System.Exception e) {
                    Debug.LogError(e.Message);
                    EditorUtility.DisplayDialog("Import Failed", "There was an error importing the CSV file.", "OK");
                }
                EditorUtility.DisplayDialog("Export Complete", "The localized text table was exported to CSV (comma-separated values) format. ", "OK");
            }
        }
Esempio n. 4
0
        private void Import()
        {
            if (!EditorUtility.DisplayDialog("Import CSV?",
                                             "Importing from CSV will overwrite the current contents. Are you sure?",
                                             "Import", "Cancel"))
            {
                return;
            }
            string newFilename = EditorUtility.OpenFilePanel("Import from CSV", EditorWindowTools.GetDirectoryName(csvFilename), "csv");

            if (!string.IsNullOrEmpty(newFilename))
            {
                csvFilename = newFilename;
                if (Application.platform == RuntimePlatform.WindowsEditor)
                {
                    csvFilename = csvFilename.Replace("/", "\\");
                }
                try
                {
                    // Read the source file and combine multiline rows:
                    var    sourceLines = new List <string>();
                    var    file        = new StreamReader(csvFilename, EncodingTypeTools.GetEncoding(encodingType));
                    string line;
                    while ((line = file.ReadLine()) != null)
                    {
                        sourceLines.Add(line.TrimEnd());
                    }
                    file.Close();
                    CombineMultilineSourceLines(sourceLines);
                    if (sourceLines.Count < 1)
                    {
                        throw new System.Exception("No lines read from CSV file.");
                    }

                    // Work with a temporary, new table:
                    LocalizedTextTable newTable = ScriptableObject.CreateInstance <LocalizedTextTable>();

                    // Read heading:
                    string[] values = CSVExporter.GetValues(sourceLines[0]);
                    sourceLines.RemoveAt(0);
                    newTable.languages = new List <string>(values);
                    newTable.languages.RemoveAt(0);

                    // Read fields:
                    newTable.fields.Clear();
                    while (sourceLines.Count > 0)
                    {
                        values = CSVExporter.GetValues(sourceLines[0]);
                        sourceLines.RemoveAt(0);
                        LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                        field.name = values[0];
                        for (int i = 1; i < values.Length; i++)
                        {
                            field.values.Add(values[i]);
                        }
                        newTable.fields.Add(field);
                    }

                    // If we got to the end, use the new table:
                    table.languages.Clear();
                    foreach (var newLanguage in newTable.languages)
                    {
                        table.languages.Add(newLanguage);
                    }
                    table.fields.Clear();
                    foreach (var newField in newTable.fields)
                    {
                        LocalizedTextTable.LocalizedTextField field = new LocalizedTextTable.LocalizedTextField();
                        field.name   = newField.name;
                        field.values = new List <string>(newField.values);
                        table.fields.Add(field);
                    }
                    DestroyImmediate(newTable);
                }
                catch (System.Exception e)
                {
                    Debug.LogError(e.Message);
                    EditorUtility.DisplayDialog("Import Failed", "There was an error importing the CSV file.", "OK");
                }
                EditorUtility.DisplayDialog("Import Complete", "The localized text table was imported from " + newFilename + ".", "OK");
            }
        }