Example #1
0
        public static void CreateLocalisationSettings()
        {
            string soundName = "Assets/" + Constants.GameName.NameOfGame + "/Settings/Localisations/SoundSettings.asset";

            AssertExistingAsset(soundName);
            string textName = "Assets/" + Constants.GameName.NameOfGame + "/Settings/Localisations/TranslationSettings.asset";

            AssertExistingAsset(textName);
            string localisationName = "Assets/" + Constants.GameName.NameOfGame + "/Settings/Localisations/LocalisationSettings.asset";

            AssertExistingAsset(localisationName);

            SoundSettings soundAsset = ScriptableObject.CreateInstance <SoundSettings>();

            AssetDatabase.CreateAsset(soundAsset, soundName);

            TranslationSettings translationAsset = ScriptableObject.CreateInstance <TranslationSettings>();

            AssetDatabase.CreateAsset(translationAsset, textName);

            LocalisationSettings localisationAsset = ScriptableObject.CreateInstance <LocalisationSettings>();

            localisationAsset.Sound = new[] { soundAsset };
            localisationAsset.Text  = new[] { translationAsset };
            AssetDatabase.CreateAsset(localisationAsset, localisationName);

            Selection.activeObject = localisationAsset;
            EditorGUIUtility.PingObject(localisationAsset);
        }
            public void Draw()
            {
                // Draw title
                EditorGUILayout.Space();
                GUILayout.Label("Settings", EditorStyles.boldLabel);

                // Draw settings field
                var settings = LocalisationSettings.Current;

                settings =
                    (LocalisationSettings)EditorGUILayout.ObjectField(
                        "Settings",
                        settings,
                        typeof(LocalisationSettings),
                        false);

                LocalisationSettings.Current = settings;

                // if no settings then draw the help box and create button
                if (settings == null)
                {
                    EditorGUILayout.HelpBox(
                        "Please specify a settings object, or alternatively create a new one with the button below",
                        MessageType.Info);

                    if (GUILayout.Button("Create new LocalisationSettings"))
                    {
                        LocalisationSettings.Create();
                    }

                    return;
                }

                EditorGUILayout.Space();

                if (GUILayout.Button("Import Schema"))
                {
                    var path = GuiUtils.OpenCsvFileDialog();
                    if (!string.IsNullOrEmpty(path))
                    {
                        Importer.ImportSchema(path, settings);

                        Debug.Log("Finished importing to schema");
                    }
                }

                if (GUILayout.Button("Import All (schema & translations)"))
                {
                    var path = GuiUtils.OpenFolderPanel();
                    if (!string.IsNullOrEmpty(path))
                    {
                        Importer.ImportAll(path, settings);

                        Debug.Log("Finished importing everything!");
                    }
                }
            }
Example #3
0
        public static void ImportAll(string path, LocalisationSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var localisationTables = AssetDatabase.FindAssets("t:LocalisationTable")
                                     .Select(AssetDatabase.GUIDToAssetPath)
                                     .Select(AssetDatabase.LoadAssetAtPath <LocalisationTable>)
                                     .ToArray();

            var files = Directory.GetFiles(path, "*.csv");

            if (files.Length == 0)
            {
                throw new Exception("There are no .csv files in the directory specified");
            }

            var schemaFile = files[0];

            ImportSchema(schemaFile, settings);

            foreach (var file in files)
            {
                var fileName = Path.GetFileNameWithoutExtension(file);
                var table    = localisationTables.FirstOrDefault(t => t.name == fileName);
                if (table == null)
                {
                    Debug.LogWarning($"LocalisationTable could not be found with the name {fileName}");
                    continue;
                }

                ImportFromCsv(file, table, settings.Schema);
            }

            foreach (var localisationTable in localisationTables)
            {
                Resources.UnloadAsset(localisationTable);
            }
        }
Example #4
0
        public static void ImportSchema(string path, LocalisationSettings settings)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var csvCategories = new List <string>();
            var csvKeys       = new Dictionary <string, List <string> >();

            var settingsSerializedObject = new SerializedObject(settings);
            var schema     = settingsSerializedObject.FindProperty("schema");
            var categories = schema.FindPropertyRelative("categories");

            try
            {
                var lines = CsvParser.Parse(File.ReadAllText(path));

                // Step 1: populate csvCategories, and csvKeys from the CSV
                foreach (var line in lines)
                {
                    var categoryName = line[0];
                    var keyName      = line[1];

                    if (!csvCategories.Contains(categoryName))
                    {
                        csvCategories.Add(categoryName);
                    }

                    if (!csvKeys.ContainsKey(categoryName))
                    {
                        csvKeys.Add(categoryName, new List <string>());
                    }

                    var keys = csvKeys[categoryName];

                    if (!keys.Contains(keyName))
                    {
                        keys.Add(keyName);
                    }
                }

                // Step 2: Loop through each category found in the csv.
                foreach (var newCategoryName in csvCategories)
                {
                    // Step 2a: find the category serializedProperty that corresponds to the category in question
                    SerializedProperty category = null;
                    for (var i = 0; i < categories.arraySize; i++)
                    {
                        var existingCategory = categories.GetArrayElementAtIndex(i);
                        if (existingCategory.FindPropertyRelative("name").stringValue == newCategoryName)
                        {
                            category = existingCategory;
                            break;
                        }
                    }

                    // Step 2b: if it was not found then it's new - let's add it
                    if (category == null)
                    {
                        categories.InsertArrayElementAtIndex(categories.arraySize);
                        category = categories.GetArrayElementAtIndex(categories.arraySize - 1);
                        category.FindPropertyRelative("name").stringValue = newCategoryName;
                        category.FindPropertyRelative("keys").ClearArray();

                        Debug.Log("Add category: " + newCategoryName);
                    }

                    // Step 2c: Loop through each key in the category from the csv, adding any new ones
                    var keys = category.FindPropertyRelative("keys");
                    foreach (var newKeyName in csvKeys[newCategoryName])
                    {
                        // find out if the key exists
                        var keyAlreadyExists = false;
                        for (var i = 0; i < keys.arraySize; i++)
                        {
                            var key = keys.GetArrayElementAtIndex(i);
                            if (key.stringValue == newKeyName)
                            {
                                keyAlreadyExists = true;
                                break;
                            }
                        }

                        // if it does not exist, then add it.
                        if (!keyAlreadyExists)
                        {
                            keys.InsertArrayElementAtIndex(keys.arraySize);
                            var newKey = keys.GetArrayElementAtIndex(keys.arraySize - 1);
                            newKey.stringValue = newKeyName;

                            Debug.Log("Add Key: " + newCategoryName + " : " + newKeyName);
                        }
                    }
                }

                // TODO: Remove all catetgories/keys that were not found in the csv (should be optional, requires GUI)

                settingsSerializedObject.ApplyModifiedProperties();
                settingsSerializedObject.Update();
            }
            catch (Exception e)
            {
                Debug.LogError($"Could not load from CSV format: {e.Message}");
            }
        }